X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/d3c74552a8f8509ff85cf956cf2b750d8387b683..6d29b81537b9a6b32bd5415d0c4ff3323faa51a0:/project/static/js/app.js?ds=inline diff --git a/project/static/js/app.js b/project/static/js/app.js index 73ce1076..7586f3fb 100644 --- a/project/static/js/app.js +++ b/project/static/js/app.js @@ -100,8 +100,134 @@ var panel_hooks; var panels = []; +var documentsUrl = '/api/documents/'; + + +var Model = Class.extend({ + observers: {}, + + init: function() {}, + + signal: function(event, data) { + console.log('signal', this, event, data); + if (this.observers[event]) { + for (observer in this.observers[event]) { + observer.handle(event, data); + } + }; + return this; + }, + + addObserver: function(observer, event) { + if (!this.observers[event]) { + this.observers[event] = []; + } + this.observers[event][observer] = observer; + return this; + }, + + removeObserver: function(observer, event) { + if (!event) { + for (e in this.observers) { + this.removeObserver(observer, e); + } + } else { + delete this.observers[event][observer]; + } + return this; + } +}); + + +var XMLModel = Model.extend({ + parent: null, + data: null, + serverURL: null, + + init: function(parent, serverURL) { + this.parent = parent; + this.serverURL = serverURL; + }, + + reload: function() { + $.ajax({ + url: this.serverURL, + dataType: 'text', + success: this.reloadSucceeded.bind(this) + }); + }, + + reloadSucceeded: function(data) { + this.data = data; + this.signal('reloaded'); + }, +}) + +var DocumentModel = Model.extend({ + data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size + xml: null, + html: null, + + init: function() {}, + + getData: function(callback) { + console.log('get:', documentsUrl + fileId); + $.ajax({ + cache: false, + url: documentsUrl + fileId, + dataType: 'json', + success: this.successfulGetData.bind(this, callback) + }); + }, + + successfulGetData: function(callback, data) { + this.data = data; + this.signal('changed'); + if (callback) { + (callback.bind(this))(data); + } + }, + + load: function(key) { + if (!this.data) { + this.getData(function() { this.load(key); }.bind(this)); + } else { + console.log('load', key, this.data[key + 'url']); + $.ajax({ + url: this.data[key + '_url'], + dataType: 'text', + success: this.loadSucceeded.bind(this, key) + }); + } + }, + + loadSucceeded: function(key, data) { + console.log('loadSucceeded', key, data); + this.set(key, data); + }, + + get: function(key) { + console.log(this[key]); + if (this[key]) { + return this[key] + } else { + this.load(key); + return ''; + } + }, + + set: function(key, value) { + this[key] = value; + this.signal(key + '-changed'); + return this; + } +}); + +var leftPanelView, rightPanelContainer, doc; + $(function() { - var splitView = new SplitView('#splitview'); - var leftPanelView = new PanelContainerView('#left-panel-container'); - var rightPanelContainer = new PanelContainerView('#right-panel-container'); + doc = new DocumentModel(); + var splitView = new SplitView('#splitview', doc); + leftPanelView = new PanelContainerView('#left-panel-container', doc); + rightPanelContainer = new PanelContainerView('#right-panel-container', doc); });