summaryrefslogtreecommitdiffstats
path: root/modules-available/locations/clientscript.js
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/locations/clientscript.js')
-rw-r--r--modules-available/locations/clientscript.js205
1 files changed, 150 insertions, 55 deletions
diff --git a/modules-available/locations/clientscript.js b/modules-available/locations/clientscript.js
index ad3e6c43..9a434e04 100644
--- a/modules-available/locations/clientscript.js
+++ b/modules-available/locations/clientscript.js
@@ -1,66 +1,161 @@
-function ip2long(IP) {
- var i = 0;
- IP = IP.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i);
- if (!IP) {
- return false;
- }
- IP.push(0, 0, 0, 0);
- for (i = 1; i < 5; i += 1) {
- IP[i] = parseInt(IP[i]) || 0;
- if (IP[i] < 0 || IP[i] > 255)
- return false;
- }
- return IP[1] * 16777216 + IP[2] * 65536 + IP[3] * 256 + IP[4] * 1;
+/*
+ * Generic helpers.
+ */
+
+/**
+ * Initialize timepicker on given element.
+ */
+function setTimepicker($e) {
+ $e.timepicker({
+ minuteStep: 15,
+ appendWidgetTo: 'body',
+ showSeconds: false,
+ showMeridian: false,
+ defaultTime: false
+ });
}
-function long2ip(a) {
- return [
- a >>> 24,
- 255 & a >>> 16,
- 255 & a >>> 8,
- 255 & a
- ].join('.');
+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;
}
-function cidrToRange(cidr) {
- var range = [2];
- cidr = cidr.split('/');
- var cidr_1 = parseInt(cidr[1]);
- if (cidr_1 <= 0 || cidr_1 > 32)
- return false;
- var param = ip2long(cidr[0]);
- if (param === false)
- return false;
- range[0] = long2ip((param) & ((-1 << (32 - cidr_1))));
- var start = ip2long(range[0]);
- range[1] = long2ip(start + Math.pow(2, (32 - cidr_1)) - 1);
- return range;
+const allDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
+
+/*
+ * Opening times related...
+ */
+
+var slxIdCounter = 0;
+
+/**
+ * Adds a new opening time to the table in expert mode.
+ */
+function newOpeningTime($loc, vals) {
+ var $row = $loc.find('.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]).prop('checked', vals['days'].indexOf(allDays[i]) !== -1);
+ }
+ }
+ $row.find('input').each(function() {
+ var $inp = $(this);
+ if ($inp.length === 0) return;
+ slxIdCounter++;
+ $inp.prop('id', 'id-inp-' + slxIdCounter);
+ $inp.siblings('label').prop('for', 'id-inp-' + slxIdCounter);
+ });
+ $row.find('.i-openingtime').val(vals['openingtime']);
+ $row.find('.i-closingtime').val(vals['closingtime']);
+ $loc.find('.expert-table').append($row);
+ return $row;
}
/**
- * Add listener to start IP input; when it loses focus, see if we have a
- * CIDR notation and fill out start+end field.
+ * Convert fields from simple mode view to entries in expert mode.
+ * @returns {Array}
*/
-function slxAttachCidr() {
- $('.cidrmagic').each(function () {
- var t = $(this);
- var s = t.find('input.cidrstart');
- var e = t.find('input.cidrend');
- if (!s || !e)
- return;
- t.removeClass('cidrmagic');
- s.focusout(function () {
- var val = s.val();
- if (val.match(/^[0-9]+\.[0-9]+(\.[0-9]+(\.[0-9]+)?)?\/[0-9]{2}$/)) {
- var res = cidrToRange(val);
- if (res === false)
- return;
- s.val(res[0]);
- e.val(res[1]);
- }
+function simpleToExpert($form) {
+ var retval = [], $open, $close;
+ $open = $form.find('.week-open');
+ $close = $form.find('.week-close');
+ if ($open.val() || $close.val()) {
+ retval.push({
+ 'days': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
+ 'openingtime': $open.val(),
+ 'closingtime': $close.val(),
+ 'tag': 'week'
});
- });
+ }
+ $open = $form.find('.saturday-open');
+ $close = $form.find('.saturday-close');
+ if ($open.val() || $close.val()) {
+ retval.push({
+ 'days': ['Saturday'],
+ 'openingtime': $open.val(),
+ 'closingtime': $close.val(),
+ 'tag': 'saturday'
+ });
+ }
+ $open = $form.find('.sunday-open');
+ $close = $form.find('.sunday-close');
+ if ($open.val() || $close.val()) {
+ retval.push({
+ 'days': ['Sunday'],
+ 'openingtime': $open.val(),
+ 'closingtime': $close.val(),
+ 'tag': 'sunday'
+ });
+ }
+ return retval;
}
-// Attach
-slxAttachCidr(); \ No newline at end of file
+/**
+ * Triggered when the openingtimes/WOL form is submitted
+ */
+function validateOpeningTimes(event) {
+ var schedule, s, e;
+ var badFormat = false;
+ var $form = $(this);
+
+ $form.find('.red-bg').removeClass('red-bg');
+ if ($form.find('.week-open').length > 0) {
+ schedule = simpleToExpert($form);
+ for (var i = 0; i < schedule.length; ++i) {
+ s = getTime(schedule[i].openingtime);
+ e = getTime(schedule[i].closingtime);
+ if (s === false) {
+ $form.find('.' + schedule[i].tag + '-open').addClass('red-bg');
+ badFormat = true;
+ }
+ if (e === false || e <= s) {
+ $form.find('.' + schedule[i].tag + '-close').addClass('red-bg');
+ badFormat = true;
+ }
+ }
+ } else {
+ // Serialize
+ schedule = [];
+ $form.find('.expert-table .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()
+ };
+ for (var i = 0; i < allDays.length; ++i) {
+ if ($t.find('.i-' + allDays[i]).is(':checked')) {
+ entry['days'].push(allDays[i]);
+ }
+ }
+ if (entry.openingtime.length === 0 && entry.closingtime.length === 0 && entry.days.length === 0) return; // Also ignore empty lines
+ 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 (entry.days.length === 0) {
+ $t.find('.days-box').addClass('red-bg');
+ badFormat = true;
+ }
+ if (badFormat) return;
+ schedule.push(entry);
+ });
+ }
+ if (badFormat) {
+ event.preventDefault();
+ }
+ $form.find('input[name="openingtimes"]').val(JSON.stringify(schedule));
+}