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, base_id) {
69 var id = (''+klass)+'_' + base_id;
70 var $tab = $('<li class="nav-item" id="'+id+'" data-ui-related="'+base_id+'" data-ui-jsclass="'+klass+'" ><a href="#" class="nav-link">'
71 + title + ' <span class="badge badge-danger tabclose">x</span></a></li>');
72 var $view = $('<div class="editor '+klass+'" id="'+base_id+'"> </div>');
74 this.perspectives[id] = new $.wiki[klass]({
80 $('#tabs').append($tab);
81 $view.hide().appendTo('#editor');
88 $.wiki.initTab = function(options) {
89 var klass = $(options.tab).attr('data-ui-jsclass');
91 let perspective = new $.wiki[klass]({
93 id: $(options.tab).attr('id'),
95 $.wiki.perspectives[perspective.perspective_id] = perspective;
99 $.wiki.perspectiveForTab = function(tab) { // element or id
100 return this.perspectives[ $(tab).attr('id')];
103 $.wiki.exitTab = function(tab){
106 if (!('.active', $tab).length) return;
107 $('.active', $tab).removeClass('active');
108 self.perspectives[$tab.attr('id')].onExit();
109 $('#' + $tab.attr('data-ui-related')).hide();
112 $.wiki.switchToTab = function(tab){
116 // Create dynamic tabs (for diffs).
117 if ($tab.length != 1) {
118 let parts = tab.split('_');
119 if (parts.length > 1) {
120 // TODO: register perspectives for it.
121 if (parts[0] == '#DiffPerspective') {
122 $tab = $($.wiki.DiffPerspective.openId(parts[1]));
128 $tab = $(DEFAULT_PERSPECTIVE);
130 var $old_a = $tab.closest('.tabs').find('.active');
132 $old_a.each(function(){
133 var tab = $(this).parent()
134 $(this).removeClass('active');
135 self.perspectives[tab.attr('id')].onExit();
136 $('#' + tab.attr('data-ui-related')).hide();
140 $('a', tab).addClass('active');
141 $('#' + $tab.attr('data-ui-related')).show();
144 console.log($.wiki.perspectives);
146 $.wiki.perspectives[$tab.attr('id')].onEnter();
152 $.wiki.Perspective = class Perspective {
153 constructor(options) {
154 this.doc = options.doc;
155 this.perspective_id = options.id || ''
159 return $.wiki.state.perspectives[this.perspective_id];
163 return this.perspective_id;
171 // called when perspective in initialized
172 if (!this.noupdate_hash_onenter) {
173 document.location.hash = '#' + this.perspective_id;
178 // called when user switches to another perspective
179 if (!this.noupdate_hash_onenter) {
180 document.location.hash = '';
190 * Stub rendering (used in generating history)
192 $.wiki.renderStub = function(params)
194 params = $.extend({ 'filters': {} }, params);
195 var $elem = params.stub.clone();
196 $elem.removeClass('row-stub');
197 params.container.append($elem);
199 $('*[data-stub-value]', $elem).each(function() {
201 var field = $this.attr('data-stub-value');
203 var value = params.data[field];
205 if(params.filters[field])
206 value = params.filters[field](value);
208 if(value === null || value === undefined) return;
210 if(!$this.attr('data-stub-target')) {
214 $this.attr($this.attr('data-stub-target'), value);
215 $this.removeAttr('data-stub-target');
216 $this.removeAttr('data-stub-value');
227 class GenericDialog {
228 constructor(element) {
233 self.$elem = $(element);
235 if(!self.$elem.attr('data-ui-initialized')) {
236 console.log("Initializing dialog", this);
238 self.$elem.attr('data-ui-initialized', true);
245 * Steps to follow when the dialog in first loaded on page.
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.
279 'overflow-y': 'scroll'
297 $("*[data-ui-error-for]", this.$elem).text('');
300 reportErrors(errors) {
301 var global = $("*[data-ui-error-for='__all__']", this.$elem);
304 $("*[data-ui-error-for]", this.$elem).text('');
305 for (var field_name in errors)
307 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
310 unassigned.push(field_name);
314 span.text(errors[field_name].join(' '));
317 if(unassigned.length > 0)
318 global.text( global.text() + 'W formularzu wystąpiły błędy');
322 $.wiki.cls.GenericDialog = GenericDialog;
324 $.wiki.showDialog = function(selector, options) {
325 var elem = $(selector);
327 if(elem.length != 1) {
328 console.log("Failed to show dialog:", selector, elem);
333 var klass = elem.attr('data-ui-jsclass');
334 return new $.wiki.cls[klass](elem, options);
336 console.log("Failed to show dialog", selector, klass, e);
341 window.addEventListener("message", (event) => {
344 $.ajax("/editor/editor-user-area/", {
345 success: function(d) {
346 $("#user-area")[0].innerHTML = d;
351 $("#login").click(function (e) {
355 let x = window.screenX + (window.innerWidth - w) / 2;
356 let y = window.screenY + (window.innerHeight - h) / 2;
358 "/accounts/login/?next=/editor/back",
360 "width=" + w + " height=" + h + " top=" + y + " left=" + x