Skip to content

Commit

Permalink
[+] Modules : Implement ajax calls on single module or bulk actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Quetzacoalt91 authored and xGouley committed Feb 10, 2016
1 parent c7b617f commit bdf5f6e
Show file tree
Hide file tree
Showing 10 changed files with 513 additions and 469 deletions.
786 changes: 391 additions & 395 deletions admin-dev/themes/default/js/bundle/module/module.js

Large diffs are not rendered by default.

151 changes: 102 additions & 49 deletions admin-dev/themes/default/js/bundle/module/module_card.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,121 @@
$(document).ready(function() {
var module_card_controller = {};

var controller = new AdminModuleCard();
controller.init();
$(document).ready(function () {

module_card_controller = new AdminModuleCard();
module_card_controller.init();

});

/**
* AdminModule card Controller.
* @constructor
*/
var AdminModuleCard = function() {
var AdminModuleCard = function () {
/* Selectors for module action links (uninstall, reset, etc...) to add a confirm popin */
this.moduleActionMenuLinkSelector = 'a.module_action_menu_';
this.moduleActionMenuInstallLinkSelector = 'a.module_action_menu_install';
this.moduleActionMenuEnableLinkSelector = 'a.module_action_menu_enable';
this.moduleActionMenuUninstallLinkSelector = 'a.module_action_menu_uninstall';
this.moduleActionMenuDisableLinkSelector = 'a.module_action_menu_disable';
this.moduleActionMenuResetLinkSelector = 'a.module_action_menu_reset';
this.moduleActionMenuUpdateLinkSelector = 'a.module_action_menu_update';

/**
* Initialize all listeners and bind everything
* @method init
* @memberof AdminModuleCard
*/
this.init = function() {
this.initActionButtons();
};
/* Selectors only for modal buttons */
this.moduleActionModalDisableLinkSelector = 'a.module_action_modal_disable';
this.moduleActionModalResetLinkSelector = 'a.module_action_modal_reset';
this.moduleActionModalUninstallLinkSelector = 'a.module_action_modal_uninstall';

/**
* Initialize all listeners and bind everything
* @method init
* @memberof AdminModuleCard
*/
this.init = function () {
this.initActionButtons();
};

this.initActionButtons = function () {
// action buttons on a module card
var confirmAction = function (action, element) {
var modal = $('#' + $(element).data('confirm_modal'));
if (modal.length != 1) {
return true;
}
modal.first().modal('show');
;
return false; // do not allow a.href to reload the page. The confirm modal dialog will do it async if needed.
};
var dispatchPreEvent = function (action, element) {
var event = jQuery.Event('module_card_action_event');
$(element).trigger(event, [action]);
if (event.isPropagationStopped() !== false || event.isImmediatePropagationStopped() !== false) {
return false; // if all handlers have not been called, then stop propagation of the click event.
}
return (event.result !== false); // explicit false must be set from handlers to stop propagation of the click event.
};

$(document).on('click', this.moduleActionMenuInstallLinkSelector, function () {
return dispatchPreEvent('install', this) && confirmAction('install', this) && module_card_controller.requestToController('install', $(this));
});
$(document).on('click', this.moduleActionMenuEnableLinkSelector, function () {
return dispatchPreEvent('enable', this) && confirmAction('enable', this) && module_card_controller.requestToController('enable', $(this));
});
$(document).on('click', this.moduleActionMenuUninstallLinkSelector, function () {
return dispatchPreEvent('uninstall', this) && confirmAction('uninstall', this) && module_card_controller.requestToController('uninstall', $(this));
});
$(document).on('click', this.moduleActionMenuDisableLinkSelector, function () {
return dispatchPreEvent('disable', this) && confirmAction('disable', this) && module_card_controller.requestToController('disable', $(this));
});
$(document).on('click', this.moduleActionMenuResetLinkSelector, function () {
return dispatchPreEvent('reset', this) && confirmAction('reset', this) && module_card_controller.requestToController('reset', $(this));
});
$(document).on('click', this.moduleActionMenuUpdateLinkSelector, function () {
return dispatchPreEvent('update', this) && confirmAction('update', this) && module_card_controller.requestToController('update', $(this));
});

$(document).on('click', this.moduleActionModalDisableLinkSelector, function () {
return module_card_controller.requestToController('disable', $(module_card_controller.moduleActionMenuDisableLinkSelector, $("div.module-item-list[data-tech-name='" + $(this).attr("data-tech-name") + "']")));
});
$(document).on('click', this.moduleActionModalResetLinkSelector, function () {
return module_card_controller.requestToController('reset', $(module_card_controller.moduleActionMenuResetLinkSelector, $("div.module-item-list[data-tech-name='" + $(this).attr("data-tech-name") + "']")));
});
$(document).on('click', this.moduleActionModalUninstallLinkSelector, function () {
return module_card_controller.requestToController('uninstall', $(module_card_controller.moduleActionMenuUninstallLinkSelector, $("div.module-item-list[data-tech-name='" + $(this).attr("data-tech-name") + "']")));
});
};

this.initActionButtons = function() {
// action buttons on a module card
var confirmAction = function(action, element) {
var modal = $('#'+$(element).data('confirm_modal'));
if (modal.length != 1) {
return true;
}
modal.first().modal('show');;
return false; // do not allow a.href to reload the page. The confirm modal dialog will do it async if needed.
};
var dispatchPreEvent = function(action, element) {
var event = jQuery.Event('module_card_action_event');
$(element).trigger(event, [action]);
if (event.isPropagationStopped() !== false || event.isImmediatePropagationStopped() !== false) {
return false; // if all handlers have not been called, then stop propagation of the click event.
}
return (event.result !== false); // explicit false must be set from handlers to stop propagation of the click event.
};
$(this.moduleActionMenuInstallLinkSelector).on('click', function() {
return dispatchPreEvent('install', this) && confirmAction('install', this);
});
$(this.moduleActionMenuEnableLinkSelector).on('click', function() {
return dispatchPreEvent('enable', this) && confirmAction('enable', this);
});
$(this.moduleActionMenuUninstallLinkSelector).on('click', function() {
return dispatchPreEvent('uninstall', this) && confirmAction('uninstall', this);
});
$(this.moduleActionMenuDisableLinkSelector).on('click', function() {
return dispatchPreEvent('disable', this) && confirmAction('disable', this);
});
$(this.moduleActionMenuResetLinkSelector).on('click', function() {
return dispatchPreEvent('reset', this) && confirmAction('reset', this);
});
$(this.moduleActionMenuUpdateLinkSelector).on('click', function() {
return dispatchPreEvent('update', this) && confirmAction('update', this);
});
};
this.requestToController = function (action, element) {
var jqElementObj = element.closest(".btn-group");
var spinnerObj = $("<button class=\"btn btn-primary-reverse btn-lg onclic unbind pull-right\"></button>");
$.ajax({
url: "//" + window.location.hostname + element.attr("href"),
dataType: 'json',
beforeSend: function () {
jqElementObj.hide();
jqElementObj.after(spinnerObj);
}
}).done(function (result) {
if (typeof result === undefined) {
$.growl.error({message: "No answer received from server"});
} else {
var moduleTechName = Object.keys(result)[0];
if (result[moduleTechName].status == false) {
$.growl.error({message: result[moduleTechName].msg});
} else {
$.growl.notice({message: result[moduleTechName].msg});
if (action != "uninstall") {
jqElementObj.html(result[moduleTechName].action_menu_html);
} else {
jqElementObj.html("");
}
}
}
}).always(function () {
jqElementObj.fadeIn();
spinnerObj.remove();
});
return false;
};

};
4 changes: 4 additions & 0 deletions src/Adapter/Module/AdminModuleDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ public function __construct(\AppKernel $kernel, Router $router)
public function clearCatalogCache()
{
$this->clearCache([self::_CACHEFILE_CATEGORIES_, self::_CACHEFILE_MODULES_]);
$this->catalog_categories = [];
$this->catalog_modules = [];
}

public function clearManageCache()
{
$this->clearCache([self::_CACHEFILE_INSTALLED_CATEGORIES_, self::_CACHEFILE_INSTALLED_MODULES_]);
$this->manage_categories = [];
$this->manage_modules = [];
}

public function getAllModules()
Expand Down
2 changes: 1 addition & 1 deletion src/PrestaShopBundle/Controller/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function moduleAction(Request $request)
$modulesProvider->clearManageCache();

if ($request->isXmlHttpRequest()) {
if ($ret[$module]['status'] === true) {
if ($ret[$module]['status'] === true && $action != 'uninstallModule') {
$moduleInstance = $modulesProvider->getManageModules(['name' => $module]);
$moduleInstanceWithUrl = $modulesProvider->generateAddonsUrls($moduleInstance);
$ret[$module]['action_menu_html'] = $this->render('PrestaShopBundle:Admin/Module/_partials:_modules_action_menu.html.twig', array(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<div class="pull-right btn-group">

{% if module.url_active == 'buy' %}
<a class="btn btn-default btn-xs light-button module_action_menu_go_to_addons" href="{{module.url}}">{% if module.price.EUR is defined and module.price.EUR > 0 %}{{module.price.EUR}}{% else %}Discover{% endif %}</a>
<a class="btn btn-primary btn-xs light-button module_action_menu_go_to_addons" href="{{module.url}}">{% if module.price.EUR is defined and module.price.EUR > 0 %}{{module.price.EUR}}{% else %}Discover{% endif %}</a>
{% else %}
<a class="btn btn-default btn-xs light-button module_action_menu_{{ module.url_active }}"
<a class="btn btn-primary btn-xs light-button module_action_menu_{{ module.url_active }}"
href="{{module.urls[module.url_active]}}"
data-confirm_modal="module-modal-confirm-{{module.name}}-{{ module.url_active }}">{{module.url_active|capitalize}}</a>
{% endif %}

{% if module.urls is defined and module.urls|length > 1 %}
<button type="button" class="btn btn-default dropdown-toggle light-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<button type="button" class="btn btn-primary dropdown-toggle light-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
Expand All @@ -23,14 +23,4 @@
{% endfor %}
</ul>
{% endif %}

{% if module.url_active == 'install' %}
{# Loader for installation process #}
<div class="loader module-install-loader">
<svg class="circular" viewBox="25 25 50 50">
<circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10"/>
</svg>
</div>
{% endif %}

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@
<div class="modal-footer">
<div class="col-xs-6 pull-left text-left">
<input type="button" class="btn btn-default uppercase" data-dismiss="modal" value="{{ trans("Cancel", {}, 'AdminTabs')|raw }}" />
{% if module_action == 'disable' %}
<input type="button" class="btn btn-primary uppercase" data-dismiss="modal" value="{{ trans("Yes, I want to disable", {}, 'AdminModules')|raw }}"
onclick="window.location.href='{{ module_url }}';" />
{% if module_action == 'disable' %}
<a class="btn btn-primary uppercase module_action_modal_{{ module_action }}" data-dismiss="modal"
data-tech-name="{{module.name}}"
href="{{ module_url }}">{{ trans("Yes, I want to disable", {}, 'AdminModules')|raw }}</a>
{% endif %}
{% if module_action == 'uninstall' %}
<input type="button" class="btn btn-primary uppercase" data-dismiss="modal" value="{{ trans("Yes, I want to uninstall", {}, 'AdminModules')|raw }}"
onclick="window.location.href='{{ module_url }}';" />
<a class="btn btn-primary uppercase module_action_modal_{{ module_action }}" data-dismiss="modal"
href="{{ module_url }}">{{ trans("Yes, I want to uninstall", {}, 'AdminModules')|raw }}</a>
{% endif %}
{% if module_action == 'reset' %}
<input type="button" class="btn btn-primary uppercase" data-dismiss="modal" value="{{ trans("Yes, I want to reset", {}, 'AdminModules')|raw }}"
onclick="window.location.href='{{ module_url }}';" />
<a class="btn btn-primary uppercase module_action_modal_{{ module_action }}" data-dismiss="modal"
href="{{ module_url }}">{{ trans("Yes, I want to reset", {}, 'AdminModules')|raw }}</a>
{% endif %}
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<script src="{{ asset('themes/default/js/bundle/plugins/jquery.pstagger.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/loader.js') }}"></script>
<script src="{{ asset('../js/vendor/node_modules/dropzone/dist/min/dropzone.min.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module_card.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module.js') }}"></script>
{% endblock %}

{% block content %}
Expand Down
2 changes: 1 addition & 1 deletion src/PrestaShopBundle/Resources/views/Admin/Module/configure.html.twig
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<script src="{{ asset('themes/default/js/bundle/plugins/jquery.pstagger.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/loader.js') }}"></script>
<script src="{{ asset('../js/vendor/node_modules/dropzone/dist/min/dropzone.min.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module_card.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module.js') }}"></script>
{% endblock %}

{% block content %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<script src="{{ asset('themes/default/js/bundle/plugins/jquery.pstagger.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/loader.js') }}"></script>
<script src="{{ asset('../js/vendor/node_modules/dropzone/dist/min/dropzone.min.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module_card.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module.js') }}"></script>
{% endblock %}

{% block content %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<script src="{{ asset('themes/default/js/bundle/plugins/jquery.pstagger.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/loader.js') }}"></script>
<script src="{{ asset('../js/vendor/node_modules/dropzone/dist/min/dropzone.min.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module_card.js') }}"></script>
<script src="{{ asset('themes/default/js/bundle/module/module.js') }}"></script>
{% endblock %}

{% block content %}
Expand Down

0 comments on commit bdf5f6e

Please sign in to comment.