Refactor kodu paneli. Wysyłanie danych do save().
[redakcja.git] / project / static / js / editor.js
1 function Panel(panelWrap) {
2         var self = this;
3         self.wrap = panelWrap;
4         self.contentDiv = $('.panel-content', panelWrap);
5         self.instanceId = Math.ceil(Math.random() * 1000000000);
6         $.log('new panel - wrap: ', self.wrap);
7
8         
9         $(document).bind('panel:unload.' + self.instanceId, 
10                         function(event, data) { self.unload(event, data); });   
11
12         $(document).bind('panel:contentChanged', function(event, data) {
13                 $.log(self, ' got changed event from: ', data);
14                 if(self != data) 
15                         self.otherPanelChanged(event.target);
16                 else 
17                         self.changed();
18
19                 return false;           
20         });
21
22 }
23
24 Panel.prototype.callHook = function(hookName) {
25         if(this.hooks && this.hooks[hookName])
26         {       
27 //              arguments.shift();
28                 $.log('calling hook: ', hookName, 'with args: ', arguments);
29                 this.hooks[hookName].apply(this, arguments);
30         }
31 }
32
33 Panel.prototype.load = function (url) {
34     $.log('preparing xhr load: ', this.wrap);
35     $(document).trigger('panel:unload', this);
36         var self = this;
37
38     $.ajax({
39         url: url,
40         dataType: 'html',
41                 success: function(data, tstat) {
42                         panel_hooks = null;
43                         $(self.contentDiv).html(data);
44                         self.hooks = panel_hooks;                       
45                         panel_hooks = null;
46                         self.callHook('load');
47                 },
48         error: function(request, textStatus, errorThrown) {
49             $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
50         }
51     });
52 }
53
54 Panel.prototype.unload = function(event, data) {
55         $.log('got unload signal', this, ' target: ', data);
56
57         if( data == this ) {
58                 $.log('unloading', this);
59                 $(document).unbind('panel:unload.' + this.instanceId);
60         $(this.contentDiv).html('');
61                 this.callHook('unload');
62                 this.hooks = null; // flush the hooks
63                 return false;
64     };
65 }
66
67 Panel.prototype.otherPanelChanged = function(other) {
68         $.log('panel ', other, ' changed.');
69         $('.change-notification', this.wrap).fadeIn();
70         this.callHook('dirty');
71 }       
72
73 Panel.prototype.changed = function () {
74         this.wrap.addClass('changed');
75 }
76
77 Panel.prototype.saveInfo = function() {
78         var saveInfo = {};
79         this.callHook('saveInfo', saveInfo);
80         return saveInfo;
81 }
82
83
84 function Editor() {
85         // editor initialization
86         // this is probably a good place to set config
87 }
88
89 Editor.prototype.setupUI = function() {
90         // set up the UI visually and attach callbacks
91         var self = this;
92         var panelRoot = $('#panels');
93
94         panelRoot.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
95     panelRoot.css('top', ($('#header').outerHeight() ) + 'px');
96
97         $('#panels > *.panel-wrap').each(function() {
98                 var panelWrap = $(this);
99                 $.log('wrap: ', panelWrap);
100                 panelWrap.data('ctrl', new Panel(panelWrap)); // attach controllers to wraps
101
102             $('.panel-toolbar select', panelWrap).change(function() {
103                         panelWrap.data('ctrl').load( $(this).val() );
104             });
105         });     
106
107         $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
108
109
110 }
111
112 Editor.prototype.loadConfig = function() {
113         // load options from cookie 
114 }
115
116 Editor.prototype.saveToBranch = function() {
117         var changed_panel = $('.panel-wrap.changed');
118         $.log('Saving to local branch - panel:', changed_panel);
119
120         if( changed_panel.length == 0) {
121                 $.log('Nothing to save.');
122                 return; /* no changes */
123         }
124
125         if( changed_panel.length > 1) {
126                 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
127                 return;
128         }
129
130         saveInfo = changed_panel.data('ctrl').saveInfo();
131
132         $.ajax({
133                 url: location.href + (saveInfo.part || ''),
134                 dataType: (saveInfo.dataType || 'text'),
135                 success: function(data, textStatus) {
136                         $.log('Success:', data);
137                 },
138                 error: function(rq, tstat, err) {
139                         $.log('save error', rq, tstat, err);
140                 },
141                 type: 'POST',
142                 data: (saveInfo.content || '')
143         });
144 };
145
146 $(function() {
147         editor = new Editor();
148
149         // do the layout
150         editor.loadConfig();
151         editor.setupUI();
152 });