1 function Panel(panelWrap) {
4 self.contentDiv = $('.panel-content', panelWrap);
5 self.instanceId = Math.ceil(Math.random() * 1000000000);
6 $.log('new panel - wrap: ', self.wrap);
8 $(document).bind('panel:unload.' + self.instanceId,
9 function(event, data) { self.unload(event, data); });
11 $(document).bind('panel:contentChanged', function(event, data) {
12 $.log(self, ' got changed event from: ', data);
14 self.otherPanelChanged(event.target);
22 Panel.prototype.callHook = function() {
23 args = $.makeArray(arguments)
24 var hookName = args.splice(0,1)[0]
25 var noHookAction = args.splice(0,1)[0]
28 $.log('calling hook: ', hookName, 'with args: ', args);
29 if(this.hooks && this.hooks[hookName])
30 result = this.hooks[hookName].apply(this, args);
31 else if (noHookAction instanceof Function)
32 result = noHookAction(args);
36 Panel.prototype.load = function (url) {
37 $.log('preparing xhr load: ', this.wrap);
38 $(document).trigger('panel:unload', this);
40 self.current_url = url;
45 success: function(data, tstat) {
47 $(self.contentDiv).html(data);
48 self.hooks = panel_hooks;
50 self.callHook('load');
52 error: function(request, textStatus, errorThrown) {
53 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
58 Panel.prototype.unload = function(event, data) {
59 $.log('got unload signal', this, ' target: ', data);
62 $.log('unloading', this);
63 $(this.contentDiv).html('');
64 this.callHook('unload');
65 this.hooks = null; // flush the hooks
70 Panel.prototype.refresh = function(event, data) {
73 $.log('hard reload for panel ', self.current_url);
74 self.load(self.current_url);
78 if( this.callHook('refresh', reload) )
79 $('.change-notification', this.wrap).fadeOut();
82 Panel.prototype.otherPanelChanged = function(other) {
83 $.log('panel ', other, ' changed.');
84 if(!this.callHook('dirty'))
85 $('.change-notification', this.wrap).fadeIn();
88 Panel.prototype.markChanged = function () {
89 this.wrap.addClass('changed');
92 Panel.prototype.changed = function () {
93 return this.wrap.hasClass('changed');
96 Panel.prototype.unmarkChanged = function () {
97 this.wrap.removeClass('changed');
100 Panel.prototype.saveInfo = function() {
102 this.callHook('saveInfo', null, saveInfo);
108 this.rootDiv = $('#panels');
111 Editor.prototype.setupUI = function() {
112 // set up the UI visually and attach callbacks
115 self.rootDiv.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
116 self.rootDiv.css('top', ($('#header').outerHeight() ) + 'px');
118 $('#panels > *.panel-wrap').each(function() {
119 var panelWrap = $(this);
120 $.log('wrap: ', panelWrap);
121 panel = new Panel(panelWrap);
122 panelWrap.data('ctrl', panel); // attach controllers to wraps
123 panel.load($('.panel-toolbar select', panelWrap).val());
125 $('.panel-toolbar select', panelWrap).change(function() {
126 var url = $(this).val();
127 panelWrap.data('ctrl').load(url);
128 self.savePanelOptions();
132 $(document).bind('panel:contentChanged', function() { self.onContentChanged.apply(self, arguments) });
134 $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
135 $('#toolbar-button-commit').click( function (event, data) { self.sendPullRequest(); } );
136 self.rootDiv.bind('stopResize', function() { self.savePanelOptions() });
139 Editor.prototype.loadConfig = function() {
140 // Load options from cookie
141 var defaultOptions = {
143 {name: 'htmleditor', ratio: 0.5},
144 {name: 'gallery', ratio: 0.5}
149 var cookie = $.cookie('options');
150 this.options = $.secureEvalJSON(cookie);
152 this.options = defaultOptions;
155 this.options = defaultOptions;
159 this.loadPanelOptions();
162 Editor.prototype.loadPanelOptions = function() {
166 $('.panel-wrap', self.rootDiv).each(function(index) {
167 var panelWidth = self.options.panels[index].ratio * self.rootDiv.width();
168 if ($(this).hasClass('last-panel')) {
178 totalWidth += panelWidth;
180 $.log('panel:', this, $(this).css('left'));
181 $('.panel-toolbar select', this).val(
182 $('.panel-toolbar option[name=' + self.options.panels[index].name + ']', this).attr('value')
187 Editor.prototype.savePanelOptions = function() {
190 $('.panel-wrap', self.rootDiv).not('.panel-content-overlay').each(function() {
192 name: $('.panel-toolbar option:selected', this).attr('name'),
193 ratio: $(this).width() / self.rootDiv.width()
196 self.options.panels = panels;
197 $.log($.toJSON(self.options));
198 $.cookie('options', $.toJSON(self.options), { expires: 7, path: '/'});
201 Editor.prototype.saveToBranch = function() {
202 var changed_panel = $('.panel-wrap.changed');
204 $.log('Saving to local branch - panel:', changed_panel);
206 if( changed_panel.length == 0) {
207 $.log('Nothing to save.');
208 return; /* no changes */
211 if( changed_panel.length > 1) {
212 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
216 saveInfo = changed_panel.data('ctrl').saveInfo();
221 success: function(data, textStatus) {
222 if (data.result != 'ok')
223 $.log('save errors: ', data.errors)
225 self.refreshPanels(changed_panel);
226 $('#toolbar-button-save').attr('disabled', 'disabled');
227 $('#toolbar-button-commit').removeAttr('disabled');
230 error: function(rq, tstat, err) {
231 $.log('save error', rq, tstat, err);
234 data: saveInfo.postData
238 Editor.prototype.onContentChanged = function(event, data) {
239 $('#toolbar-button-save').removeAttr('disabled');
240 $('#toolbar-button-commit').attr('disabled', 'disabled');
243 Editor.prototype.refreshPanels = function(goodPanel) {
245 var panels = $('#' + self.rootDiv.attr('id') +' > *.panel-wrap', self.rootDiv.parent());
247 panels.each(function() {
248 var panel = $(this).data('ctrl');
249 $.log('Refreshing: ', this, panel);
250 if ( panel.changed() )
251 panel.unmarkChanged();
258 Editor.prototype.sendPullRequest = function () {
259 if( $('.panel-wrap.changed').length != 0)
260 alert("There are unsaved changes - can't make a pull request.");
263 url: '/pull-request',
265 success: function(data, textStatus) {
266 $.log('data: ' + data);
268 error: function(rq, tstat, err) {
269 $.log('commit error', rq, tstat, err);
277 editor = new Editor();