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, revision) {
50 this.set('state', 'empty');
51 this.set('revision', revision);
52 this.serverURL = serverURL;
53 this.toolbarButtonsModel = new Editor.ToolbarButtonsModel();
54 this.addObserver(this, 'data', this.dataChanged.bind(this));
58 if (this.get('state') == 'empty') {
59 this.set('state', 'loading');
63 data: {revision: this.get('revision')},
64 success: this.loadingSucceeded.bind(this)
71 update: function(message) {
72 if (this.get('state') == 'dirty') {
73 this.set('state', 'updating');
76 contents: this.get('data'),
77 revision: this.get('revision')
80 payload.message = message;
88 success: this.updatingSucceeded.bind(this),
89 error: this.updatingFailed.bind(this)
96 updatingSucceeded: function() {
97 if (this.get('state') != 'updating') {
98 alert('erroneous state:', this.get('state'));
100 this.set('state', 'updated');
103 updatingFailed: function() {
104 if (this.get('state') != 'updating') {
105 alert('erroneous state:', this.get('state'));
107 this.set('state', 'dirty');
111 set: function(property, value) {
112 if (property == 'state') {
113 console.log(this.description(), ':', property, '=', value);
115 return this._super(property, value);
118 dataChanged: function(property, value) {
119 if (this.get('state') == 'synced') {
120 this.set('state', 'dirty');
124 loadingSucceeded: function(data) {
125 if (this.get('state') != 'loading') {
126 alert('erroneous state:', this.get('state'));
128 this.set('data', data);
129 this.set('state', 'synced');
132 dispose: function() {
133 this.removeObserver(this);
139 Editor.HTMLModel = Editor.Model.extend({
140 _className: 'Editor.HTMLModel',
145 init: function(serverURL, revision) {
147 this.set('state', 'empty');
148 this.set('revision', revision);
149 this.serverURL = serverURL;
153 if (this.get('state') == 'empty') {
154 this.set('state', 'loading');
158 data: {revision: this.get('revision')},
159 success: this.loadingSucceeded.bind(this)
164 loadingSucceeded: function(data) {
165 if (this.get('state') != 'loading') {
166 alert('erroneous state:', this.get('state'));
168 this.set('data', data);
169 this.set('state', 'synced');
173 set: function(property, value) {
174 if (property == 'state') {
175 console.log(this.description(), ':', property, '=', value);
177 return this._super(property, value);
182 Editor.ImageGalleryModel = Editor.Model.extend({
183 _className: 'Editor.ImageGalleryModel',
187 init: function(serverURL) {
189 this.set('state', 'empty');
190 this.serverURL = serverURL;
196 if (this.get('state') == 'empty') {
197 this.set('state', 'loading');
201 success: this.loadingSucceeded.bind(this)
206 loadingSucceeded: function(data) {
207 if (this.get('state') != 'loading') {
208 alert('erroneous state:', this.get('state'));
211 this.set('pages', data[0].pages);
212 this.set('state', 'synced');
215 set: function(property, value) {
216 if (property == 'state') {
217 console.log(this.description(), ':', property, '=', value);
219 return this._super(property, value);
224 Editor.DocumentModel = Editor.Model.extend({
225 _className: 'Editor.DocumentModel',
226 data: null, // name, text_url, user_revision, latest_shared_rev, parts_url, dc_url, size, merge_url
232 this.set('state', 'empty');
237 if (this.get('state') == 'empty') {
238 this.set('state', 'loading');
241 url: documentsUrl + fileId,
243 success: this.successfulLoad.bind(this)
248 successfulLoad: function(data) {
249 this.set('data', data);
250 this.set('state', 'synced');
251 this.contentModels = {
252 'xml': new Editor.XMLModel(data.text_url, data.user_revision),
253 'html': new Editor.HTMLModel(data.html_url, data.user_revision),
254 'gallery': new Editor.ImageGalleryModel(data.gallery_url)
256 for (var key in this.contentModels) {
257 this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
261 contentModelStateChanged: function(property, value, contentModel) {
262 if (value == 'dirty') {
263 this.set('state', 'dirty');
264 for (var key in this.contentModels) {
265 if (this.contentModels[key].guid() != contentModel.guid()) {
266 this.contentModels[key].set('state', 'unsynced');
269 } else if (value == 'updated') {
270 this.set('state', 'synced');
271 for (key in this.contentModels) {
272 if (this.contentModels[key].guid() == contentModel.guid()) {
273 this.contentModels[key].set('state', 'synced');
274 } else if (this.contentModels[key].get('state') == 'unsynced') {
275 this.contentModels[key].set('state', 'empty');
281 saveDirtyContentModel: function(message) {
282 for (var key in this.contentModels) {
283 if (this.contentModels[key].get('state') == 'dirty') {
284 this.contentModels[key].update(message);
291 this.set('state', 'loading');
293 url: this.data.merge_url,
298 target_revision: this.data.user_revision
300 complete: this.updateCompleted.bind(this),
301 success: function(data) { this.set('updateData', data); }.bind(this)
305 updateCompleted: function(xhr, textStatus) {
306 console.log(xhr.status, textStatus);
307 if (xhr.status == 200) { // Sukces
308 this.data.user_revision = this.get('updateData').revision;
309 for (var key in this.contentModels) {
310 this.contentModels[key].set('revision', this.data.user_revision);
311 this.contentModels[key].set('state', 'empty');
313 } else if (xhr.status == 202) { // Wygenerowano PullRequest
314 } else if (xhr.status == 204) { // Nic nie zmieniono
315 } else if (xhr.status == 409) { // Konflikt podczas operacji
317 this.set('state', 'synced');
318 this.set('updateData', null);
321 merge: function(message) {
322 this.set('state', 'loading');
324 url: this.data.merge_url,
329 target_revision: this.data.user_revision,
332 complete: this.mergeCompleted.bind(this),
333 success: function(data) { this.set('mergeData', data); }.bind(this)
337 mergeCompleted: function(xhr, textStatus) {
338 console.log(xhr.status, textStatus);
339 if (xhr.status == 200) { // Sukces
340 this.data.user_revision = this.get('mergeData').revision;
341 for (var key in this.contentModels) {
342 this.contentModels[key].set('revision', this.data.user_revision);
343 this.contentModels[key].set('state', 'empty');
345 } else if (xhr.status == 202) { // Wygenerowano PullRequest (tutaj?)
346 } else if (xhr.status == 204) { // Nic nie zmieniono
347 } else if (xhr.status == 409) { // Konflikt podczas operacji
349 this.set('state', 'synced');
350 this.set('mergeData', null);
354 set: function(property, value) {
355 if (property == 'state') {
356 console.log(this.description(), ':', property, '=', value);
358 return this._super(property, value);
363 var leftPanelView, rightPanelContainer, doc;
366 doc = new Editor.DocumentModel();
367 var editor = new EditorView('#body-wrap', doc);
369 var splitView = new SplitView('#splitview', doc);
370 leftPanelView = new PanelContainerView('#left-panel-container', doc);
371 rightPanelContainer = new PanelContainerView('#right-panel-container', doc);