3 var noop = function() { };
11 "ScanGalleryPerspective": {
15 "CodeMirrorPerspective": {}
17 "VisualPerspective": {},
18 "HistoryPerspective": {},
19 "SummaryPerspective": {}
25 $.wiki.loadConfig = function() {
26 if(!window.localStorage)
30 var value = window.localStorage.getItem(CurrentDocument.id) || "{}";
31 var config = JSON.parse(value);
33 if (config.version == $.wiki.state.version) {
34 $.wiki.state.perspectives = $.extend($.wiki.state.perspectives, config.perspectives);
37 console.log("Failed to load config, using default.");
40 console.log("Loaded:", $.wiki.state, $.wiki.state.version);
43 $(window).bind('unload', function() {
44 if(window.localStorage)
45 window.localStorage.setItem(CurrentDocument.id, JSON.stringify($.wiki.state));
49 $.wiki.activePerspective = function() {
50 return this.perspectives[$("#tabs li a.active").parent().attr('id')];
53 $.wiki.exitContext = function() {
54 var ap = this.activePerspective();
59 $.wiki.enterContext = function(ap) {
63 $.wiki.isDirty = function() {
64 var ap = this.activePerspective();
65 return (!!CurrentDocument && CurrentDocument.has_local_changes) || ap.dirty();
68 $.wiki.newTab = function(doc, title, klass) {
69 var base_id = 'id' + Math.floor(Math.random()* 5000000000);
70 var id = (''+klass)+'_' + base_id;
71 var $tab = $('<li class="nav-item" id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" ><a href="#" class="nav-link">'
72 + title + ' <span class="badge badge-danger tabclose">x</span></a></li>');
73 var $view = $('<div class="editor '+klass+'" id="'+base_id+'"> </div>');
75 this.perspectives[id] = new $.wiki[klass]({
81 $('#tabs').append($tab);
82 $view.hide().appendTo('#editor');
89 $.wiki.initTab = function(options) {
90 var klass = $(options.tab).attr('data-ui-jsclass');
92 return new $.wiki[klass]({
94 id: $(options.tab).attr('id'),
95 callback: function() {
96 $.wiki.perspectives[this.perspective_id] = this;
98 options.callback.call(this);
103 $.wiki.perspectiveForTab = function(tab) { // element or id
104 return this.perspectives[ $(tab).attr('id')];
107 $.wiki.switchToTab = function(tab){
112 $tab = $(DEFAULT_PERSPECTIVE);
114 var $old_a = $tab.closest('.tabs').find('.active');
116 $old_a.each(function(){
117 var tab = $(this).parent()
118 $(this).removeClass('active');
119 self.perspectives[tab.attr('id')].onExit();
120 $('#' + tab.attr('data-ui-related')).hide();
124 $('a', tab).addClass('active');
125 $('#' + $tab.attr('data-ui-related')).show();
128 console.log($.wiki.perspectives);
130 $.wiki.perspectives[$tab.attr('id')].onEnter();
136 $.wiki.Perspective = function(options) {
139 this.doc = options.doc;
141 this.perspective_id = options.id;
144 this.perspective_id = '';
148 options.callback.call(this);
151 $.wiki.Perspective.prototype.config = function() {
152 return $.wiki.state.perspectives[this.perspective_id];
155 $.wiki.Perspective.prototype.toString = function() {
156 return this.perspective_id;
159 $.wiki.Perspective.prototype.dirty = function() {
163 $.wiki.Perspective.prototype.onEnter = function () {
164 // called when perspective in initialized
165 if (!this.noupdate_hash_onenter) {
166 document.location.hash = '#' + this.perspective_id;
170 $.wiki.Perspective.prototype.onExit = function () {
171 // called when user switches to another perspective
172 if (!this.noupdate_hash_onenter) {
173 document.location.hash = '';
177 $.wiki.Perspective.prototype.destroy = function() {
181 $.wiki.Perspective.prototype.freezeState = function () {
182 // free UI state (don't store data here)
185 $.wiki.Perspective.prototype.unfreezeState = function (frozenState) {
190 * Stub rendering (used in generating history)
192 $.wiki.renderStub = function(params)
194 params = $.extend({ 'filters': {} }, params);
195 var $elem = params.stub.clone();
196 $elem.removeClass('row-stub');
197 params.container.append($elem);
199 $('*[data-stub-value]', $elem).each(function() {
201 var field = $this.attr('data-stub-value');
203 var value = params.data[field];
205 if(params.filters[field])
206 value = params.filters[field](value);
208 if(value === null || value === undefined) return;
210 if(!$this.attr('data-stub-target')) {
214 $this.attr($this.attr('data-stub-target'), value);
215 $this.removeAttr('data-stub-target');
216 $this.removeAttr('data-stub-value');
227 function GenericDialog(element) {
232 self.$elem = $(element);
234 if(!self.$elem.attr('data-ui-initialized')) {
235 console.log("Initializing dialog", this);
237 self.$elem.attr('data-ui-initialized', true);
243 GenericDialog.prototype = {
246 * Steps to follow when the dialog in first loaded on page.
248 initialize: function(){
252 $('button[data-ui-action]', self.$elem).click(function(event) {
253 event.preventDefault();
255 var action = $(this).attr('data-ui-action');
256 console.log("Button pressed, action: ", action);
259 self[action + "Action"].call(self);
261 console.log("Action failed:", e);
262 // always hide on cancel
263 if(action == 'cancel')
270 * Prepare dialog for user. Clear any unnessary data.
287 cancelAction: function() {
291 doneAction: function() {
295 clearForm: function() {
296 $("*[data-ui-error-for]", this.$elem).text('');
299 reportErrors: function(errors) {
300 var global = $("*[data-ui-error-for='__all__']", this.$elem);
303 $("*[data-ui-error-for]", this.$elem).text('');
304 for (var field_name in errors)
306 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
309 unassigned.push(field_name);
313 span.text(errors[field_name].join(' '));
316 if(unassigned.length > 0)
317 global.text( global.text() + 'W formularzu wystąpiły błędy');
321 $.wiki.cls.GenericDialog = GenericDialog;
323 $.wiki.showDialog = function(selector, options) {
324 var elem = $(selector);
326 if(elem.length != 1) {
327 console.log("Failed to show dialog:", selector, elem);
332 var klass = elem.attr('data-ui-jsclass');
333 return new $.wiki.cls[klass](elem, options);
335 console.log("Failed to show dialog", selector, klass, e);