Skip to content

Commit

Permalink
MDL-39319 langimport: Javascript confirmation when uninstalling
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Mastny committed May 29, 2015
1 parent 6ddf92c commit 96a2e00
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 3 deletions.
15 changes: 13 additions & 2 deletions admin/tool/langimport/index.php
Expand Up @@ -79,7 +79,7 @@

if (in_array('en', $uninstalllang)) {
// TODO.
$controller->errors[] = 'English language pack can not be uninstalled';
$controller->errors[] = get_string('noenglishuninstall', 'tool_langimport');

} else if (empty($confirmtounistall) and confirm_sesskey()) { // User chose langs to be deleted, show confirmation.
echo $OUTPUT->header();
Expand Down Expand Up @@ -170,7 +170,10 @@
echo html_writer::select($installedlangs, 'uninstalllang[]', '', false, array('size' => 15, 'multiple' => 'multiple'));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
echo html_writer::empty_tag('br');
echo html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('uninstall', 'tool_langimport')));
echo html_writer::empty_tag('input', array('id' => 'languninstallbutton',
'type' => 'submit',
'value' => get_string('uninstall', 'tool_langimport'))
);
echo html_writer::end_tag('fieldset');
echo html_writer::end_tag('form');
if ($remote) {
Expand Down Expand Up @@ -207,5 +210,13 @@
echo html_writer::end_tag('tr');
echo html_writer::end_tag('table');
echo $OUTPUT->box_end();

$uninstallurl = new moodle_url('/admin/tool/langimport/index.php');
$PAGE->requires->strings_for_js(array('uninstallconfirm', 'uninstall', 'selectlangs', 'noenglishuninstall'),
'tool_langimport');
$PAGE->requires->yui_module('moodle-core-languninstallconfirm',
'Y.M.core.languninstallconfirm.init',
array(array('uninstallUrl' => $uninstallurl->out()))
);
echo $OUTPUT->footer();
die();
6 changes: 5 additions & 1 deletion admin/tool/langimport/lang/en/tool_langimport.php
Expand Up @@ -44,6 +44,10 @@
$string['purgestringcaches'] = 'Purge string caches';
$string['remotelangnotavailable'] = 'Because Moodle cannot connect to download.moodle.org, it is not possible for language packs to be installed automatically. Please download the appropriate ZIP file(s) from <a href="https://download.moodle.org/langpack/">download.moodle.org/langpack</a>, copy them to your {$a} directory and unzip them manually.';
$string['uninstall'] = 'Uninstall selected language packs';
$string['uninstallconfirm'] = 'You are about to completely uninstall these language packs: {$a}. Are you sure?';
$string['uninstallconfirm'] = 'You are about to completely uninstall these language packs: <strong>{$a}</strong>. Are you sure?';
$string['updatelangs'] = 'Update all installed language packs';
$string['selectlangs'] = 'Select languages to unistall!';
$string['noenglishuninstall'] = 'English language pack can not be uninstalled';



@@ -0,0 +1,109 @@
YUI.add('moodle-core-languninstallconfirm', function (Y, NAME) {

/**
* Home for a Confirmation class.
*
* @module moodle-core-languninstallconfirm
*/

/**
* A class for a language uninstall confirmation.
*
* @class M.core.languninstallconfirm
* @constructor
* @extends Base
*/
function Confirmation() {
Confirmation.superclass.constructor.apply(this, arguments);
}

var SELECTORS = {
UNINSTALLBUTTON: '#languninstallbutton',
UNINSTALLSELECT: '#menuuninstalllang option',
ENGLISHOPTION: '#menuuninstalllang option[value=\'en\']'
};

Confirmation.NAME = NAME;
Confirmation.ATTRS = {
/**
* Uninstall url
*
* @property uninstallUrl
* @type string
*/
uninstallUrl: {
validator: Y.Lang.isString
}
};
Y.extend(Confirmation, Y.Base, {
/**
* Initializer.
* Registers onclicks.
*
* @method initializer
*/
initializer: function() {
Y.one(SELECTORS.UNINSTALLBUTTON).on('click', this._confirm, this);
},
/**
* Confirmation.
* Displays the confirmation dialogue.
*
* @method _confirm
* @protected
* @param {EventFacade} e
*/
_confirm: function(e) {
e.preventDefault();
var selectedLangCodes = [];
var selectedLangNames = [];

Y.all(SELECTORS.UNINSTALLSELECT).each(function(option){
if (option.get('selected')){
selectedLangCodes.push(option.getAttribute('value'));
selectedLangNames.push(option.get('text'));
}
});
// Nothing was selected, show warning.
if (selectedLangCodes.length === 0){
new M.core.alert({ message: M.util.get_string('selectlangs', 'tool_langimport') }).show();
return;
} else if (selectedLangCodes.indexOf('en')> -1) { // Don't uninstall english.
Y.one(SELECTORS.ENGLISHOPTION).set('selected',false);
new M.core.alert({ message: M.util.get_string('noenglishuninstall', 'tool_langimport') }).show();
return;
}
var confirmationConfig = {
modal: true,
visible : false,
centered : true,
title : M.util.get_string('uninstall','tool_langimport'),
question : M.util.get_string('uninstallconfirm','tool_langimport', selectedLangNames.join(", "))
};
new M.core.confirm(confirmationConfig)
.show()
.on('complete-yes', this._uninstall, this, selectedLangCodes);
},
/**
* Uninstall.
* Redirects to an uninstall process.
*
* @method _uninstall
* @protected
* @param {EventFacade} e
* @param {Array} langCodes array of lang codes to be uninstalled
*/
_uninstall : function(e, langCodes) {
Y.config.win.location.href = this.get('uninstallUrl') + '?mode=4' +
'&sesskey=' + M.cfg.sesskey +
'&confirmtouninstall=' + langCodes.join('-');
}

});

Y.namespace('M.core.languninstallconfirm').Confirmation = Confirmation;
Y.namespace('M.core.languninstallconfirm').init = function(config) {
return new Confirmation(config);
};

}, '@VERSION@', {"requires": ["base", "node", "moodle-core-notification-confirm", "moodle-core-notification-alert"]});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,109 @@
YUI.add('moodle-core-languninstallconfirm', function (Y, NAME) {

/**
* Home for a Confirmation class.
*
* @module moodle-core-languninstallconfirm
*/

/**
* A class for a language uninstall confirmation.
*
* @class M.core.languninstallconfirm
* @constructor
* @extends Base
*/
function Confirmation() {
Confirmation.superclass.constructor.apply(this, arguments);
}

var SELECTORS = {
UNINSTALLBUTTON: '#languninstallbutton',
UNINSTALLSELECT: '#menuuninstalllang option',
ENGLISHOPTION: '#menuuninstalllang option[value=\'en\']'
};

Confirmation.NAME = NAME;
Confirmation.ATTRS = {
/**
* Uninstall url
*
* @property uninstallUrl
* @type string
*/
uninstallUrl: {
validator: Y.Lang.isString
}
};
Y.extend(Confirmation, Y.Base, {
/**
* Initializer.
* Registers onclicks.
*
* @method initializer
*/
initializer: function() {
Y.one(SELECTORS.UNINSTALLBUTTON).on('click', this._confirm, this);
},
/**
* Confirmation.
* Displays the confirmation dialogue.
*
* @method _confirm
* @protected
* @param {EventFacade} e
*/
_confirm: function(e) {
e.preventDefault();
var selectedLangCodes = [];
var selectedLangNames = [];

Y.all(SELECTORS.UNINSTALLSELECT).each(function(option){
if (option.get('selected')){
selectedLangCodes.push(option.getAttribute('value'));
selectedLangNames.push(option.get('text'));
}
});
// Nothing was selected, show warning.
if (selectedLangCodes.length === 0){
new M.core.alert({ message: M.util.get_string('selectlangs', 'tool_langimport') }).show();
return;
} else if (selectedLangCodes.indexOf('en')> -1) { // Don't uninstall english.
Y.one(SELECTORS.ENGLISHOPTION).set('selected',false);
new M.core.alert({ message: M.util.get_string('noenglishuninstall', 'tool_langimport') }).show();
return;
}
var confirmationConfig = {
modal: true,
visible : false,
centered : true,
title : M.util.get_string('uninstall','tool_langimport'),
question : M.util.get_string('uninstallconfirm','tool_langimport', selectedLangNames.join(", "))
};
new M.core.confirm(confirmationConfig)
.show()
.on('complete-yes', this._uninstall, this, selectedLangCodes);
},
/**
* Uninstall.
* Redirects to an uninstall process.
*
* @method _uninstall
* @protected
* @param {EventFacade} e
* @param {Array} langCodes array of lang codes to be uninstalled
*/
_uninstall : function(e, langCodes) {
Y.config.win.location.href = this.get('uninstallUrl') + '?mode=4' +
'&sesskey=' + M.cfg.sesskey +
'&confirmtouninstall=' + langCodes.join('-');
}

});

Y.namespace('M.core.languninstallconfirm').Confirmation = Confirmation;
Y.namespace('M.core.languninstallconfirm').init = function(config) {
return new Confirmation(config);
};

}, '@VERSION@', {"requires": ["base", "node", "moodle-core-notification-confirm", "moodle-core-notification-alert"]});
10 changes: 10 additions & 0 deletions lib/yui/src/languninstallconfirm/build.json
@@ -0,0 +1,10 @@
{
"name": "moodle-core-languninstallconfirm",
"builds": {
"moodle-core-languninstallconfirm": {
"jsfiles": [
"languninstallconfirm.js"
]
}
}
}
105 changes: 105 additions & 0 deletions lib/yui/src/languninstallconfirm/js/languninstallconfirm.js
@@ -0,0 +1,105 @@
/**
* Home for a Confirmation class.
*
* @module moodle-core-languninstallconfirm
*/

/**
* A class for a language uninstall confirmation.
*
* @class M.core.languninstallconfirm
* @constructor
* @extends Base
*/
function Confirmation() {
Confirmation.superclass.constructor.apply(this, arguments);
}

var SELECTORS = {
UNINSTALLBUTTON: '#languninstallbutton',
UNINSTALLSELECT: '#menuuninstalllang option',
ENGLISHOPTION: '#menuuninstalllang option[value=\'en\']'
};

Confirmation.NAME = NAME;
Confirmation.ATTRS = {
/**
* Uninstall url
*
* @property uninstallUrl
* @type string
*/
uninstallUrl: {
validator: Y.Lang.isString
}
};
Y.extend(Confirmation, Y.Base, {
/**
* Initializer.
* Registers onclicks.
*
* @method initializer
*/
initializer: function() {
Y.one(SELECTORS.UNINSTALLBUTTON).on('click', this._confirm, this);
},
/**
* Confirmation.
* Displays the confirmation dialogue.
*
* @method _confirm
* @protected
* @param {EventFacade} e
*/
_confirm: function(e) {
e.preventDefault();
var selectedLangCodes = [];
var selectedLangNames = [];

Y.all(SELECTORS.UNINSTALLSELECT).each(function(option){
if (option.get('selected')){
selectedLangCodes.push(option.getAttribute('value'));
selectedLangNames.push(option.get('text'));
}
});
// Nothing was selected, show warning.
if (selectedLangCodes.length === 0){
new M.core.alert({ message: M.util.get_string('selectlangs', 'tool_langimport') }).show();
return;
} else if (selectedLangCodes.indexOf('en')> -1) { // Don't uninstall english.
Y.one(SELECTORS.ENGLISHOPTION).set('selected',false);
new M.core.alert({ message: M.util.get_string('noenglishuninstall', 'tool_langimport') }).show();
return;
}
var confirmationConfig = {
modal: true,
visible : false,
centered : true,
title : M.util.get_string('uninstall','tool_langimport'),
question : M.util.get_string('uninstallconfirm','tool_langimport', selectedLangNames.join(", "))
};
new M.core.confirm(confirmationConfig)
.show()
.on('complete-yes', this._uninstall, this, selectedLangCodes);
},
/**
* Uninstall.
* Redirects to an uninstall process.
*
* @method _uninstall
* @protected
* @param {EventFacade} e
* @param {Array} langCodes array of lang codes to be uninstalled
*/
_uninstall : function(e, langCodes) {
Y.config.win.location.href = this.get('uninstallUrl') + '?mode=4' +
'&sesskey=' + M.cfg.sesskey +
'&confirmtouninstall=' + langCodes.join('-');
}

});

Y.namespace('M.core.languninstallconfirm').Confirmation = Confirmation;
Y.namespace('M.core.languninstallconfirm').init = function(config) {
return new Confirmation(config);
};
10 changes: 10 additions & 0 deletions lib/yui/src/languninstallconfirm/meta/languninstallconfirm.json
@@ -0,0 +1,10 @@
{
"moodle-core-languninstallconfirm": {
"requires": [
"base",
"node",
"moodle-core-notification-confirm",
"moodle-core-notification-alert"
]
}
}

0 comments on commit 96a2e00

Please sign in to comment.