Restructure JavaScript

This commit is contained in:
Michael Babker 2016-03-16 01:18:16 -04:00
parent e436fb7a22
commit 60b48e2236
3 changed files with 134 additions and 76 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);