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(hookName) {
23 if(this.hooks && this.hooks[hookName])
26 $.log('calling hook: ', hookName, 'with args: ', arguments);
27 this.hooks[hookName].apply(this, arguments);
31 Panel.prototype.load = function (url) {
32 $.log('preparing xhr load: ', this.wrap);
33 $(document).trigger('panel:unload', this);
39 success: function(data, tstat) {
41 $(self.contentDiv).html(data);
42 self.hooks = panel_hooks;
44 self.callHook('load');
46 error: function(request, textStatus, errorThrown) {
47 $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
52 Panel.prototype.unload = function(event, data) {
53 $.log('got unload signal', this, ' target: ', data);
56 $.log('unloading', this);
57 $(this.contentDiv).html('');
58 this.callHook('unload');
59 this.hooks = null; // flush the hooks
64 Panel.prototype.otherPanelChanged = function(other) {
65 $.log('panel ', other, ' changed.');
66 $('.change-notification', this.wrap).fadeIn();
67 this.callHook('dirty');
70 Panel.prototype.changed = function () {
71 this.wrap.addClass('changed');
74 Panel.prototype.saveInfo = function() {
76 this.callHook('saveInfo', saveInfo);
82 // editor initialization
83 // this is probably a good place to set config
86 Editor.prototype.setupUI = function() {
87 // set up the UI visually and attach callbacks
89 var panelRoot = $('#panels');
91 panelRoot.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
92 panelRoot.css('top', ($('#header').outerHeight() ) + 'px');
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
99 $('.panel-toolbar select', panelWrap).change(function() {
100 panelWrap.data('ctrl').load( $(this).val() );
104 $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
109 Editor.prototype.loadConfig = function() {
110 // load options from cookie
113 Editor.prototype.saveToBranch = function() {
114 var changed_panel = $('.panel-wrap.changed');
115 $.log('Saving to local branch - panel:', changed_panel);
117 if( changed_panel.length == 0) {
118 $.log('Nothing to save.');
119 return; /* no changes */
122 if( changed_panel.length > 1) {
123 alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
127 saveInfo = changed_panel.data('ctrl').saveInfo();
130 url: location.href + (saveInfo.part || ''),
131 dataType: (saveInfo.dataType || 'text'),
132 success: function(data, textStatus) {
133 $.log('Success:', data);
135 error: function(rq, tstat, err) {
136 $.log('save error', rq, tstat, err);
139 data: (saveInfo.content || '')
144 editor = new Editor();