Refactor kodu paneli. Wysyłanie danych do save().
authorŁukasz Rekucki <lrekucki@gmail.com>
Wed, 26 Aug 2009 08:50:11 +0000 (10:50 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Wed, 26 Aug 2009 08:50:11 +0000 (10:50 +0200)
project/static/js/editor.js
project/templates/explorer/file_xml.html
project/templates/explorer/panels/dceditor.html
project/templates/explorer/panels/gallery.html
project/templates/explorer/panels/htmleditor.html
project/templates/explorer/panels/xmleditor.html

index 3cd59f0..4a1c9d1 100644 (file)
-$(function() 
-{
-       function saveToBranch(data) {
-               $.log('Saving to local branch');
-               var changed_panel = $('.panel-wrap.changed');
-
-               if( changed_panel.length == 0) 
-                       return; /* no changes */
-
-               if( changed_panel.length > 1)
-                       alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
-
-               save_data = changed_panel.data('ctrl').saveData();
-
-               $.ajax({
-                       url: location.href,
-                       dataType: 'json',
-                       success: function(data, textStatus) {
-                               $.log('Success:', data);
-                       },
-                       error: function(rq, tstat, err) {
-                               $.log('save error', rq, tstat, err);
-                       },
-                       type: 'POST',
-                       data: save_data
-               });
-       }
+function Panel(panelWrap) {
+       var self = this;
+       self.wrap = panelWrap;
+       self.contentDiv = $('.panel-content', panelWrap);
+       self.instanceId = Math.ceil(Math.random() * 1000000000);
+       $.log('new panel - wrap: ', self.wrap);
+
        
-       $('#toolbar-button-save').click(saveToBranch);
-});
+       $(document).bind('panel:unload.' + self.instanceId, 
+                       function(event, data) { self.unload(event, data); });   
+
+       $(document).bind('panel:contentChanged', function(event, data) {
+               $.log(self, ' got changed event from: ', data);
+               if(self != data) 
+                       self.otherPanelChanged(event.target);
+               else 
+                       self.changed();
+
+               return false;           
+       });
+
+}
+
+Panel.prototype.callHook = function(hookName) {
+       if(this.hooks && this.hooks[hookName])
+       {       
+//             arguments.shift();
+               $.log('calling hook: ', hookName, 'with args: ', arguments);
+               this.hooks[hookName].apply(this, arguments);
+       }
+}
+
+Panel.prototype.load = function (url) {
+    $.log('preparing xhr load: ', this.wrap);
+    $(document).trigger('panel:unload', this);
+       var self = this;
+
+    $.ajax({
+        url: url,
+        dataType: 'html',
+               success: function(data, tstat) {
+                       panel_hooks = null;
+                       $(self.contentDiv).html(data);
+                       self.hooks = panel_hooks;                       
+                       panel_hooks = null;
+                       self.callHook('load');
+               },
+        error: function(request, textStatus, errorThrown) {
+            $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
+        }
+    });
+}
+
+Panel.prototype.unload = function(event, data) {
+       $.log('got unload signal', this, ' target: ', data);
+
+       if( data == this ) {
+               $.log('unloading', this);
+               $(document).unbind('panel:unload.' + this.instanceId);
+        $(this.contentDiv).html('');
+               this.callHook('unload');
+               this.hooks = null; // flush the hooks
+               return false;
+    };
+}
+
+Panel.prototype.otherPanelChanged = function(other) {
+       $.log('panel ', other, ' changed.');
+       $('.change-notification', this.wrap).fadeIn();
+       this.callHook('dirty');
+}      
+
+Panel.prototype.changed = function () {
+       this.wrap.addClass('changed');
+}
+
+Panel.prototype.saveInfo = function() {
+       var saveInfo = {};
+       this.callHook('saveInfo', saveInfo);
+       return saveInfo;
+}
+
+
+function Editor() {
+       // editor initialization
+       // this is probably a good place to set config
+}
 
+Editor.prototype.setupUI = function() {
+       // set up the UI visually and attach callbacks
+       var self = this;
+       var panelRoot = $('#panels');
+
+       panelRoot.makeHorizPanel({}); // TODO: this probably doesn't belong into jQuery
+    panelRoot.css('top', ($('#header').outerHeight() ) + 'px');
+
+       $('#panels > *.panel-wrap').each(function() {
+               var panelWrap = $(this);
+               $.log('wrap: ', panelWrap);
+               panelWrap.data('ctrl', new Panel(panelWrap)); // attach controllers to wraps
+
+           $('.panel-toolbar select', panelWrap).change(function() {
+                       panelWrap.data('ctrl').load( $(this).val() );
+           });
+       });     
+
+       $('#toolbar-button-save').click( function (event, data) { self.saveToBranch(); } );
+
+
+}
+
+Editor.prototype.loadConfig = function() {
+       // load options from cookie 
+}
+
+Editor.prototype.saveToBranch = function() {
+       var changed_panel = $('.panel-wrap.changed');
+       $.log('Saving to local branch - panel:', changed_panel);
+
+       if( changed_panel.length == 0) {
+               $.log('Nothing to save.');
+               return; /* no changes */
+       }
+
+       if( changed_panel.length > 1) {
+               alert('Błąd: więcej niż jeden panel został zmodyfikowany. Nie można zapisać.');
+               return;
+       }
+
+       saveInfo = changed_panel.data('ctrl').saveInfo();
+
+       $.ajax({
+               url: location.href + (saveInfo.part || ''),
+               dataType: (saveInfo.dataType || 'text'),
+               success: function(data, textStatus) {
+                       $.log('Success:', data);
+               },
+               error: function(rq, tstat, err) {
+                       $.log('save error', rq, tstat, err);
+               },
+               type: 'POST',
+               data: (saveInfo.content || '')
+       });
+};
+
+$(function() {
+       editor = new Editor();
+
+       // do the layout
+       editor.loadConfig();
+       editor.setupUI();
+});
index fe7e0c0..baeb3d1 100644 (file)
@@ -26,7 +26,7 @@
                         <option value="{% url gallery_panel hash %}">Galeria skanów</option>
                         <option value="{% url dceditor_panel hash %}">Edytor DublinCore</option>
                     </select>
-                    <strong class="change-notification" style="display: none">Zmieniono!</strong>
+                    <strong class="change-notification" style="display: none">Widok nieaktualny!</strong>
                </div>
                <div id="left-panel-content" class="panel-content"></div>
                <button type="button" class="panel-slider" id="slider01">&nbsp;</button>
@@ -40,7 +40,7 @@
                         <option value="{% url gallery_panel hash %}">Galeria skanów</option>
                         <option value="{% url dceditor_panel hash %}">Edytor DublinCore</option>
                     </select>
-                    <strong class="change-notification" style="display: none">Zmieniono!</strong>
+                    <strong class="change-notification" style="display: none">Widok nieaktualny!</strong>
                  </div>
                  <div id="right-panel-content" class="panel-content"></div>
             </div>
index ee5146c..96c2c47 100644 (file)
@@ -5,17 +5,5 @@
     </form>
 </div>
 <script type="text/javascript" charset="utf-8">
-    (function() {
-        var id = Math.ceil(Math.random() * 1000000000);
-        
-        panel(function(event, me) {
-            // $(document).bind('panel:contentChanged.' + id, function(event, p) {
-            //     $('.change-notification', $(me).parent()).fadeIn();
-            // });
-            $(me).addClass('panel-htmleditor');
-        }, function(event, me) {
-            $(me).removeClass('panel-htmleditor');
-            // $(document).unbind('panel:contentChanged.' + id);
-        });
-    })()
+panel_hooks = { };
 </script>
index 6c44a19..d339bce 100644 (file)
 </div>
 <script type="text/javascript" charset="utf-8">
 
-    (function() {
-        function resizeEditor(event, panel) {
-            var panel = panel || event.data.panel;
-            $('.images-wrap', panel).height($(panel).height());
-        }
-
-        panel(function(event, panel) {
-            $('.id_folders', panel).change(function() {
-                $('.images', panel).load('{% url folder_image_ajax %}' + $('.id_folders', panel).val() + '/', function() {
-                    $('.images-wrap', panel).data('lazyload:lastCheckedScrollTop', -10000);
+panel_hooks = {
+       load: function() {
+               var contentDiv = this.contentDiv;
+               $('.id_folders', contentDiv).change(function() 
+               {
+               $('.images', contentDiv).load('{% url folder_image_ajax %}' + $('.id_folders', panel).val() + '/', 
+                               function() {
+                    $('.images-wrap', contentDiv).data('lazyload:lastCheckedScrollTop', -10000);
                 });
-            });
-            $('.images-wrap', panel).lazyload('.image-box', {threshold: 640 * 10, scrollTreshold: 640 * 5});
-            
-            $(window).bind('resize', {'panel': panel}, resizeEditor);
-            resizeEditor(null, panel);  
-        }, function(event, panel) {
-        });
-    })()
+               });
+               
+               $('.images-wrap', contentDiv).lazyload('.image-box', 
+                       {threshold: 640 * 10, scrollTreshold: 640 * 5}
+               );
+       }
+};
 </script>
index bd1bd50..d5e0acb 100644 (file)
@@ -1,20 +1,12 @@
 {{ html|safe }}
 <script type="text/javascript" charset="utf-8">
-(function() {
-
-var id = Math.ceil(Math.random() * 1000000000);
-
-load_callback = function(panel) {
-       $(document).bind('panel:contentChanged.' + id, function(event, p) {
-         $('.change-notification', $(this).parent()).fadeIn();
-       });
-    $(this).addClass('panel-htmleditor');
-}
-
-unload_callback = function(panel) {
-           $(me).removeClass('panel-htmleditor');
-            $(document).unbind('panel:contentChanged.' + id);
-}
-
-})();
+panel_hooks = {
+       load: function() 
+       {
+               this.contentDiv.addClass('panel-htmleditor');
+       },
+       unload: function() {
+               this.contentDiv.removeClass('panel-htmleditor');
+       }
+};
 </script>
index dfe9477..9186f04 100644 (file)
@@ -6,18 +6,22 @@
 </div>
 
 <script type="text/javascript" charset="utf-8">
-(function() {
 
-    function xmleditor_onload(event, panel) {
+panel_hooks = {
+       load: function () {
+               var self = this;
+               var panel = self.contentDiv;
+
         var textareaId = 'xmleditor-' + Math.ceil(Math.random() * 1000000000);
-        $('textarea', panel).attr('id', textareaId);
-        var editor = CodeMirror.fromTextArea(textareaId, {
+               $('textarea', panel).attr('id', textareaId);
+
+               var texteditor = CodeMirror.fromTextArea(textareaId, {
             parserfile: 'parsexml.js',
             path: "/static/js/codemirror/",
             stylesheet: "/static/css/xmlcolors.css",
             parserConfig: {useHTMLKludges: false},
             onChange: function() {
-                $(document).trigger('panel:contentChanged', panel);
+                               panel.trigger('panel:contentChanged', self);
             },
             initCallback: function(editor) {
                 // Toolbar
                 $('.toolbar-buttons li', panel).each(function() {
                     var tag = $(this).attr('p:tag');
                     var handler = function() {
-                        var text = editor.selection();
+                        var text = texteditor.selection();
                         editor.replaceSelection('<' + tag + '>' + text + '</' + tag + '>');
                         if (text.length == 0) {
-                            var pos = editor.cursorPosition();
-                            editor.selectLines(pos.line, pos.character + tag.length + 2);
+                            var pos = texteditor.cursorPosition();
+                            texteditor.selectLines(pos.line, pos.character + tag.length + 2);
                         }
                         $(document).trigger('panel:contentChanged', panel);
                     }
@@ -49,7 +53,7 @@
                     $(this).click(handler)
                 });
 
-                editor.grabKeys(function(event) { 
+                texteditor.grabKeys(function(event) { 
                     if (keys[event.keyCode]) {
                         keys[event.keyCode]();
                     }
@@ -57,7 +61,7 @@
             }
         })
         
-        $(editor.frame).css({width: '100%', height: '100%'});
+        $(texteditor.frame).css({width: '100%', height: '100%'});
         
         $('#toolbar-buttons li').wTooltip({
             delay: 1000, 
                 fontSize: "12px",
             }
         });
-    };
-    
-       // define the callback
-       load_callback = xmleditor_onload;
-})();
+
+               this.texteditor = texteditor;
+    },
+
+       unload: function() { 
+               this.texteditor = null;
+       },
+
+       saveInfo: function(hn, saveInfo) {
+               var myInfo = {content: this.texteditor.getCode(), dataType: 'xml'};
+               $.extend(saveInfo, myInfo);
+       }               
+};
+
 </script>