From e46f201915e63a80d70b815acdba0cb4859f8ec3 Mon Sep 17 00:00:00 2001
From: Benjamin Dauvergne <bdauvergne@entrouvert.com>
Date: Fri, 18 Jul 2014 17:35:45 +0200
Subject: [PATCH] gadjo.js: add support for ajax submission of form

---
 gadjo/static/js/gadjo.js |   74 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 57 insertions(+), 17 deletions(-)

diff --git a/gadjo/static/js/gadjo.js b/gadjo/static/js/gadjo.js
index 23e35d1..b6706bd 100644
--- a/gadjo/static/js/gadjo.js
+++ b/gadjo/static/js/gadjo.js
@@ -1,36 +1,76 @@
 function displayPopup(event)
 {
-    var url = $(this).attr('href');
+    var $anchor = $(this);
+    var url = $anchor.attr('href');
+    var selector = $anchor.data('selector') || 'form';
+    var title_selector = $anchor.data('title-selector') || '#appbar h2';
+
+    function ajaxform_submit (data, status, xhr, form) {
+        if ('location' in data) {
+            var e = $.Event('gadjo:dialog-done');
+            $anchor.trigger(e, form);
+
+            if (! e.isDefaultPrevented()) {
+              if (data.location.split('#')[0] == window.location.href.split('#')[0]) {
+                  window.location.reload(true);
+              }
+              window.location = data.location;
+            }
+        } else {
+            var $form = $(form);
+
+            $form.empty().append($(data.content).find(selector).children());
+            $form.find('.buttons').hide();
+        }
+    }
+
     $.ajax({
         url: url,
         success: function(html) {
-            var form = $(html).find('#content form');
-            var title = $(html).find('#appbar h2').text();
-            var dialog = $(form).dialog({modal: true, title: title, width: 'auto'});
+            var is_json = typeof html != 'string';
+            if (is_json) {
+                var html = html.content;
+            } else {
+                var html = html;
+            }
+            var $html = $(html);
+            var $form = $html.find(selector);
+            var title = $html.find(title_selector).text();
             var buttons = Array();
-            if (! form.prop('action')) {
-                form.prop('action', url);
+
+            $form.dialog({modal: true, 'title': title, width: 'auto'});
+
+            if (! $form.prop('action')) {
+                $form.prop('action', url);
             }
-            $(dialog).find('.buttons').hide();
-            $(html).find('.buttons button, .buttons a').each(function(idx, elem) {
+
+            $form.find('.buttons').hide();
+
+            $form.find('.buttons button, .buttons a').each(function(idx, elem) {
+                var $elem = $(elem);
                 var button = Object();
-                button.text = $(elem).text();
-                if ($(elem).hasClass('cancel')) {
-                    button.click = function() { $(this).dialog('destroy'); return false; };
+
+                button.text = $elem.text();
+                if ($elem.hasClass('cancel')) {
+                    button.click = function() { $form.dialog('destroy'); return false; };
                 } else {
-                    button.click = function() { (form).find('button').click(); return false; };
+                    button.click = function() { $form.find('button').click(); return false; };
                 }
-                if ($(elem).hasClass('submit-button')) {
+                if ($elem.hasClass('submit-button')) {
                     button.class = 'submit-button';
-                } else if ($(elem).hasClass('delete-button')) {
+                } else if ($elem.hasClass('delete-button')) {
                     button.class = 'delete-button';
                 }
                 buttons.push(button);
             });
+
             buttons.reverse();
-            $(dialog).dialog('option', 'buttons', buttons);
-            if ($(dialog).find('input:visible').length) {
-                $(dialog).find('input:visible')[0].focus();
+            $form.dialog('option', 'buttons', buttons);
+            if ($form.find('input:visible').length) {
+                $form.find('input:visible')[0].focus();
+            }
+            if (is_json && $.fn.ajaxForm != undefined) {
+                $form.ajaxForm({success: ajaxform_submit});
             }
             return false;
         }
-- 
1.7.10.4

