X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/e823ad27e90d5cba05fb2ea8ac2a0d258be2f707..97e1a72c211205e4596accbfc86cf5eeaec487ee:/project/static/js/app.js?ds=sidebyside diff --git a/project/static/js/app.js b/project/static/js/app.js index 6e6f0f80..fde15393 100644 --- a/project/static/js/app.js +++ b/project/static/js/app.js @@ -3,6 +3,14 @@ var editor; var panel_hooks; +// prevent a console.log from blowing things up if we are on a browser that +// does not support it +if (typeof console === 'undefined') { + window.console = {} ; + console.log = console.info = console.warn = console.error = function(){}; +} + + (function(){ // Classes var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/; @@ -41,20 +49,21 @@ var panel_hooks; this.render_template = function render_template(str, data){ // Figure out if we're getting a template, or if we need to - // load the template - and be sure to cache the result. + // load the template - and be sure to cache the result. var fn = !/^[\d\s-_]/.test(str) ? cache[str] = cache[str] || render_template(document.getElementById(str).innerHTML) : // Generate a reusable function that will serve as a template // generator (and which will be cached). + new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};" + // Introduce the data as local variables using with(){} "with(obj){p.push('" + - // Convert the template into pure JavaScript + // Convert the template into pure JavaScript str .replace(/[\r\t\n]/g, " ") .split("<%").join("\t") @@ -70,6 +79,7 @@ var panel_hooks; }; })(); + (function() { var slice = Array.prototype.slice; @@ -97,189 +107,79 @@ var panel_hooks; } })(); - -var panels = []; -var documentsUrl = '/api/documents/'; +var Editor = Editor || {}; -var Model = Class.extend({ - observers: {}, +// Obiekt implementujący wzorzec KVC/KVO +Editor.Object = Class.extend({ + _className: 'Editor.Object', + _observers: {}, + _guid: null, - init: function() {}, + init: function() { + this._observers = {}; + }, - signal: function(event, data) { - console.log('signal', this, event, data); - if (this.observers[event]) { - for (key in this.observers[event]) { - this.observers[event][key](event, data); - } - }; - return this; + description: function() { + return this._className + '(guid = ' + this.guid() + ')'; }, - addObserver: function(observer, event, callback) { - if (!this.observers[event]) { - this.observers[event] = {}; + addObserver: function(observer, property, callback) { + // console.log('Add observer', observer.description(), 'to', this.description(), '[', property, ']'); + if (!this._observers[property]) { + this._observers[property] = {} } - this.observers[event][observer.id] = callback; + this._observers[property][observer.guid()] = callback; return this; }, - removeObserver: function(observer, event) { - if (!event) { - for (e in this.observers) { - this.removeObserver(observer, e); + removeObserver: function(observer, property) { + if (!property) { + for (var property in this._observers) { + this.removeObserver(observer, property) } } else { - delete this.observers[event][observer.id]; + // console.log('Remove observer', observer.description(), 'from', this.description(), '[', property, ']'); + delete this._observers[property][observer.guid()]; } return this; - } -}); - - -var XMLModel = Model.extend({ - parent: null, - data: '', - serverURL: null, - needsReload: false, - - init: function(parent, serverURL) { - this.parent = parent; - this.serverURL = serverURL; }, - getData: function() { - if (!this.data) { - this.reload(); + notifyObservers: function(property) { + var currentValue = this[property]; + for (var guid in this._observers[property]) { + // console.log(this._observers[property][guid]); + // console.log('Notifying', guid, 'of', this.description(), '[', property, ']'); + this._observers[property][guid](property, currentValue, this); } - return this.data; - }, - - setData: function(data) { - this.data = data; - this.dataChanged(); - }, - - reload: function() { - $.ajax({ - url: this.serverURL, - dataType: 'text', - success: this.reloadSucceeded.bind(this) - }); - }, - - reloadSucceeded: function(data) { - this.data = data; - this.signal('reloaded'); - }, - - dataChanged: function() { - this.parent.modelChanged('xml'); - this.signal('dataChanged'); - }, - - needsReload: function() { - this.needsReload = true; - this.signal('needsReload'); - } -}) - - - -var HTMLModel = Model.extend({ - parent: null, - data: '', - serverURL: null, - needsReload: false, - - init: function(parent, serverURL) { - this.parent = parent; - this.serverURL = serverURL; - }, - - getData: function() { - if (!this.data) { - this.reload(); - } - return this.data; + return this; }, - setData: function(data) { - console.log('setData'); - if (this.data != data) { - this.data = data; - this.dataChanged(); + guid: function() { + if (!this._guid) { + this._guid = ('editor-' + Editor.Object._lastGuid++); } + return this._guid; }, - reload: function() { - $.ajax({ - url: this.serverURL, - dataType: 'text', - success: this.reloadSucceeded.bind(this) - }); + get: function(property) { + return this[property]; }, - reloadSucceeded: function(data) { - this.data = data; - this.signal('reloaded'); - }, - - dataChanged: function() { - this.parent.modelChanged('html'); - }, - - needsReload: function() { - this.needsReload = true; - this.signal('needsReload'); - } -}) - - -var DocumentModel = Model.extend({ - data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size - xml: null, - html: null, - contentModels: {}, - - init: function() { - this.getData(); - }, - - getData: function() { - console.log('DocumentModel#getData'); - $.ajax({ - cache: false, - url: documentsUrl + fileId, - dataType: 'json', - success: this.successfulGetData.bind(this) - }); - }, - - successfulGetData: function(data) { - console.log('DocumentModel#successfulGetData:', data); - this.data = data; - this.contentModels = { - 'xml': new XMLModel(this, data.text_url) - }; + set: function(property, value) { + if (this[property] != value) { + this[property] = value; + this.notifyObservers(property); + } + return this; }, - modelChanged: function(contentModelName) { - for (modelName in this.contentModels) { - if (!(modelName == contentModelName)) { - this.contentModels[modelName].needsReload(); - } - } + dispose: function() { + delete this._observers; } }); -var leftPanelView, rightPanelContainer, doc; +Editor.Object._lastGuid = 0; -$(function() { - doc = new DocumentModel(); - var splitView = new SplitView('#splitview', doc); - leftPanelView = new PanelContainerView('#left-panel-container', doc); - rightPanelContainer = new PanelContainerView('#right-panel-container', doc); -}); +var panels = [];