2018-03-03 06:16:55 +01:00
|
|
|
var settings_ui = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2018-03-05 03:13:29 +01:00
|
|
|
exports.display_checkmark = function ($elem) {
|
|
|
|
var check_mark = document.createElement("img");
|
2018-04-12 19:55:17 +02:00
|
|
|
check_mark.src = "/static/images/checkbox-green.svg";
|
2018-03-05 03:13:29 +01:00
|
|
|
$elem.prepend(check_mark);
|
2018-04-12 19:55:17 +02:00
|
|
|
$(check_mark).css("width", "13px");
|
2018-03-05 03:13:29 +01:00
|
|
|
};
|
|
|
|
|
2018-05-30 19:59:47 +02:00
|
|
|
exports.strings = {
|
|
|
|
success: i18n.t("Saved"),
|
|
|
|
failure: i18n.t("Save failed"),
|
|
|
|
saving: i18n.t("Saving"),
|
2018-03-05 03:23:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Generic function for informing users about changes to the settings
|
|
|
|
// UI. Intended to replace the old system that was built around
|
|
|
|
// direct calls to `ui_report`.
|
2018-03-11 10:47:36 +01:00
|
|
|
exports.do_settings_change = function (request_method, url, data, status_element, opts) {
|
2018-03-23 15:23:31 +01:00
|
|
|
var spinner = $(status_element).expectOne();
|
2018-03-29 02:02:01 +02:00
|
|
|
spinner.fadeTo(0, 1);
|
2018-03-23 15:23:31 +01:00
|
|
|
loading.make_indicator(spinner, {text: exports.strings.saving});
|
2018-03-11 10:35:03 +01:00
|
|
|
var success_msg;
|
|
|
|
var success_continuation;
|
2018-03-23 15:23:31 +01:00
|
|
|
var error_continuation;
|
2018-03-30 02:31:11 +02:00
|
|
|
var remove_after = 1000;
|
|
|
|
var appear_after = 500;
|
|
|
|
|
2018-03-11 10:35:03 +01:00
|
|
|
if (opts !== undefined) {
|
|
|
|
success_msg = opts.success_msg;
|
|
|
|
success_continuation = opts.success_continuation;
|
2018-03-23 15:23:31 +01:00
|
|
|
error_continuation = opts.error_continuation;
|
2018-03-30 02:31:11 +02:00
|
|
|
if (opts.sticky) {
|
|
|
|
remove_after = null;
|
|
|
|
}
|
2018-03-11 10:35:03 +01:00
|
|
|
}
|
2018-03-05 03:23:38 +01:00
|
|
|
if (success_msg === undefined) {
|
|
|
|
success_msg = exports.strings.success;
|
|
|
|
}
|
|
|
|
|
2018-03-11 10:47:36 +01:00
|
|
|
request_method({
|
2018-03-05 03:23:38 +01:00
|
|
|
url: url,
|
|
|
|
data: data,
|
2018-03-23 15:23:31 +01:00
|
|
|
success: function (reponse_data) {
|
2018-03-30 02:31:11 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
ui_report.success(success_msg, spinner, null, remove_after);
|
|
|
|
settings_ui.display_checkmark(spinner);
|
|
|
|
}, appear_after);
|
2018-03-23 15:23:31 +01:00
|
|
|
if (success_continuation !== undefined) {
|
|
|
|
success_continuation(reponse_data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
ui_report.error(exports.strings.failure, xhr, spinner);
|
|
|
|
if (error_continuation !== undefined) {
|
|
|
|
error_continuation(xhr);
|
|
|
|
}
|
|
|
|
},
|
2018-03-05 03:23:38 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-03 05:44:58 +01:00
|
|
|
// This function is used to disable sub-setting when main setting is checked or unchecked
|
|
|
|
// or two settings are inter-dependent on their values values.
|
|
|
|
// * is_checked is boolean, shows if the main setting is checked or not.
|
|
|
|
// * sub_setting_id is sub setting or setting which depend on main setting,
|
|
|
|
// string id of setting.
|
|
|
|
// * disable_on_uncheck is boolean, true if sub setting should be disabled
|
|
|
|
// when main setting unchecked.
|
|
|
|
exports.disable_sub_setting_onchange = function (is_checked, sub_setting_id, disable_on_uncheck) {
|
2018-06-06 18:50:09 +02:00
|
|
|
if (is_checked && disable_on_uncheck || !is_checked && !disable_on_uncheck) {
|
2018-03-03 05:44:58 +01:00
|
|
|
$("#" + sub_setting_id).attr("disabled", false);
|
|
|
|
$("#" + sub_setting_id + "_label").parent().removeClass("control-label-disabled");
|
2018-06-06 18:50:09 +02:00
|
|
|
} else if (is_checked && !disable_on_uncheck || !is_checked && disable_on_uncheck) {
|
2018-03-03 05:44:58 +01:00
|
|
|
$("#" + sub_setting_id).attr("disabled", "disabled");
|
|
|
|
$("#" + sub_setting_id + "_label").parent().addClass("control-label-disabled");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-03 06:16:55 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = settings_ui;
|
|
|
|
}
|