3 var noop = function() { };
11 "CodeMirrorPerspective": {}
16 $.wiki.loadConfig = function() {
17 if(!window.localStorage)
21 var value = window.localStorage.getItem(CurrentDocument.id) || "{}";
22 var config = JSON.parse(value);
24 if (config.version == $.wiki.state.version) {
25 $.wiki.state.perspectives = $.extend($.wiki.state.perspectives, config.perspectives);
28 console.log("Failed to load config, using default.");
31 console.log("Loaded:", $.wiki.state, $.wiki.state.version);
34 $(window).bind('unload', function() {
35 if(window.localStorage)
36 window.localStorage.setItem(CurrentDocument.id, JSON.stringify($.wiki.state));
40 $.wiki.activePerspective = function() {
41 return this.perspectives[$("#tabs li a.active").parent().attr('id')];
44 $.wiki.exitContext = function() {
45 var ap = this.activePerspective();
50 $.wiki.enterContext = function(ap) {
54 $.wiki.isDirty = function() {
55 var ap = this.activePerspective();
56 return (!!CurrentDocument && CurrentDocument.has_local_changes) || ap.dirty();
59 $.wiki.newTab = function(doc, title, klass) {
60 var base_id = 'id' + Math.floor(Math.random()* 5000000000);
61 var id = (''+klass)+'_' + base_id;
62 var $tab = $('<li id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" >'
63 + title + '<img src="'+STATIC_URL+'icons/close.png" class="tabclose"></li>');
64 var $tab = $('<li class="nav-item" id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" ><a href="#" class="nav-link">'
65 + title + ' <span class="badge badge-danger tabclose">x</span></a></li>');
67 var $view = $('<div class="editor '+klass+'" id="'+base_id+'"> </div>');
69 this.perspectives[id] = new $.wiki[klass]({
75 $('#tabs').append($tab);
76 $view.hide().appendTo('#editor');
83 $.wiki.initTab = function(options) {
84 var klass = $(options.tab).attr('data-ui-jsclass');
86 return new $.wiki[klass]({
88 id: $(options.tab).attr('id'),
89 callback: function() {
90 $.wiki.perspectives[this.perspective_id] = this;
92 options.callback.call(this);
97 $.wiki.perspectiveForTab = function(tab) { // element or id
98 return this.perspectives[ $(tab).attr('id')];
101 $.wiki.switchToTab = function(tab){
106 $tab = $(DEFAULT_PERSPECTIVE);
108 var $old_a = $tab.closest('.tabs').find('.active');
110 $old_a.each(function(){
111 var tab = $(this).parent();
112 $(this).removeClass('active');
113 self.perspectives[tab.attr('id')].onExit();
114 $('#' + tab.attr('data-ui-related')).hide();
118 $('a', tab).addClass('active');
119 $('#' + $tab.attr('data-ui-related')).show();
122 console.log($.wiki.perspectives);
124 $.wiki.perspectives[$tab.attr('id')].onEnter();
130 $.wiki.Perspective = function(options) {
133 this.doc = options.doc;
135 this.perspective_id = options.id;
138 this.perspective_id = '';
142 options.callback.call(this);
145 $.wiki.Perspective.prototype.config = function() {
146 return $.wiki.state.perspectives[this.perspective_id];
149 $.wiki.Perspective.prototype.toString = function() {
150 return this.perspective_id;
153 $.wiki.Perspective.prototype.dirty = function() {
157 $.wiki.Perspective.prototype.onEnter = function () {
158 // called when perspective in initialized
159 if (!this.noupdate_hash_onenter) {
160 document.location.hash = '#' + this.perspective_id;
164 $.wiki.Perspective.prototype.onExit = function () {
165 // called when user switches to another perspective
166 if (!this.noupdate_hash_onenter) {
167 document.location.hash = '';
171 $.wiki.Perspective.prototype.destroy = function() {
175 $.wiki.Perspective.prototype.freezeState = function () {
176 // free UI state (don't store data here)
179 $.wiki.Perspective.prototype.unfreezeState = function (frozenState) {
184 * Stub rendering (used in generating history)
186 $.wiki.renderStub = function(params)
188 params = $.extend({ 'filters': {} }, params);
189 var $elem = params.stub.clone();
190 $elem.removeClass('row-stub');
191 params.container.append($elem);
193 $('*[data-stub-value]', $elem).each(function() {
195 var field = $this.attr('data-stub-value');
197 var value = params.data[field];
199 if(params.filters[field])
200 value = params.filters[field](value);
202 if(value === null || value === undefined) return;
204 if(!$this.attr('data-stub-target')) {
208 $this.attr($this.attr('data-stub-target'), value);
209 $this.removeAttr('data-stub-target');
210 $this.removeAttr('data-stub-value');
221 function GenericDialog(element) {
226 self.$elem = $(element);
228 if(!self.$elem.attr('data-ui-initialized')) {
229 console.log("Initializing dialog", this);
231 self.$elem.attr('data-ui-initialized', true);
237 GenericDialog.prototype = {
240 * Steps to follow when the dialog in first loaded on page.
242 initialize: function(){
246 $('button[data-ui-action]', self.$elem).click(function(event) {
247 event.preventDefault();
249 var action = $(this).attr('data-ui-action');
250 console.log("Button pressed, action: ", action);
253 self[action + "Action"].call(self);
255 console.log("Action failed:", e);
256 // always hide on cancel
257 if(action == 'cancel')
264 * Prepare dialog for user. Clear any unnessary data.
281 cancelAction: function() {
285 doneAction: function() {
289 clearForm: function() {
290 $("*[data-ui-error-for]", this.$elem).text('');
293 reportErrors: function(errors) {
294 var global = $("*[data-ui-error-for='__all__']", this.$elem);
297 for (var field_name in errors)
299 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
302 unassigned.push(field_name);
306 span.text(errors[field_name].join(' '));
309 if(unassigned.length > 0)
310 global.text( global.text() + 'W formularzu wystąpiły błędy');
314 $.wiki.cls.GenericDialog = GenericDialog;
316 $.wiki.showDialog = function(selector, options) {
317 var elem = $(selector);
319 if(elem.length != 1) {
320 console.log("Failed to show dialog:", selector, elem);
325 var klass = elem.attr('data-ui-jsclass');
326 return new $.wiki.cls[klass](elem, options);
328 console.log("Failed to show dialog", selector, klass, e);