Panele posiadaja teraz obiekt kontroller, który trzyma ich stan. TODO: lepsza inicjal...
authorŁukasz Rekucki <lrekucki@gmail.com>
Tue, 25 Aug 2009 16:06:51 +0000 (18:06 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Tue, 25 Aug 2009 16:06:51 +0000 (18:06 +0200)
wczytanego JS.

project/static/js/editor.js [new file with mode: 0644]
project/static/js/panels.js
project/templates/explorer/file_xml.html
project/templates/explorer/panels/gallery.html
project/templates/explorer/panels/htmleditor.html
project/templates/explorer/panels/xmleditor.html

diff --git a/project/static/js/editor.js b/project/static/js/editor.js
new file mode 100644 (file)
index 0000000..3cd59f0
--- /dev/null
@@ -0,0 +1,31 @@
+$(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
+               });
+       }
+       
+       $('#toolbar-button-save').click(saveToBranch);
+});
+
index c53d135..d04b421 100644 (file)
@@ -1,46 +1,67 @@
-function loadPanel(target, url) {
-    $.log('ajax', url, 'into', target);
-    $('.change-notification', $(target).parent()).fadeOut();
-    $(document).trigger('panel:unload', target);
+function Panel(target) {
+       var self = this;
+       this.target = target;
+       this.instanceId = Math.ceil(Math.random() * 1000000000);
+       $.log('new panel - target: ', this.target);
+       $(document).bind('panel:unload.' + this.instanceId, 
+                       function(event, data) { self.unload(event, data); });   
+}
+
+Panel.prototype.load = function (url) {
+    $.log('preparing xhr load: ', this.target);
+    $('.change-notification', $(this.target).parent()).fadeOut();
+    $(document).trigger('panel:unload', this.target);
+       var self = this;
+
     $.ajax({
         url: url,
         dataType: 'html',
-        success: function(data, textStatus) {
-            $(target).html(data);
-            $(document).trigger('panel:load', target);
-        },
+               success: function(data, tstat) {
+                       load_callback = unload_callback = null;
+                       $(self.target).html(data);
+                       self._onUnload = unload_callback;
+                       
+                       if(load_callback != null) {
+                               $.log('Invoking panel load callback.');
+                               load_callback(self);
+                       }
+               },
         error: function(request, textStatus, errorThrown) {
-            $.log('ajax', url, target, 'error:', textStatus, errorThrown);
+            $.log('ajax', url, this.target, 'error:', textStatus, errorThrown);
         }
     });
 }
 
-// Funkcja do tworzenia nowych paneli
-function panel(load, unload) {
-    var self = null;
-    var eventId = Math.ceil(Math.random() * 1000000000);
-    
-    unloadHandler = function(event, panel) {
-        if (self && self == panel) {
-            $(document).unbind('panel:unload.' + eventId);
-            $(panel).html('');
-            unload(event, panel);
-            return false;
-        }
+Panel.prototype.unload = function(event, data) {
+       $.log('got unload signal', this.target, ' target: ', data);
+       if(this.target == data) {
+               $(document).unbind('panel:unload.' + this.instanceId);
+        $(this.target).html('');
+               if(this._onUnload != null) this._onUnload(this);
+               return false;
     };
-    
-    $(document).one('panel:load', function(event, panel) {
-        self = panel;
-        $(document).bind('panel:unload.' + eventId, unloadHandler);
-        load(event, panel);
-    });
 }
 
+
+/* 
+ * Return the data that should be saved 
+ */
+Panel.prototype.saveData = function() {
+       return "";
+}
+
+
 $(function() {
     $('#panels').makeHorizPanel({});
     $('#panels').css('top', ($('#header').outerHeight() ) + 'px');
+
+       $('.panel-content').each(function() {
+               var ctrl = new Panel(this);
+               $(this).data('ctrl', ctrl);
+       });
        
     $('.panel-toolbar select').change(function() {
-        loadPanel($('.panel-content', $(this).parent().parent()), $(this).val())
+               var panel = $('.panel-content', $(this).parent().parent());
+               $(panel).data('ctrl').load( $(this).val() );
     });
 });
index 58219de..33e8802 100644 (file)
@@ -7,7 +7,7 @@
     <script src="/static/js/jquery.wtooltip.js" type="text/javascript" charset="utf-8"></script>
     <script src="/static/js/jquery.hpanel.js" type="text/javascript" charset="utf-8"></script>
     <script src="/static/js/panels.js" type="text/javascript" charset="utf-8"></script>
-    <script src="/static/js/save-commit.js" type="text/javascript" charset="utf-8"></script>
+    <script src="/static/js/editor.js" type="text/javascript" charset="utf-8"></script>
 {% endblock extrahead %}
 
 {% block breadcrumbs %}<a href="{% url file_list %}">Platforma Redakcyjna</a> ❯ plik {{ hash }}{% endblock breadcrumbs %}
index 60935bc..6c44a19 100644 (file)
@@ -10,6 +10,7 @@
     </div>
 </div>
 <script type="text/javascript" charset="utf-8">
+
     (function() {
         function resizeEditor(event, panel) {
             var panel = panel || event.data.panel;
index 4515677..bd1bd50 100644 (file)
@@ -1,16 +1,20 @@
 {{ html|safe }}
 <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) {
+(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);
-        });
-    })()
+}
+
+})();
 </script>
index 0c8724f..dfe9477 100644 (file)
@@ -7,6 +7,7 @@
 
 <script type="text/javascript" charset="utf-8">
 (function() {
+
     function xmleditor_onload(event, panel) {
         var textareaId = 'xmleditor-' + Math.ceil(Math.random() * 1000000000);
         $('textarea', panel).attr('id', textareaId);
@@ -70,8 +71,7 @@
         });
     };
     
-    function xmleditor_onunload(event, panel) {}
-    
-    panel(xmleditor_onload, xmleditor_onunload);
+       // define the callback
+       load_callback = xmleditor_onload;
 })();
 </script>