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 = class Perspective {
146 constructor(options) {
149 this.doc = options.doc;
151 this.perspective_id = options.id;
154 this.perspective_id = '';
158 options.callback.call(this);
162 return $.wiki.state.perspectives[this.perspective_id];
166 return this.perspective_id;
174 // called when perspective in initialized
175 if (!this.noupdate_hash_onenter) {
176 document.location.hash = '#' + this.perspective_id;
181 // called when user switches to another perspective
182 if (!this.noupdate_hash_onenter) {
183 document.location.hash = '';
192 // free UI state (don't store data here)
195 unfreezeState(frozenState) {
201 * Stub rendering (used in generating history)
203 $.wiki.renderStub = function(params)
205 params = $.extend({ 'filters': {} }, params);
206 var $elem = params.stub.clone();
207 $elem.removeClass('row-stub');
208 params.container.append($elem);
210 $('*[data-stub-value]', $elem).each(function() {
212 var field = $this.attr('data-stub-value');
214 var value = params.data[field];
216 if(params.filters[field])
217 value = params.filters[field](value);
219 if(value === null || value === undefined) return;
221 if(!$this.attr('data-stub-target')) {
225 $this.attr($this.attr('data-stub-target'), value);
226 $this.removeAttr('data-stub-target');
227 $this.removeAttr('data-stub-value');
238 class GenericDialog {
239 constructor(element) {
244 self.$elem = $(element);
246 if(!self.$elem.attr('data-ui-initialized')) {
247 console.log("Initializing dialog", this);
249 self.$elem.attr('data-ui-initialized', true);
256 * Steps to follow when the dialog in first loaded on page.
262 $('button[data-ui-action]', self.$elem).click(function(event) {
263 event.preventDefault();
265 var action = $(this).attr('data-ui-action');
266 console.log("Button pressed, action: ", action);
269 self[action + "Action"].call(self);
271 console.log("Action failed:", e);
272 // always hide on cancel
273 if(action == 'cancel')
280 * Prepare dialog for user. Clear any unnessary data.
290 'overflow-y': 'scroll'
308 $("*[data-ui-error-for]", this.$elem).text('');
311 reportErrors(errors) {
312 var global = $("*[data-ui-error-for='__all__']", this.$elem);
315 $("*[data-ui-error-for]", this.$elem).text('');
316 for (var field_name in errors)
318 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
321 unassigned.push(field_name);
325 span.text(errors[field_name].join(' '));
328 if(unassigned.length > 0)
329 global.text( global.text() + 'W formularzu wystąpiły błędy');
333 $.wiki.cls.GenericDialog = GenericDialog;
335 $.wiki.showDialog = function(selector, options) {
336 var elem = $(selector);
338 if(elem.length != 1) {
339 console.log("Failed to show dialog:", selector, elem);
344 var klass = elem.attr('data-ui-jsclass');
345 return new $.wiki.cls[klass](elem, options);
347 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