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 var populate = function($this) {
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');
218 if ($elem.attr('data-stub-value')) populate($elem);
219 $('*[data-stub-value]', $elem).each(function() {populate($(this))});
228 class GenericDialog {
229 constructor(element) {
234 self.$elem = $(element);
236 if(!self.$elem.attr('data-ui-initialized')) {
237 console.log("Initializing dialog", this);
239 self.$elem.attr('data-ui-initialized', true);
246 * Steps to follow when the dialog in first loaded on page.
252 $('button[data-ui-action]', self.$elem).click(function(event) {
253 event.preventDefault();
255 var action = $(this).attr('data-ui-action');
256 console.log("Button pressed, action: ", action);
259 self[action + "Action"].call(self);
261 console.log("Action failed:", e);
262 // always hide on cancel
263 if(action == 'cancel')
270 * Prepare dialog for user. Clear any unnessary data.
280 'overflow-y': 'scroll'
298 $("*[data-ui-error-for]", this.$elem).text('');
301 reportErrors(errors) {
302 var global = $("*[data-ui-error-for='__all__']", this.$elem);
305 $("*[data-ui-error-for]", this.$elem).text('');
306 for (var field_name in errors)
308 var span = $("*[data-ui-error-for='"+field_name+"']", this.$elem);
311 unassigned.push(errors[field_name]);
315 span.text(errors[field_name].join(' '));
318 if(unassigned.length > 0)
320 global.text() + 'Wystąpił błąd: ' + unassigned.join(', '));
324 $.wiki.cls.GenericDialog = GenericDialog;
326 $.wiki.showDialog = function(selector, options) {
327 var elem = $(selector);
329 if(elem.length != 1) {
330 console.log("Failed to show dialog:", selector, elem);
335 var klass = elem.attr('data-ui-jsclass');
336 return new $.wiki.cls[klass](elem, options);
338 console.log("Failed to show dialog", selector, klass, e);
343 window.addEventListener("message", (event) => {
346 $.ajax("/editor/editor-user-area/", {
347 success: function(d) {
348 $("#user-area")[0].innerHTML = d;
349 $('#history-view-editor').toggleClass('can-approve', $('#user-area #pubmark_dialog').length > 0);
354 $("#login").click(function (e) {
358 let x = window.screenX + (window.innerWidth - w) / 2;
359 let y = window.screenY + (window.innerHeight - h) / 2;
361 "/accounts/login/?next=/editor/back",
363 "width=" + w + " height=" + h + " top=" + y + " left=" + x