From 60b48e2236aa181c0ea62f4348b9c47940030643 Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Wed, 16 Mar 2016 01:18:16 -0400 Subject: [PATCH] Restructure JavaScript --- .../PatchTester/View/Pulls/tmpl/default.php | 1 + media/com_patchtester/js/fetcher.js | 142 +++++++++++------- media/com_patchtester/js/patchtester.js | 67 ++++++--- 3 files changed, 134 insertions(+), 76 deletions(-) diff --git a/administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default.php b/administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default.php index 0176c3a..ae6bcf3 100644 --- a/administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default.php +++ b/administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default.php @@ -8,6 +8,7 @@ /** @var \PatchTester\View\Pulls\PullsHtmlView $this */ +\JHtml::_('behavior.core'); \JHtml::_('bootstrap.tooltip'); \JHtml::_('formbehavior.chosen', 'select'); \JHtml::_('stylesheet', 'com_patchtester/octicons.css', array(), true); diff --git a/media/com_patchtester/js/fetcher.js b/media/com_patchtester/js/fetcher.js index 1f1d9a1..061cf05 100644 --- a/media/com_patchtester/js/fetcher.js +++ b/media/com_patchtester/js/fetcher.js @@ -1,67 +1,99 @@ -jQuery(function () { - var path = 'index.php?option=com_patchtester&tmpl=component&format=json'; +/** + * Patch testing component for the Joomla! CMS + * + * @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2016 Open Source Matters, Inc. All rights reserved. + * @license GNU General Public License version 2 or later + */ - function initialize() { - offset = 0; - progress = 0; - path = path + '&' + jQuery('#patchtester-token').attr('name') + '=1'; - getRequest('startfetch'); - }; +if (typeof jQuery === 'undefined') { + throw new Error('PatchFetcher JavaScript requires jQuery') +} - function getRequest(task) { - jQuery.ajax({ - type: "GET", - url: path, - data: 'task=' + task, - dataType: 'json', - success: handleResponse, - error: handleFailure - }); - }; +if (typeof Joomla === 'undefined') { + throw new Error('PatchFetcher JavaScript requires the Joomla core JavaScript API') +} - function handleResponse(json, resp) { - try { - if (json === null) { - throw resp; - } - if (json.error) { - throw json; - } +!function (jQuery, Joomla, window) { + 'use strict'; - jQuery('#patchtester-progress-message').html(json.message); + /** + * Initialize the PatchFetcher object + * + * @constructor + */ + var PatchFetcher = function () { + var offset = null, + progress = null, + path = 'index.php?option=com_patchtester&tmpl=component&format=json'; - if (json.data.header) { - jQuery('#patchtester-progress-header').html(json.data.header); - } + var initialize = function () { + offset = 0; + progress = 0; + path = path + '&' + jQuery('#patchtester-token').attr('name') + '=1'; - if (json.data.complete) { - // Nothing to do - } else { - // Send another request - getRequest('fetch'); - } - } catch (error) { - try { - if (json.error) { + getRequest('startfetch'); + }; + + var getRequest = function (task) { + jQuery.ajax({ + type: 'GET', + url: path, + data: 'task=' + task, + dataType: 'json', + success: function (response, textStatus, xhr) { + try { + if (response === null) { + throw textStatus; + } + if (response.error) { + throw response; + } + + jQuery('#patchtester-progress-message').html(response.message); + + if (response.data.header) { + jQuery('#patchtester-progress-header').html(response.data.header); + } + + if (!response.data.complete) { + // Send another request + getRequest('fetch'); + } + } catch (error) { + try { + if (response.error) { + jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); + jQuery('#patchtester-progress-message').html(response.message); + } + } catch (ignore) { + if (error === '') { + error = Joomla.JText._('COM_PATCHTESTER_NO_ERROR_RETURNED'); + } + + jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); + jQuery('#patchtester-progress-message').html(error); + } + } + return true; + }, + error: function (jqXHR, textStatus, errorThrown) { + var json = (typeof jqXHR == 'object' && jqXHR.responseText) ? jqXHR.responseText : null; jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); - jQuery('#patchtester-progress-message').html(json.message); + jQuery('#patchtester-progress-message').html(json); } - } catch (ignore) { - if (error === '') { - error = Joomla.JText._('COM_PATCHTESTER_NO_ERROR_RETURNED'); - } - jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); - jQuery('#patchtester-progress-message').html(error); - } + }); } - return true; + + initialize(); }; - function handleFailure(xhr) { - json = (typeof xhr == 'object' && xhr.responseText) ? xhr.responseText : null; - jQuery('#patchtester-progress-header').text(Joomla.JText._('COM_PATCHTESTER_FETCH_AN_ERROR_HAS_OCCURRED')); - jQuery('#patchtester-progress-message').html(json); - }; + jQuery(function () { + new PatchFetcher(); - initialize(); -}); + if (typeof window.parent.SqueezeBox == 'object') { + jQuery(window.parent.SqueezeBox).on('close', function () { + window.parent.location.reload(true); + }); + } + }); +}(jQuery, Joomla, window); diff --git a/media/com_patchtester/js/patchtester.js b/media/com_patchtester/js/patchtester.js index f43177b..d1f9057 100644 --- a/media/com_patchtester/js/patchtester.js +++ b/media/com_patchtester/js/patchtester.js @@ -1,23 +1,48 @@ -var PatchTester = { - orderTable: function () { - var table = document.getElementById('sortTable'), - direction = document.getElementById('directionTable'), - order = table.options[table.selectedIndex].value, - currentOrder = document.getElementById('adminForm').getAttribute('data-order').valueOf(); +/** + * Patch testing component for the Joomla! CMS + * + * @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2016 Open Source Matters, Inc. All rights reserved. + * @license GNU General Public License version 2 or later + */ - if (order != currentOrder) { - var dirn = 'asc'; - } else { - var dirn = direction.options[direction.selectedIndex].value; - } - - Joomla.tableOrdering(order, dirn, ''); - }, - - submitpatch: function (task, id) { - var idField = document.getElementById('pull_id'); - idField.value = id; - - return Joomla.submitbutton(task); - } +if (typeof Joomla === 'undefined') { + throw new Error('PatchTester JavaScript requires the Joomla core JavaScript API') } + +!function (Joomla, window, document) { + 'use strict'; + + window.PatchTester = { + /** + * Re-order the pull request list table + */ + orderTable: function () { + var table = document.getElementById('sortTable'), + direction = document.getElementById('directionTable'), + order = table.options[table.selectedIndex].value, + currentOrder = document.getElementById('adminForm').getAttribute('data-order').valueOf(); + + if (order != currentOrder) { + var dirn = 'asc'; + } else { + var dirn = direction.options[direction.selectedIndex].value; + } + + Joomla.tableOrdering(order, dirn, ''); + }, + + /** + * Process the patch action + * + * @param {String} task The task to perform + * @param {Number} id The item ID + */ + submitpatch: function (task, id) { + var idField = document.getElementById('pull_id'); + idField.value = id; + + Joomla.submitform(task); + } + }; + +}(Joomla, window, document);