1 /*globals Editor fileId SplitView PanelContainerView EditorView*/
2 var documentsUrl = '/api/documents/';
5 Editor.Model = Editor.Object.extend({
11 Editor.ToolbarButtonsModel = Editor.Model.extend({
12 _className: 'Editor.ToolbarButtonsModel',
13 serverURL: '/api/toolbar/buttons',
21 if (!this.get('buttons').length) {
25 success: this.loadSucceeded.bind(this)
30 loadSucceeded: function(data) {
31 this.set('buttons', data);
38 // empty -> loading -> synced -> unsynced -> loading
40 // -> dirty -> updating -> updated -> synced
42 Editor.XMLModel = Editor.Model.extend({
43 _className: 'Editor.XMLModel',
48 init: function(serverURL) {
50 this.set('state', 'empty');
51 this.serverURL = serverURL;
52 this.toolbarButtonsModel = new Editor.ToolbarButtonsModel();
53 this.addObserver(this, 'data', this.dataChanged.bind(this));
57 if (this.get('state') == 'empty') {
58 this.set('state', 'loading');
62 success: this.loadingSucceeded.bind(this)
69 update: function(message) {
70 if (this.get('state') == 'dirty') {
71 this.set('state', 'updating');
74 contents: this.get('data')
77 payload.message = message;
85 success: this.updatingSucceeded.bind(this),
86 error: this.updatingFailed.bind(this)
93 updatingSucceeded: function() {
94 if (this.get('state') != 'updating') {
95 alert('erroneous state:', this.get('state'));
97 this.set('state', 'updated');
100 updatingFailed: function() {
101 if (this.get('state') != 'updating') {
102 alert('erroneous state:', this.get('state'));
104 this.set('state', 'dirty');
108 set: function(property, value) {
109 if (property == 'state') {
110 console.log(this.description(), ':', property, '=', value);
112 return this._super(property, value);
115 dataChanged: function(property, value) {
116 if (this.get('state') == 'synced') {
117 this.set('state', 'dirty');
121 loadingSucceeded: function(data) {
122 if (this.get('state') != 'loading') {
123 alert('erroneous state:', this.get('state'));
125 this.set('data', data);
126 this.set('state', 'synced');
129 dispose: function() {
130 this.removeObserver(this);
136 Editor.HTMLModel = Editor.Model.extend({
137 _className: 'Editor.HTMLModel',
142 init: function(serverURL) {
144 this.set('state', 'empty');
145 this.serverURL = serverURL;
149 if (this.get('state') == 'empty') {
150 this.set('state', 'loading');
154 success: this.loadingSucceeded.bind(this)
159 loadingSucceeded: function(data) {
160 if (this.get('state') != 'loading') {
161 alert('erroneous state:', this.get('state'));
163 this.set('data', data);
164 this.set('state', 'synced');
168 set: function(property, value) {
169 if (property == 'state') {
170 console.log(this.description(), ':', property, '=', value);
172 return this._super(property, value);
177 Editor.ImageGalleryModel = Editor.Model.extend({
178 _className: 'Editor.ImageGalleryModel',
182 init: function(serverURL) {
184 this.set('state', 'empty');
185 this.serverURL = serverURL;
191 if (this.get('state') == 'empty') {
192 this.set('state', 'loading');
196 success: this.loadingSucceeded.bind(this)
201 loadingSucceeded: function(data) {
202 if (this.get('state') != 'loading') {
203 alert('erroneous state:', this.get('state'));
206 this.set('pages', data.pages)
207 this.set('state', 'synced');
210 set: function(property, value) {
211 if (property == 'state') {
212 console.log(this.description(), ':', property, '=', value);
214 return this._super(property, value);
219 Editor.DocumentModel = Editor.Model.extend({
220 _className: 'Editor.DocumentModel',
221 data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size
227 this.set('state', 'empty');
232 if (this.get('state') == 'empty') {
233 this.set('state', 'loading');
236 url: documentsUrl + fileId,
238 success: this.successfulLoad.bind(this)
243 successfulLoad: function(data) {
244 this.set('data', data);
245 this.set('state', 'synced');
246 this.contentModels = {
247 'xml': new Editor.XMLModel(data.text_url),
248 'html': new Editor.HTMLModel(data.html_url),
249 'gallery': new Editor.ImageGalleryModel(data.gallery_url)
251 for (var key in this.contentModels) {
252 this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
256 contentModelStateChanged: function(property, value, contentModel) {
257 if (value == 'dirty') {
258 this.set('state', 'dirty');
259 for (var key in this.contentModels) {
260 if (this.contentModels[key].guid() != contentModel.guid()) {
261 this.contentModels[key].set('state', 'unsynced');
264 } else if (value == 'updated') {
265 this.set('state', 'synced');
266 for (key in this.contentModels) {
267 if (this.contentModels[key].guid() == contentModel.guid()) {
268 this.contentModels[key].set('state', 'synced');
269 } else if (this.contentModels[key].get('state') == 'unsynced') {
270 this.contentModels[key].set('state', 'empty');
276 saveDirtyContentModel: function(message) {
277 for (var key in this.contentModels) {
278 if (this.contentModels[key].get('state') == 'dirty') {
279 this.contentModels[key].update(message);
294 set: function(property, value) {
295 if (property == 'state') {
296 console.log(this.description(), ':', property, '=', value);
298 return this._super(property, value);
303 var leftPanelView, rightPanelContainer, doc;
306 doc = new Editor.DocumentModel();
307 var editor = new EditorView('#body-wrap', doc);
309 var splitView = new SplitView('#splitview', doc);
310 leftPanelView = new PanelContainerView('#left-panel-container', doc);
311 rightPanelContainer = new PanelContainerView('#right-panel-container', doc);