Load the timezones via select2

This commit is contained in:
Joas Schilling 2016-08-01 17:05:40 +02:00
parent 2734ff7d4e
commit ea4c6bd285
No known key found for this signature in database
GPG Key ID: E166FD8976B3BAC8
6 changed files with 129 additions and 21 deletions

View File

@ -25,5 +25,6 @@ return [
['name' => 'flowOperations#addOperation', 'url' => '/operations', 'verb' => 'POST'], ['name' => 'flowOperations#addOperation', 'url' => '/operations', 'verb' => 'POST'],
['name' => 'flowOperations#updateOperation', 'url' => '/operations/{id}', 'verb' => 'PUT'], ['name' => 'flowOperations#updateOperation', 'url' => '/operations/{id}', 'verb' => 'PUT'],
['name' => 'flowOperations#deleteOperation', 'url' => '/operations/{id}', 'verb' => 'DELETE'], ['name' => 'flowOperations#deleteOperation', 'url' => '/operations/{id}', 'verb' => 'DELETE'],
['name' => 'requestTime#getTimezones', 'url' => '/timezones', 'verb' => 'GET'],
] ]
]; ];

View File

@ -308,7 +308,6 @@
}, 7000, this.$el.find('.msg.success')); }, 7000, this.$el.find('.msg.success'));
this.message = ''; this.message = '';
} }
} }
}); });

View File

@ -44,10 +44,11 @@
return; return;
} }
var startTime = '16:00', var startTime = '09:00',
endTime = '18:00', endTime = '18:00',
timezone = 'Europe/London', timezone = jstz.determine().name(),
$element = $(element); $element = $(element);
if (_.isString(check['value']) && check['value'] !== '') { if (_.isString(check['value']) && check['value'] !== '') {
var value = JSON.parse(check['value']), var value = JSON.parse(check['value']),
splittedStart = value[0].split(' ', 2), splittedStart = value[0].split(' ', 2),
@ -57,6 +58,7 @@
endTime = splittedEnd[0]; endTime = splittedEnd[0];
timezone = splittedStart[1]; timezone = splittedStart[1];
} }
var valueJSON = JSON.stringify([startTime + ' ' + timezone, endTime + ' ' + timezone]); var valueJSON = JSON.stringify([startTime + ' ' + timezone, endTime + ' ' + timezone]);
if (check['value'] !== valueJSON) { if (check['value'] !== valueJSON) {
check['value'] = valueJSON; check['value'] = valueJSON;
@ -68,28 +70,78 @@
$('<input>') $('<input>')
.attr('type', 'text') .attr('type', 'text')
.attr('placeholder', t('workflowengine', 'Start')) .attr('placeholder', t('workflowengine', 'Start'))
.attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: '16:00'}))
.addClass('has-tooltip')
.tooltip({
placement: 'bottom'
})
.addClass('start') .addClass('start')
.val(startTime) .val(startTime)
.insertBefore($element); .insertBefore($element);
$('<input>') $('<input>')
.attr('type', 'text') .attr('type', 'text')
.attr('placeholder', t('workflowengine', 'End')) .attr('placeholder', t('workflowengine', 'End'))
.attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: '16:00'}))
.addClass('has-tooltip')
.tooltip({
placement: 'bottom'
})
.addClass('end') .addClass('end')
.val(endTime) .val(endTime)
.insertBefore($element); .insertBefore($element);
var timezoneSelect = $('<select>') var timezoneInput = $('<input>')
.addClass('timezone'); .attr('type', 'hidden')
_.each(this.timezones, function(timezoneName){ .css('width', '250px')
var timezoneElement = $('<option>').val(timezoneName).html(timezoneName); .insertBefore($element)
.val(timezone);
if (timezoneName === timezone) { timezoneInput.select2({
timezoneElement.attr('selected', 'selected'); allowClear: false,
multiple: false,
placeholder: t('workflowengine', 'Select timezone…'),
ajax: {
url: OC.generateUrl('apps/workflowengine/timezones'),
dataType: 'json',
quietMillis: 100,
data: function (term) {
if (term === '') {
// Default search in the same continent...
term = jstz.determine().name().split('/');
term = term[0];
} }
return {
timezoneSelect.append(timezoneElement); search: term
};
},
results: function (response) {
var results = [];
$.each(response, function(timezone) {
results.push({ id: timezone });
}); });
timezoneSelect.insertBefore($element);
return {
results: results,
more: false
};
}
},
initSelection: function (element, callback) {
callback(element.val());
},
formatResult: function (element) {
return '<span>' + element.id + '</span>';
},
formatSelection: function (element) {
if (!_.isUndefined(element.id)) {
element = element.id;
}
return '<span>' + element + '</span>';
}
});
// Has to be added after select2 for `event.target.classList`
timezoneInput.addClass('timezone');
$element.parent() $element.parent()
.on('change', '.start', _.bind(this.update, this)) .on('change', '.start', _.bind(this.update, this))
@ -136,6 +188,7 @@
} }
this._$element.val(JSON.stringify(data)); this._$element.val(JSON.stringify(data));
this._$element.trigger('change');
} }
}; };
})(); })();

