diff options
Diffstat (limited to 'assets/js/src/Popover.js')
-rw-r--r-- | assets/js/src/Popover.js | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/assets/js/src/Popover.js b/assets/js/src/Popover.js index 8d72dec8..3a209e8a 100644 --- a/assets/js/src/Popover.js +++ b/assets/js/src/Popover.js @@ -13,7 +13,7 @@ Popover.prototype.open = function(link) { self.app.dropdown.close(); $.get(link, function(content) { - $("body").append('<div id="popover-container"><div id="popover-content">' + content + '</div></div>'); + $("body").prepend('<div id="popover-container"><div id="popover-content">' + content + '</div></div>'); self.app.refresh(); self.router.dispatch(this.app); self.afterOpen(); @@ -35,10 +35,11 @@ Popover.prototype.onClick = function(e) { e.preventDefault(); e.stopPropagation(); - var link = e.target.getAttribute("href"); + var target = e.currentTarget || e.target; + var link = target.getAttribute("href"); if (! link) { - link = e.target.getAttribute("data-href"); + link = target.getAttribute("data-href"); } if (link) { @@ -55,26 +56,52 @@ Popover.prototype.listen = function() { Popover.prototype.afterOpen = function() { var self = this; - var taskForm = $("#task-form"); + var popoverForm = $("#popover-content .popover-form"); - if (taskForm) { - taskForm.on("submit", function(e) { + // Submit forms with Ajax request + if (popoverForm) { + popoverForm.on("submit", function(e) { e.preventDefault(); $.ajax({ type: "POST", - url: taskForm.attr("action"), - data: taskForm.serialize(), + url: popoverForm.attr("action"), + data: popoverForm.serialize(), success: function(data, textStatus, request) { - if (request.getResponseHeader("X-Ajax-Redirect")) { - window.location = request.getResponseHeader("X-Ajax-Redirect"); - } - else { - $("#popover-content").html(data); - self.afterOpen(); - } + self.afterSubmit(data, request, self); + }, + beforeSend: function() { + var button = $('.popover-form button[type="submit"]'); + button.html('<i class="fa fa-spinner fa-pulse"></i> ' + button.html()); + button.attr("disabled", true); } }); }); } + + // Submit link with Ajax request + $(document).on("click", ".popover-link", function(e) { + e.preventDefault(); + + $.ajax({ + type: "GET", + url: $(this).attr("href"), + success: function(data, textStatus, request) { + self.afterSubmit(data, request, self); + } + }); + }); +}; + +Popover.prototype.afterSubmit = function(data, request, self) { + var redirect = request.getResponseHeader("X-Ajax-Redirect"); + + if (redirect) { + window.location = redirect === 'self' ? window.location.href.split("#")[0] : redirect; + } + else { + $("#popover-content").html(data); + $("#popover-content input[autofocus]").focus(); + self.afterOpen(); + } }; |