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