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.active").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 $view = $('<div class="editor '+klass+'" id="'+base_id+'"> </div>');
66 this.perspectives[id] = new $.wiki[klass]({
72 $('#tabs').append($tab);
73 $view.hide().appendTo('#editor');
80 $.wiki.initTab = function(options) {
81 var klass = $(options.tab).attr('data-ui-jsclass');
83 return new $.wiki[klass]({
85 id: $(options.tab).attr('id'),
86 callback: function() {
87 $.wiki.perspectives[this.perspective_id] = this;
89 options.callback.call(this);
94 $.wiki.perspectiveForTab = function(tab) { // element or id
95 return this.perspectives[ $(tab).attr('id')];
98 $.wiki.switchToTab = function(tab){
103 $tab = $(DEFAULT_PERSPECTIVE);
105 var $old = $tab.closest('.tabs').find('.active');
107 $old.each(function(){
108 $(this).removeClass('active');
109 self.perspectives[$(this).attr('id')].onExit();
110 $('#' + $(this).attr('data-ui-related')).hide();
114 $tab.addClass('active');
115 $('#' + $tab.attr('data-ui-related')).show();
118 console.log($.wiki.perspectives);
120 $.wiki.perspectives[$tab.attr('id')].onEnter();
126 $.wiki.Perspective = function(options) {
129 this.doc = options.doc;
131 this.perspective_id = options.id;
134 this.perspective_id = '';
138 options.callback.call(this);
141 $.wiki.Perspective.prototype.config = function() {
142 return $.wiki.state.perspectives[this.perspective_id];
145 $.wiki.Perspective.prototype.toString = function() {
146 return this.perspective_id;
149 $.wiki.Perspective.prototype.dirty = function() {
153 $.wiki.Perspective.prototype.onEnter = function () {
154 // called when perspective in initialized
155 if (!this.noupdate_hash_onenter) {
156 document.location.hash = '#' + this.perspective_id;
160 $.wiki.Perspective.prototype.onExit = function () {
161 // called when user switches to another perspective
162 if (!this.noupdate_hash_onenter) {
163 document.location.hash = '';
167 $.wiki.Perspective.prototype.destroy = function() {
171 $.wiki.Perspective.prototype.freezeState = function () {
172 // free UI state (don't store data here)
175 $.wiki.Perspective.prototype.unfreezeState = function (frozenState) {
180 * Stub rendering (used in generating history)
182 $.wiki.renderStub = function(params)
184 params = $.extend({ 'filters': {} }, params);
185 var $elem = params.stub.clone();
186 $elem.removeClass('row-stub');
187 params.container.append($elem);
189 $('*[data-stub-value]', $elem).each(function() {
191 var field = $this.attr('data-stub-value');
193 var value = params.data[field];
195 if(params.filters[field])
196 value = params.filters[field](value);
198 if(value === null || value === undefined) return;
200 if(!$this.attr('data-stub-target')) {
204 $this.attr($this.attr('data-stub-target'), value);
205 $this.removeAttr('data-stub-target');
206 $this.removeAttr('data-stub-value');
217 function GenericDialog(element) {
222 self.$elem = $(element);
224 if(!self.$elem.attr('data-ui-initialized')) {
225 console.log("Initializing dialog", this);
227 self.$elem.attr('data-ui-initialized', true);
233 GenericDialog.prototype = {
236 * Steps to follow when the dialog in first loaded on page.
238 initialize: function(){
242 $('button[data-ui-action]', self.$elem).click(function(event) {
243 event.preventDefault();
245 var action = $(this).attr('data-ui-action');
246 console.log("Button pressed, action: ", action);
249 self[action + "Action"].call(self);
251 console.log("Action failed:", e);
252 // always hide on cancel
253 if(action == 'cancel')
260 * Prepare dialog for user. Clear any unnessary data.
277 cancelAction: function() {
281 doneAction: function() {
285 clearForm: function() {
286 $("*[data-ui-error-for]", this.$elem).text('');
289 reportErrors: function(errors) {
290 var global = $("*[data-ui-error-for='__all__']", this.$elem);
293 for (var field_name in errors)
295 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
298 unassigned.push(field_name);
302 span.text(errors[field_name].join(' '));
305 if(unassigned.length > 0)
306 global.text( global.text() + 'W formularzu wystąpiły błędy');
310 $.wiki.cls.GenericDialog = GenericDialog;
312 $.wiki.showDialog = function(selector, options) {
313 var elem = $(selector);
315 if(elem.length != 1) {
316 console.log("Failed to show dialog:", selector, elem);
321 var klass = elem.attr('data-ui-jsclass');
322 return new $.wiki.cls[klass](elem, options);
324 console.log("Failed to show dialog", selector, klass, e);