Dodanie KVC/KVO. Przeniesienie modeli do models.js.
[redakcja.git] / project / static / js / models.js
1 /*globals Editor fileId SplitView PanelContainerView*/
2 var documentsUrl = '/api/documents/';
3
4
5 Editor.Model = Editor.Object.extend({
6   synced: false,
7   data: null
8 });
9
10
11 Editor.XMLModel = Editor.Model.extend({
12   serverURL: null,
13   
14   init: function(serverURL) {
15     this.serverURL = serverURL;
16   },
17   
18   getData: function() {
19     if (!this.data) {
20       this.reload();
21     }
22     return this.data;
23   },
24   
25   setData: function(data) {
26     this.data = data;
27     this.dataChanged();
28   },
29   
30   load: function() {
31     if (!this.get('synced')) {
32       $.ajax({
33         url: this.serverURL,
34         dataType: 'text',
35         success: this.reloadSucceeded.bind(this)
36       });
37     }
38   },
39   
40   reloadSucceeded: function(data) {
41     this.set('data', data);
42     this.set('synced', true);
43   }
44 });
45
46
47 Editor.HTMLModel = Editor.Model.extend({
48   serverURL: null,
49   
50   init: function(serverURL) {
51     this.serverURL = serverURL;
52   },
53   
54   load: function() {
55     if (!this.get('synced')) {
56       $.ajax({
57         url: this.serverURL,
58         dataType: 'text',
59         success: this.reloadSucceeded.bind(this)
60       });
61     }
62   },
63   
64   reloadSucceeded: function(data) {
65     this.set('data', data);
66     this.set('synced', true);
67   }
68 });
69
70
71 Editor.DocumentModel = Editor.Model.extend({
72   data: null, // name, text_url, latest_rev, latest_shared_rev, parts_url, dc_url, size
73   contentModels: {},
74   
75   init: function() {
76     this.load();
77   },
78   
79   load: function() {
80     console.log('DocumentModel#load');
81     $.ajax({
82       cache: false,
83       url: documentsUrl + fileId,
84       dataType: 'json',
85       success: this.successfulLoad.bind(this)
86     });
87   },
88   
89   successfulLoad: function(data) {
90     console.log('DocumentModel#successfulLoad:', data);
91     this.set('data', data);
92     this.contentModels = {
93       'xml': new Editor.XMLModel(data.text_url),
94       'html': new Editor.HTMLModel(data.html_url)
95     };
96   }
97 });
98
99
100 var leftPanelView, rightPanelContainer, doc;
101
102 $(function() {
103   doc = new Editor.DocumentModel();
104   var splitView = new SplitView('#splitview', doc);
105   leftPanelView = new PanelContainerView('#left-panel-container', doc);
106   rightPanelContainer = new PanelContainerView('#right-panel-container', doc);
107 });