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');
 
 107   set: function(property, value) {
 
 108     if (property == 'state') {
 
 109       console.log(this.description(), ':', property, '=', value);
 
 111     return this._super(property, value);
 
 114   dataChanged: function(property, value) {
 
 115     if (this.get('state') == 'synced') {
 
 116       this.set('state', 'dirty');
 
 120   loadingSucceeded: function(data) {
 
 121     if (this.get('state') != 'loading') {
 
 122       alert('erroneous state:', this.get('state'));
 
 124     this.set('data', data);
 
 125     this.set('state', 'synced');
 
 128   dispose: function() {
 
 129     this.removeObserver(this);
 
 135 Editor.HTMLModel = Editor.Model.extend({
 
 136   _className: 'Editor.HTMLModel',
 
 141   init: function(serverURL) {
 
 143     this.set('state', 'empty');
 
 144     this.serverURL = serverURL;
 
 148     if (this.get('state') == 'empty') {
 
 149       this.set('state', 'loading');
 
 153         success: this.loadingSucceeded.bind(this)
 
 158   loadingSucceeded: function(data) {
 
 159     if (this.get('state') != 'loading') {
 
 160       alert('erroneous state:', this.get('state'));
 
 162     this.set('data', data);
 
 163     this.set('state', 'synced');
 
 166   set: function(property, value) {
 
 167     if (property == 'state') {
 
 168       console.log(this.description(), ':', property, '=', value);
 
 170     return this._super(property, value);
 
 175 Editor.DocumentModel = Editor.Model.extend({
 
 176   _className: 'Editor.DocumentModel',
 
 177   data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size
 
 183     this.set('state', 'empty');
 
 188     if (this.get('state') == 'empty') {
 
 189       this.set('state', 'loading');
 
 192         url: documentsUrl + fileId,
 
 194         success: this.successfulLoad.bind(this)
 
 199   successfulLoad: function(data) {
 
 200     this.set('data', data);
 
 201     this.set('state', 'synced');
 
 202     this.contentModels = {
 
 203       'xml': new Editor.XMLModel(data.text_url),
 
 204       'html': new Editor.HTMLModel(data.html_url)
 
 206     for (var key in this.contentModels) {
 
 207       this.contentModels[key].addObserver(this, 'state', this.contentModelStateChanged.bind(this));
 
 211   contentModelStateChanged: function(property, value, contentModel) {
 
 212     if (value == 'dirty') {
 
 213       for (var key in this.contentModels) {
 
 214         if (this.contentModels[key].guid() != contentModel.guid()) {
 
 215           // console.log(this.contentModels[key].description(), 'frozen');
 
 216           this.contentModels[key].set('state', 'unsynced');
 
 222   quickSave: function(message) {
 
 223     for (var key in this.contentModels) {
 
 224       if (this.contentModels[key].get('state') == 'dirty') {
 
 225         this.contentModels[key].update(message);
 
 233 var leftPanelView, rightPanelContainer, doc;
 
 236   doc = new Editor.DocumentModel();
 
 237   var editor = new EditorView('#body-wrap', doc);
 
 239   var splitView = new SplitView('#splitview', doc);
 
 240   leftPanelView = new PanelContainerView('#left-panel-container', doc);
 
 241   rightPanelContainer = new PanelContainerView('#right-panel-container', doc);