From c1af2b1f2dfecff255c3c34b47f7a953eda33efd Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Sun, 18 Oct 2015 17:16:50 +0200 Subject: [PATCH] Improved JavaScript code --- src/static/pretixcontrol/js/sb-admin-2.js | 12 +-- src/static/pretixcontrol/js/ui/main.js | 5 +- src/static/pretixpresale/js/ui/asynctask.js | 92 +++++++++++---------- src/static/pretixpresale/js/ui/main.js | 27 +++--- 4 files changed, 74 insertions(+), 62 deletions(-) diff --git a/src/static/pretixcontrol/js/sb-admin-2.js b/src/static/pretixcontrol/js/sb-admin-2.js index 7e0de935e7..ca20df47b1 100644 --- a/src/static/pretixcontrol/js/sb-admin-2.js +++ b/src/static/pretixcontrol/js/sb-admin-2.js @@ -1,10 +1,12 @@ +/*global $ */ //Loads the correct sidebar on window load, //collapses the sidebar on window resize. // Sets the min-height of #page-wrapper to window size -$(function() { - $(window).bind("load resize", function() { - topOffset = 50; - width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width; +$(function () { + 'use strict'; + $(window).bind("load resize", function () { + var topOffset = 50, + width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width; if (width < 768) { $('div.navbar-collapse').addClass('collapse'); topOffset = 100; // 2-row-menu @@ -12,7 +14,7 @@ $(function() { $('div.navbar-collapse').removeClass('collapse'); } - height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1; + var height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1; height = height - topOffset; if (height < 1) height = 1; if (height > topOffset) { diff --git a/src/static/pretixcontrol/js/ui/main.js b/src/static/pretixcontrol/js/ui/main.js index fa487d6853..84aa06e261 100644 --- a/src/static/pretixcontrol/js/ui/main.js +++ b/src/static/pretixcontrol/js/ui/main.js @@ -1,5 +1,8 @@ -"use strict"; +/*global $*/ + $(function () { + "use strict"; + var nested_formset_config = { form: '[data-nested-formset-form]', emptyForm: 'script[type=form-template][data-nested-formset-empty-form]', diff --git a/src/static/pretixpresale/js/ui/asynctask.js b/src/static/pretixpresale/js/ui/asynctask.js index d6e2a79144..5bbf39f1c3 100644 --- a/src/static/pretixpresale/js/ui/asynctask.js +++ b/src/static/pretixpresale/js/ui/asynctask.js @@ -1,11 +1,60 @@ +/*global $, waitingDialog, default_loading_message */ var async_task_id = null; var async_task_timeout = null; var async_task_check_url = null; +function async_task_check() { + "use strict"; + $.ajax( + { + 'type': 'GET', + 'url': async_task_check_url, + 'success': async_task_check_callback, + 'error': async_task_error, + 'context': this, + 'dataType': 'json' + } + ); +} + +function async_task_check_callback(data, jqXHR, status) { + "use strict"; + if (data.ready && data.redirect) { + location.href = data.redirect; + return; + } + async_task_timeout = window.setTimeout(async_task_check, 500); +} + +function async_task_callback(data, jqXHR, status) { + "use strict"; + $(this).data('ajaxing', false); + if (data.redirect) { + location.href = data.redirect; + return; + } + async_task_id = data.async_id; + async_task_check_url = data.check_url; + async_task_timeout = window.setTimeout(async_task_check, 500); +} + +function async_task_error(jqXHR, textStatus, errorThrown) { + "use strict"; + waitingDialog.hide(); + // TODO: Handle status codes != 200 + // if(jqXHR.status == 500) { + // } if(jqXHR.status == 403) { + // } if(jqXHR.status == 503) { + // } +} + $(function () { + "use strict"; $("body").on('submit', 'form[data-asynctask]', function (e) { e.preventDefault(); - if ($(this).data('ajaxing')) return; + if ($(this).data('ajaxing')) { + return; + } $(this).data('ajaxing', true); waitingDialog.show(default_loading_message); @@ -22,44 +71,3 @@ $(function () { ); }); }); - -function async_task_check() { - $.ajax( - { - 'type': 'GET', - 'url': async_task_check_url, - 'success': async_task_check_callback, - 'error': async_task_error, - 'context': this, - 'dataType': 'json' - } - ); -} - -function async_task_check_callback(data, jqXHR, status) { - if (data.ready && data.redirect) { - location.href = data.redirect; - return; - } - async_task_timeout = window.setTimeout(async_task_check, 500); -} - -function async_task_callback(data, jqXHR, status) { - $(this).data('ajaxing', false); - if (data.redirect) { - location.href = data.redirect; - return; - } - async_task_id = data.async_id; - async_task_check_url = data.check_url; - async_task_timeout = window.setTimeout(async_task_check, 500); -} - -function async_task_error(jqXHR, textStatus, errorThrown) { - waitingDialog.hide(); - // TODO - // if(jqXHR.status == 500) { - // } if(jqXHR.status == 403) { - // } if(jqXHR.status == 503) { - // } -} diff --git a/src/static/pretixpresale/js/ui/main.js b/src/static/pretixpresale/js/ui/main.js index 1fd027ad87..eaef1ac489 100644 --- a/src/static/pretixpresale/js/ui/main.js +++ b/src/static/pretixpresale/js/ui/main.js @@ -1,28 +1,27 @@ -"use strict"; +/*global $ */ $(function () { + "use strict"; $("input[data-toggle=radiocollapse]").change(function () { $($(this).attr("data-parent")).find(".collapse.in").collapse('hide'); $($(this).attr("data-target")).collapse('show'); }); $(".js-only").removeClass("js-only"); $(".variations").hide(); - $("a[data-toggle=variations]").click(function() { + $("a[data-toggle=variations]").click(function () { $(this).parent().parent().parent().find(".variations").slideToggle(); }); $(".collapsed").removeClass("collapsed").addClass("collapse"); }); -var waitingDialog = (function ($) { - - return { - show: function (message, options) { - $("#loadingmodal h1").html(message); - $("body").addClass("loading"); - }, - hide: function () { - $("body").removeClass("loading"); - } +var waitingDialog = { + show: function (message) { + "use strict"; + $("#loadingmodal").find("h1").html(message); + $("body").addClass("loading"); + }, + hide: function () { + "use strict"; + $("body").removeClass("loading"); } - -})(jQuery); +};