View File

@ -30,6 +30,7 @@ class Application extends \OCP\AppFramework\App {
parent::__construct('workflowengine'); parent::__construct('workflowengine');
$this->getContainer()->registerAlias('FlowOperationsController', 'OCA\WorkflowEngine\Controller\FlowOperations'); $this->getContainer()->registerAlias('FlowOperationsController', 'OCA\WorkflowEngine\Controller\FlowOperations');
$this->getContainer()->registerAlias('RequestTimeController', 'OCA\WorkflowEngine\Controller\RequestTime');
} }
/** /**
@ -51,6 +52,8 @@ class Application extends \OCP\AppFramework\App {
'systemtags/systemtagscollection', 'systemtags/systemtagscollection',
]); ]);
vendor_script('jsTimezoneDetect/jstz');
script('workflowengine', [ script('workflowengine', [
'admin', 'admin',

View File

@ -29,7 +29,7 @@ use OCP\WorkflowEngine\ICheck;
class RequestTime implements ICheck { class RequestTime implements ICheck {
const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])'; const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])';
const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\\\\\/[a-zA-Z\-\_]+)+)'; const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)';
/** @var bool[] */ /** @var bool[] */
protected $cachedResults; protected $cachedResults;
@ -110,16 +110,15 @@ class RequestTime implements ICheck {
throw new \UnexpectedValueException('Invalid time limits', 2); throw new \UnexpectedValueException('Invalid time limits', 2);
} }
try { $values = json_decode($value, true);
new \DateTimeZone(stripslashes($matches[3])); $time1 = \DateTime::createFromFormat('H:i e', $values[0]);
} catch(\Exception $e) { if ($time1 === false) {
throw new \UnexpectedValueException('Invalid timezone1', 3); throw new \UnexpectedValueException('Invalid start time given', 3);
} }
try { $time2 = \DateTime::createFromFormat('H:i e', $values[1]);
new \DateTimeZone(stripslashes($matches[6])); if ($time2 === false) {
} catch(\Exception $e) { throw new \UnexpectedValueException('Invalid end time given', 3);
throw new \UnexpectedValueException('Invalid timezone2', 3);
} }
} }
} }

View File

@ -0,0 +1,53 @@
<?php
/**
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\WorkflowEngine\Controller;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
class RequestTime extends Controller {
/**
* @NoCSRFRequired
* @NoAdminRequired
*
* @param string $search
* @return JSONResponse
*/
public function getTimezones($search = '') {
$timezones = \DateTimeZone::listIdentifiers();
if ($search !== '') {
$timezones = array_filter($timezones, function ($timezone) use ($search) {
return strpos(strtolower($timezone), strtolower($search)) !== false;
});
}
$timezones = array_slice($timezones, 0, 10);
$response = [];
foreach ($timezones as $timezone) {
$response[$timezone] = $timezone;
}
return new JSONResponse($response);
}
}