summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/clientscript.js
diff options
context:
space:
mode:
authorSimon Rettberg2017-06-10 16:15:22 +0200
committerSimon Rettberg2017-06-10 16:15:22 +0200
commita0b42aa257d0a6c56cd4a099aa0a2cea4a8dc2c9 (patch)
tree3e1655c828e0cb6f069b8c17ddf260237e0f4ce2 /modules-available/locationinfo/clientscript.js
parent[inc/Util] Add randomUuid() function (diff)
downloadslx-admin-a0b42aa257d0a6c56cd4a099aa0a2cea4a8dc2c9.tar.gz
slx-admin-a0b42aa257d0a6c56cd4a099aa0a2cea4a8dc2c9.tar.xz
slx-admin-a0b42aa257d0a6c56cd4a099aa0a2cea4a8dc2c9.zip
[locationinfo] Started rewrite for panel-based approach
Diffstat (limited to 'modules-available/locationinfo/clientscript.js')
-rw-r--r--modules-available/locationinfo/clientscript.js143
1 files changed, 143 insertions, 0 deletions
diff --git a/modules-available/locationinfo/clientscript.js b/modules-available/locationinfo/clientscript.js
new file mode 100644
index 00000000..b18cc5bd
--- /dev/null
+++ b/modules-available/locationinfo/clientscript.js
@@ -0,0 +1,143 @@
+/*
+ * Generic helpers.
+ */
+
+/**
+ * Initialize timepicker on given element.
+ */
+function setTimepicker($e) {
+ $e.timepicker({
+ minuteStep: 15,
+ appendWidgetTo: 'body',
+ showSeconds: false,
+ showMeridian: false,
+ defaultTime: false
+ });
+}
+
+function getTime(str) {
+ if (!str) return false;
+ str = str.split(':');
+ if (str.length !== 2) return false;
+ var h = parseInt(str[0].replace(/^0/, ''));
+ var m = parseInt(str[1].replace(/^0/, ''));
+ if (h < 0 || h > 23) return false;
+ if (m < 0 || m > 59) return false;
+ return h * 60 + m;
+}
+
+const allDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
+
+/*
+ * Opening times related...
+ */
+
+/**
+ * Adds a new opening time to the table in expert mode.
+ */
+function newOpeningTime(vals) {
+ var $row = $('#expert-template div.row').clone();
+ if (vals['days'] && Array.isArray(vals['days'])) {
+ for (var i = 0; i < allDays.length; ++i) {
+ $row.find('.i-' + allDays[i]).attr('checked', vals['days'].indexOf(allDays[i]) !== -1);
+ }
+ }
+ $row.find('.i-openingtime').val(vals['openingtime']);
+ $row.find('.i-closingtime').val(vals['closingtime']);
+ $('#expert-table').append($row);
+ return $row;
+}
+
+/**
+ * Convert fields from simple mode view to entries in expert mode.
+ * @returns {Array}
+ */
+function simpleToExpert() {
+ var retval = [];
+ if ($('#week-open').val() || $('#week-close').val()) {
+ retval.push({
+ 'days': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
+ 'openingtime': $('#week-open').val(),
+ 'closingtime': $('#week-close').val(),
+ 'tag': '#week'
+ });
+ }
+ if ($('#saturday-open').val() || $('#saturday-close').val()) {
+ retval.push({
+ 'days': ['Saturday'],
+ 'openingtime': $('#saturday-open').val(),
+ 'closingtime': $('#saturday-close').val(),
+ 'tag': '#saturday'
+ });
+ }
+ if ($('#sunday-open').val() || $('#sunday-close').val()) {
+ retval.push({
+ 'days': ['Sunday'],
+ 'openingtime': $('#sunday-open').val(),
+ 'closingtime': $('#sunday-close').val(),
+ 'tag': '#sunday'
+ });
+ }
+ return retval;
+}
+
+/**
+ * Triggered when the form is submitted
+ */
+function submitLocationSettings(event) {
+ var schedule, s, e;
+ var badFormat = false;
+ $('#settings-outer').find('.red-bg').removeClass('red-bg');
+ if ($('#week-open').length > 0) {
+ schedule = simpleToExpert();
+ for (var i = 0; i < schedule.length; ++i) {
+ s = getTime(schedule[i].openingtime);
+ e = getTime(schedule[i].closingtime);
+ if (s === false) {
+ $(schedule[i].tag + '-open').addClass('red-bg');
+ badFormat = true;
+ }
+ if (e === false || e <= s) {
+ $(schedule[i].tag + '-close').addClass('red-bg');
+ badFormat = true;
+ }
+ }
+ } else {
+ // Serialize
+ schedule = [];
+ $('#expert-table').find('.expert-row').each(function () {
+ var $t = $(this);
+ if ($t.find('.i-delete').is(':checked')) return; // Skip marked as delete
+ var entry = {
+ 'days': [],
+ 'openingtime': $t.find('.i-openingtime').val(),
+ 'closingtime': $t.find('.i-closingtime').val()
+ };
+ s = getTime(entry.openingtime);
+ e = getTime(entry.closingtime);
+ if (s === false) {
+ $t.find('.i-openingtime').addClass('red-bg');
+ badFormat = true;
+ }
+ if (e === false || e <= s) {
+ $t.find('.i-closingtime').addClass('red-bg');
+ badFormat = true;
+ }
+ if (badFormat) return;
+ for (var i = 0; i < allDays.length; ++i) {
+ if ($t.find('.i-' + allDays[i]).is(':checked')) {
+ entry['days'].push(allDays[i]);
+ }
+ }
+ if (entry.days.length === 0) {
+ $t.find('.days-box').addClass('red-bg');
+ badFormat = true;
+ }
+ schedule.push(entry);
+ });
+ }
+ if (badFormat) {
+ event.preventDefault();
+ }
+ $('#json-openingtimes').val(JSON.stringify(schedule));
+} \ No newline at end of file