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.active").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 id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" >'
72 + title + '<img src="/static/icons/close.png" class="tabclose"></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 = $('#tabs li').filter('.active');
116 $old.each(function(){
117 $(this).removeClass('active');
118 self.perspectives[$(this).attr('id')].onExit();
119 $('#' + $(this).attr('data-ui-related')).hide();
123 $tab.addClass('active');
124 $('#' + $tab.attr('data-ui-related')).show();
127 console.log($.wiki.perspectives);
129 $.wiki.perspectives[$tab.attr('id')].onEnter();
135 $.wiki.Perspective = function(options) {
138 this.doc = options.doc;
140 this.perspective_id = options.id;
143 this.perspective_id = '';
147 options.callback.call(this);
150 $.wiki.Perspective.prototype.config = function() {
151 return $.wiki.state.perspectives[this.perspective_id];
154 $.wiki.Perspective.prototype.toString = function() {
155 return this.perspective_id;
158 $.wiki.Perspective.prototype.dirty = function() {
162 $.wiki.Perspective.prototype.onEnter = function () {
163 // called when perspective in initialized
164 if (!this.noupdate_hash_onenter) {
165 document.location.hash = '#' + this.perspective_id;
169 $.wiki.Perspective.prototype.onExit = function () {
170 // called when user switches to another perspective
171 if (!this.noupdate_hash_onenter) {
172 document.location.hash = '';
176 $.wiki.Perspective.prototype.destroy = function() {
180 $.wiki.Perspective.prototype.freezeState = function () {
181 // free UI state (don't store data here)
184 $.wiki.Perspective.prototype.unfreezeState = function (frozenState) {
189 * Stub rendering (used in generating history)
191 $.wiki.renderStub = function(params)
193 params = $.extend({ 'filters': {} }, params);
194 var $elem = params.stub.clone();
195 $elem.removeClass('row-stub');
196 params.container.append($elem);
198 $('*[data-stub-value]', $elem).each(function() {
200 var field = $this.attr('data-stub-value');
202 var value = params.data[field];
204 if(params.filters[field])
205 value = params.filters[field](value);
207 if(value === null || value === undefined) return;
209 if(!$this.attr('data-stub-target')) {
213 $this.attr($this.attr('data-stub-target'), value);
214 $this.removeAttr('data-stub-target');
215 $this.removeAttr('data-stub-value');
226 function GenericDialog(element) {
231 self.$elem = $(element);
233 if(!self.$elem.attr('data-ui-initialized')) {
234 console.log("Initializing dialog", this);
236 self.$elem.attr('data-ui-initialized', true);
242 GenericDialog.prototype = {
245 * Steps to follow when the dialog in first loaded on page.
247 initialize: function(){
251 $('button[data-ui-action]', self.$elem).click(function(event) {
252 event.preventDefault();
254 var action = $(this).attr('data-ui-action');
255 console.log("Button pressed, action: ", action);
258 self[action + "Action"].call(self);
260 console.log("Action failed:", e);
261 // always hide on cancel
262 if(action == 'cancel')
269 * Prepare dialog for user. Clear any unnessary data.
286 cancelAction: function() {
290 doneAction: function() {
294 clearForm: function() {
295 $("*[data-ui-error-for]", this.$elem).text('');
298 reportErrors: function(errors) {
299 var global = $("*[data-ui-error-for='__all__']", this.$elem);
302 for (var field_name in errors)
304 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
307 unassigned.push(field_name);
311 span.text(errors[field_name].join(' '));
314 if(unassigned.length > 0)
315 global.text( global.text() + 'W formularzu wystąpiły błędy');
319 $.wiki.cls.GenericDialog = GenericDialog;
321 $.wiki.showDialog = function(selector, options) {
322 var elem = $(selector);
324 if(elem.length != 1) {
325 console.log("Failed to show dialog:", selector, elem);
330 var klass = elem.attr('data-ui-jsclass');
331 return new $.wiki.cls[klass](elem, options);
333 console.log("Failed to show dialog", selector, klass, e);