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.exitTab = function(tab){
110 if (!('.active', $tab)) return;
111 $('.active', $tab).removeClass('active');
112 self.perspectives[$tab.attr('id')].onExit();
113 $('#' + $tab.attr('data-ui-related')).hide();
116 $.wiki.switchToTab = function(tab){
121 $tab = $(DEFAULT_PERSPECTIVE);
123 var $old_a = $tab.closest('.tabs').find('.active');
125 $old_a.each(function(){
126 var tab = $(this).parent()
127 $(this).removeClass('active');
128 self.perspectives[tab.attr('id')].onExit();
129 $('#' + tab.attr('data-ui-related')).hide();
133 $('a', tab).addClass('active');
134 $('#' + $tab.attr('data-ui-related')).show();
137 console.log($.wiki.perspectives);
139 $.wiki.perspectives[$tab.attr('id')].onEnter();
145 $.wiki.Perspective = function(options) {
148 this.doc = options.doc;
150 this.perspective_id = options.id;
153 this.perspective_id = '';
157 options.callback.call(this);
160 $.wiki.Perspective.prototype.config = function() {
161 return $.wiki.state.perspectives[this.perspective_id];
164 $.wiki.Perspective.prototype.toString = function() {
165 return this.perspective_id;
168 $.wiki.Perspective.prototype.dirty = function() {
172 $.wiki.Perspective.prototype.onEnter = function () {
173 // called when perspective in initialized
174 if (!this.noupdate_hash_onenter) {
175 document.location.hash = '#' + this.perspective_id;
179 $.wiki.Perspective.prototype.onExit = function () {
180 // called when user switches to another perspective
181 if (!this.noupdate_hash_onenter) {
182 document.location.hash = '';
186 $.wiki.Perspective.prototype.destroy = function() {
190 $.wiki.Perspective.prototype.freezeState = function () {
191 // free UI state (don't store data here)
194 $.wiki.Perspective.prototype.unfreezeState = function (frozenState) {
199 * Stub rendering (used in generating history)
201 $.wiki.renderStub = function(params)
203 params = $.extend({ 'filters': {} }, params);
204 var $elem = params.stub.clone();
205 $elem.removeClass('row-stub');
206 params.container.append($elem);
208 $('*[data-stub-value]', $elem).each(function() {
210 var field = $this.attr('data-stub-value');
212 var value = params.data[field];
214 if(params.filters[field])
215 value = params.filters[field](value);
217 if(value === null || value === undefined) return;
219 if(!$this.attr('data-stub-target')) {
223 $this.attr($this.attr('data-stub-target'), value);
224 $this.removeAttr('data-stub-target');
225 $this.removeAttr('data-stub-value');
236 function GenericDialog(element) {
241 self.$elem = $(element);
243 if(!self.$elem.attr('data-ui-initialized')) {
244 console.log("Initializing dialog", this);
246 self.$elem.attr('data-ui-initialized', true);
252 GenericDialog.prototype = {
255 * Steps to follow when the dialog in first loaded on page.
257 initialize: function(){
261 $('button[data-ui-action]', self.$elem).click(function(event) {
262 event.preventDefault();
264 var action = $(this).attr('data-ui-action');
265 console.log("Button pressed, action: ", action);
268 self[action + "Action"].call(self);
270 console.log("Action failed:", e);
271 // always hide on cancel
272 if(action == 'cancel')
279 * Prepare dialog for user. Clear any unnessary data.
289 'overflow-y': 'scroll'
298 cancelAction: function() {
302 doneAction: function() {
306 clearForm: function() {
307 $("*[data-ui-error-for]", this.$elem).text('');
310 reportErrors: function(errors) {
311 var global = $("*[data-ui-error-for='__all__']", this.$elem);
314 $("*[data-ui-error-for]", this.$elem).text('');
315 for (var field_name in errors)
317 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
320 unassigned.push(field_name);
324 span.text(errors[field_name].join(' '));
327 if(unassigned.length > 0)
328 global.text( global.text() + 'W formularzu wystąpiły błędy');
332 $.wiki.cls.GenericDialog = GenericDialog;
334 $.wiki.showDialog = function(selector, options) {
335 var elem = $(selector);
337 if(elem.length != 1) {
338 console.log("Failed to show dialog:", selector, elem);
343 var klass = elem.attr('data-ui-jsclass');
344 return new $.wiki.cls[klass](elem, options);
346 console.log("Failed to show dialog", selector, klass, e);
352 window.addEventListener("message", (event) => {
355 $.ajax("/editor/editor-user-area/", {
356 success: function(d) {
357 $("#user-area")[0].innerHTML = d;
362 $("#login").click(function (e) {
366 let x = window.screenX + (window.innerWidth - w) / 2;
367 let y = window.screenY + (window.innerHeight - h) / 2;
369 "/accounts/login/?next=/editor/back",
371 "width=" + w + " height=" + h + " top=" + y + " left=" + x