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.DocumentModel = Editor.Model.extend({
178 _className: 'Editor.DocumentModel',
179 data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size
185 this.set('state', 'empty');
190 if (this.get('state') == 'empty') {
191 this.set('state', 'loading');
194 url: documentsUrl + fileId,
196 success: this.successfulLoad.bind(this)
201 successfulLoad: function(data) {
202 this.set('data', data);
203 this.set('state', 'synced');
204 this.contentModels = {
205 'xml': new Editor.XMLModel(data.text_url),
206 'html': new Editor.HTMLModel(data.html_url)
208 for (var key in this.contentModels) {
209 this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
213 contentModelStateChanged: function(property, value, contentModel) {
214 if (value == 'dirty') {
215 this.set('state', 'dirty');
216 for (var key in this.contentModels) {
217 if (this.contentModels[key].guid() != contentModel.guid()) {
218 this.contentModels[key].set('state', 'unsynced');
221 } else if (value == 'updated') {
222 this.set('state', 'synced');
223 for (key in this.contentModels) {
224 if (this.contentModels[key].guid() == contentModel.guid()) {
225 this.contentModels[key].set('state', 'synced');
226 } else if (this.contentModels[key].get('state') == 'unsynced') {
227 this.contentModels[key].set('state', 'empty');
233 quickSave: function(message) {
234 for (var key in this.contentModels) {
235 if (this.contentModels[key].get('state') == 'dirty') {
236 this.contentModels[key].update(message);
243 set: function(property, value) {
244 if (property == 'state') {
245 console.log(this.description(), ':', property, '=', value);
247 return this._super(property, value);
252 var leftPanelView, rightPanelContainer, doc;
255 doc = new Editor.DocumentModel();
256 var editor = new EditorView('#body-wrap', doc);
258 var splitView = new SplitView('#splitview', doc);
259 leftPanelView = new PanelContainerView('#left-panel-container', doc);
260 rightPanelContainer = new PanelContainerView('#right-panel-container', doc);