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 -> 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)
67 set: function(property, value) {
68 if (property == 'state') {
69 console.log(this.description(), ':', property, '=', value);
71 return this._super(property, value);
74 dataChanged: function(property, value) {
75 if (this.get('state') == 'synced') {
76 this.set('state', 'dirty');
80 loadingSucceeded: function(data) {
81 if (this.get('state') != 'loading') {
82 alert('erroneous state:', this.get('state'));
84 this.set('data', data);
85 this.set('state', 'synced');
89 this.removeObserver(this);
95 Editor.HTMLModel = Editor.Model.extend({
96 _className: 'Editor.HTMLModel',
101 init: function(serverURL) {
103 this.set('state', 'empty');
104 this.serverURL = serverURL;
108 if (this.get('state') == 'empty') {
109 this.set('state', 'loading');
113 success: this.loadingSucceeded.bind(this)
118 loadingSucceeded: function(data) {
119 if (this.get('state') != 'loading') {
120 alert('erroneous state:', this.get('state'));
122 this.set('data', data);
123 this.set('state', 'synced');
126 set: function(property, value) {
127 if (property == 'state') {
128 console.log(this.description(), ':', property, '=', value);
130 return this._super(property, value);
135 Editor.DocumentModel = Editor.Model.extend({
136 _className: 'Editor.DocumentModel',
137 data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size
143 this.set('state', 'empty');
148 if (this.get('state') == 'empty') {
149 this.set('state', 'loading');
152 url: documentsUrl + fileId,
154 success: this.successfulLoad.bind(this)
159 successfulLoad: function(data) {
160 this.set('data', data);
161 this.set('state', 'synced');
162 this.contentModels = {
163 'xml': new Editor.XMLModel(data.text_url),
164 'html': new Editor.HTMLModel(data.html_url)
166 for (var key in this.contentModels) {
167 this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
171 contentModelStateChanged: function(property, value, contentModel) {
172 if (value == 'dirty') {
173 for (var key in this.contentModels) {
174 if (this.contentModels[key].guid() != contentModel.guid()) {
175 // console.log(this.contentModels[key].description(), 'frozen');
176 this.contentModels[key].set('state', 'unsynced');
184 var leftPanelView, rightPanelContainer, doc;
187 doc = new Editor.DocumentModel();
188 var editor = new EditorView('#body-wrap', doc);
189 var splitView = new SplitView('#splitview', doc);
190 leftPanelView = new PanelContainerView('#left-panel-container', doc);
191 rightPanelContainer = new PanelContainerView('#right-panel-container', doc);