Skip to content

Commit

Permalink
Improvements on JavaScript side by Fedik. Closes Bakual#5
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik authored and Thomas Hunziker committed Jan 19, 2017
1 parent 0840d29 commit 4d61081
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 45 deletions.
3 changes: 3 additions & 0 deletions administrator/modules/mod_sampledata/mod_sampledata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<filename>helper.php</filename>
<folder>tmpl</folder>
</files>
<media destination="mod_sampledata" folder="media">
<folder>js</folder>
</media>
<languages>
<language tag="en-GB">en-GB.mod_sampledata.ini</language>
<language tag="en-GB">en-GB.mod_sampledata.sys.ini</language>
Expand Down
62 changes: 17 additions & 45 deletions administrator/modules/mod_sampledata/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,24 @@

JHtml::_('jquery.framework');
JHtml::_('bootstrap.tooltip');
JHtml::_('script', 'mod_sampledata/sampledata-process.js', false, true);

$url = 'index.php?option=com_ajax&format=json&group=sampledata';
$js = '
function applySampledataAjax(type, steps, step) {
if (step <= steps) {
jQuery("div.sampledata-progress-" + type + " ul").append("<li class=\"sampledata-steps-" + type + "-" + step + " center\"><img src=\"' . JUri::root() . '/media/jui/img/ajax-loader.gif\" width=\"30\" height=\"30\" ></li>");
jQuery.get(
"' . $url . '&type=" + type + "&plugin=SampledataApplyStep" + step,
function(response) {
var success = false;
if (response.data.length > 0) {
jQuery.each(
response.data,
function(index, value) {
var successClass = "error";
if (value.success) {
success = true;
successClass = "success";
jQuery(".sampledata-progress-" + type + " progress").val(step/steps);
}
jQuery("li.sampledata-steps-" + type + "-" + step).removeClass("center");
jQuery("li.sampledata-steps-" + type + "-" + step).addClass("alert alert-" + successClass);
jQuery("li.sampledata-steps-" + type + "-" + step).html(value.message);
}
);
if (success) {
step++;
applySampledataAjax(type, steps, step);
}
} else {
jQuery(".sampledata-progress-" + type + " progress").val(0);
jQuery("li.sampledata-steps-" + type + "-" + step).addClass("alert alert-error");
jQuery("li.sampledata-steps-" + type + "-" + step).html("' . JText::_('MOD_SAMPLEDATA_INVALID_RESPONSE') . '");
}
}
);
}
}
function applySampledata(type, steps) {
var step = 1;
jQuery(".sampledata-" + type).after("<div class=\"row-fluid sampledata-progress-" + type + "\"><ul class=\"span12 unstyled\"></ul></div>");
jQuery(".sampledata-" + type).after("<div class=\"row-fluid sampledata-progress-" + type + "\"><progress class=\"span12\"></progress></div>");
applySampledataAjax(type, steps, step);
}';
JText::script('MOD_SAMPLEDATA_CONFIRM_START');
JText::script('MOD_SAMPLEDATA_ITEM_ALREADY_PROCESSED');
JText::script('MOD_SAMPLEDATA_INVALID_RESPONSE');

JFactory::getDocument()->addScriptDeclaration($js);
JFactory::getDocument()->addScriptDeclaration('
var modSampledataUrl = "index.php?option=com_ajax&format=json&group=sampledata",
modSampledataIconProgress = "' . JUri::root(true) . '/media/jui/img/ajax-loader.gif";
');
?>
<div class="sampledata-container">
<?php if ($items) : ?>
<div class="row-striped">
<?php foreach($items as $i => $item) : ?>
<div class="row-fluid sampledata-<?php echo $item->name; ?>">
<div class="span4">
<a href="#" onclick="applySampledata('<?php echo $item->name; ?>', '<?php echo $item->steps; ?>');">
<a href="#" onclick="sampledataApply(this)" data-type="<?php echo $item->name; ?>" data-steps="<?php echo $item->steps; ?>">
<strong class="row-title">
<span class="icon-<?php echo $item->icon; ?>"> </span>
<?php echo htmlspecialchars($item->title, ENT_QUOTES, 'UTF-8'); ?>
Expand All @@ -77,6 +41,14 @@ function applySampledata(type, steps) {
</small>
</div>
</div>
<!-- Progress bar -->
<div class="row-fluid sampledata-progress-<?php echo $item->name; ?> hide">
<progress class="span12"></progress>
</div>
<!-- Progress messages -->
<div class="row-fluid sampledata-progress-<?php echo $item->name; ?> hide">
<ul class="unstyled"></ul>
</div>
<?php endforeach; ?>
</div>
<?php else : ?>
Expand Down
96 changes: 96 additions & 0 deletions media/mod_sampledata/js/sampledata-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

!(function ($) {
"use strict";

var inProgress = false;

var sampledataAjax = function(type, steps, step) {
if (step > steps) {
$('.sampledata-' + type + ' .row-title').append('<span class="icon-publish"> </span>');
inProgress = false;
return;
}
var stepClass = 'sampledata-steps-' + type + '-' + step,
$stepLi = $('<li class="' + stepClass + '"><p class="loader-image text-center"><img src="' + window.modSampledataIconProgress + '" width="30" height="30" ></p></li>'),
$progress = $(".sampledata-progress-" + type + " progress");

$("div.sampledata-progress-" + type + " ul").append($stepLi);

var request = $.ajax({
url: window.modSampledataUrl,
type: 'POST',
dataType: 'json',
data: {
type: type,
plugin: 'SampledataApplyStep' + step,
step: step
}
});
request.done(function(response){
$stepLi.children('.loader-image').remove();

if (response.success && response.data && response.data.length > 0) {
var success, value, resultClass, $msg;

// Display all messages that we got
for(var i = 0, l = response.data.length; i < l; i++) {
value = response.data[i];
success = value.success;
resultClass = success ? 'success' : 'error';
$stepLi.append($('<div>', {
html: value.message,
'class': 'alert alert-' + resultClass,
}));
}

// Update progress
$progress.val(step/steps);

// Move on next step
if (success) {
step++;
sampledataAjax(type, steps, step);
}

} else {
$stepLi.addClass('alert alert-error');
$stepLi.html(Joomla.JText._('MOD_SAMPLEDATA_INVALID_RESPONSE'));
inProgress = false;
}
});
request.fail(function(jqXHR, textStatus){
alert('Something went wrong! Please reboot the Windows and try again!');
});
};

window.sampledataApply = function(el) {
var $el = $(el), type = $el.data('type'), steps = $el.data('steps');

// Check whether the work in progress or we alredy proccessed with current item
if (inProgress) {
return;
}
if ($el.data('processed')) {
alert(Joomla.JText._('MOD_SAMPLEDATA_ITEM_ALREADY_PROCESSED'));
return;
}

// Make sure that use run this not by random clicking on the page links
if (!confirm(Joomla.JText._('MOD_SAMPLEDATA_CONFIRM_START'))) {
return false;
}

// Turn on the progress container
$('.sampledata-progress-' + type).show();
$el.data('processed', true)

inProgress = true;
sampledataAjax(type, steps, 1);
return false;
};

})(jQuery);

0 comments on commit 4d61081

Please sign in to comment.