*/
WCF.Poll = { };
-/**
- * Handles poll option management.
- *
- * @param string containerID
- * @param array<object> optionList
- */
-WCF.Poll.Management = Class.extend({
- /**
- * container object
- * @var jQuery
- */
- _container: null,
-
- /**
- * number of options
- * @var int
- */
- _count: 0,
-
+if (COMPILER_TARGET_DEFAULT) {
/**
- * editor element id
- * @var string
+ * Handles poll option management.
+ *
+ * @param string containerID
+ * @param array<object> optionList
*/
- _editorId: '',
-
- /**
- * maximum allowed number of options
- * @var int
- */
- _maxOptions: 0,
-
- /**
- * Initializes the WCF.Poll.Management class.
- *
- * @param {string} containerID
- * @param {Object[]} optionList
- * @param {int} maxOptions
- * @param {string} editorId
- */
- init: function(containerID, optionList, maxOptions, editorId) {
- this._count = 0;
- this._maxOptions = maxOptions || -1;
- this._container = $('#' + containerID).children('ol:eq(0)');
- if (!this._container.length) {
- console.debug("[WCF.Poll.Management] Invalid container id given, aborting.");
- return;
- }
+ WCF.Poll.Management = Class.extend({
+ /**
+ * container object
+ * @var jQuery
+ */
+ _container: null,
- optionList = optionList || [];
- this._createOptionList(optionList);
+ /**
+ * number of options
+ * @var int
+ */
+ _count: 0,
- // bind event listener
- if (editorId) {
- this._editorId = editorId;
-
- WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'reset_' + editorId, this._reset.bind(this));
- WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'submit_' + editorId, this._submit.bind(this));
- WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'validate_' + editorId, this._validate.bind(this));
- }
- else {
- this._container.closest('form').submit($.proxy(this._submit, this));
- }
+ /**
+ * editor element id
+ * @var string
+ */
+ _editorId: '',
- // init sorting
- require(['WoltLabSuite/Core/Ui/Sortable/List'], function (UiSortableList) {
- new UiSortableList({
- containerId: containerID,
- options: {
- toleranceElement: '> div'
- }
- });
- });
- },
-
- /**
- * Creates the option list on init.
- *
- * @param array<object> optionList
- */
- _createOptionList: function(optionList) {
- for (var $i = 0, $length = optionList.length; $i < $length; $i++) {
- var $option = optionList[$i];
- this._createOption($option.optionValue, $option.optionID);
- }
-
- // add an empty option, unless it would exceed the limit
- if (optionList.length < this._maxOptions) {
- this._createOption();
- }
- },
-
- /**
- * Creates a new option element.
- *
- * @param string optionValue
- * @param integer optionID
- * @param jQuery insertAfter
- */
- _createOption: function(optionValue, optionID, insertAfter) {
- optionValue = optionValue || '';
- optionID = parseInt(optionID) || 0;
- insertAfter = insertAfter || null;
+ /**
+ * maximum allowed number of options
+ * @var int
+ */
+ _maxOptions: 0,
- var $listItem = $('<li class="sortableNode" />').data('optionID', optionID);
- if (insertAfter === null) {
- $listItem.appendTo(this._container);
- }
- else {
- $listItem.insertAfter(insertAfter);
- }
-
- // insert buttons
- var $container = $('<div class="pollOptionInput" />').appendTo($listItem);
- $('<span class="icon icon16 fa-arrows sortableNodeHandle" />').appendTo($container);
- $('<span class="icon icon16 fa-plus jsTooltip jsAddOption pointer" title="' + WCF.Language.get('wcf.poll.button.addOption') + '" />').click($.proxy(this._addOption, this)).appendTo($container);
- $('<span class="icon icon16 fa-times jsTooltip jsDeleteOption pointer" title="' + WCF.Language.get('wcf.poll.button.removeOption') + '" />').click($.proxy(this._removeOption, this)).appendTo($container);
-
- // insert input field
- var $input = $('<input type="text" value="' + optionValue + '" maxlength="255" />').keydown($.proxy(this._keyDown, this)).appendTo($container);
- $input.click(function() {
- // work-around for some weird focus issue on iOS/Android
- if (document.activeElement !== this) {
- this.focus();
+ /**
+ * Initializes the WCF.Poll.Management class.
+ *
+ * @param {string} containerID
+ * @param {Object[]} optionList
+ * @param {int} maxOptions
+ * @param {string} editorId
+ */
+ init: function (containerID, optionList, maxOptions, editorId) {
+ this._count = 0;
+ this._maxOptions = maxOptions || -1;
+ this._container = $('#' + containerID).children('ol:eq(0)');
+ if (!this._container.length) {
+ console.debug("[WCF.Poll.Management] Invalid container id given, aborting.");
+ return;
}
- });
-
- if (insertAfter !== null) {
- $input.focus();
- }
-
- WCF.DOMNodeInsertedHandler.execute();
-
- this._count++;
- if (this._count === this._maxOptions) {
- this._container.find('span.jsAddOption').removeClass('pointer').addClass('disabled');
- }
- },
-
- /**
- * Handles key down events for option input field.
- *
- * @param object event
- */
- _keyDown: function(event) {
- // ignore every key except for [Enter]
- if (event.which !== 13) {
- return;
- }
-
- $(event.currentTarget).parent().children('.jsAddOption').trigger('click');
+
+ optionList = optionList || [];
+ this._createOptionList(optionList);
+
+ // bind event listener
+ if (editorId) {
+ this._editorId = editorId;
+
+ WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'reset_' + editorId, this._reset.bind(this));
+ WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'submit_' + editorId, this._submit.bind(this));
+ WCF.System.Event.addListener('com.woltlab.wcf.redactor2', 'validate_' + editorId, this._validate.bind(this));
+ }
+ else {
+ this._container.closest('form').submit($.proxy(this._submit, this));
+ }
+
+ // init sorting
+ require(['WoltLabSuite/Core/Ui/Sortable/List'], function (UiSortableList) {
+ new UiSortableList({
+ containerId: containerID,
+ options: {
+ toleranceElement: '> div'
+ }
+ });
+ });
+ },
- event.preventDefault();
- },
-
- /**
- * Adds a new option after current one.
- *
- * @param object event
- */
- _addOption: function(event) {
- if (this._count === this._maxOptions) {
- return false;
- }
+ /**
+ * Creates the option list on init.
+ *
+ * @param array<object> optionList
+ */
+ _createOptionList: function (optionList) {
+ for (var $i = 0, $length = optionList.length; $i < $length; $i++) {
+ var $option = optionList[$i];
+ this._createOption($option.optionValue, $option.optionID);
+ }
+
- // add empty option
- this._createOption();
++ // add an empty option, unless it would exceed the limit
++ if (optionList.length < this._maxOptions) {
++ this._createOption();
++ }
+ },
- var $listItem = $(event.currentTarget).closest('li', this._container[0]);
+ /**
+ * Creates a new option element.
+ *
+ * @param string optionValue
+ * @param integer optionID
+ * @param jQuery insertAfter
+ */
+ _createOption: function (optionValue, optionID, insertAfter) {
+ optionValue = optionValue || '';
+ optionID = parseInt(optionID) || 0;
+ insertAfter = insertAfter || null;
+
+ var $listItem = $('<li class="sortableNode" />').data('optionID', optionID);
+ if (insertAfter === null) {
+ $listItem.appendTo(this._container);
+ }
+ else {
+ $listItem.insertAfter(insertAfter);
+ }
+
+ // insert buttons
+ var $container = $('<div class="pollOptionInput" />').appendTo($listItem);
+ $('<span class="icon icon16 fa-arrows sortableNodeHandle" />').appendTo($container);
+ $('<span class="icon icon16 fa-plus jsTooltip jsAddOption pointer" title="' + WCF.Language.get('wcf.poll.button.addOption') + '" />').click($.proxy(this._addOption, this)).appendTo($container);
+ $('<span class="icon icon16 fa-times jsTooltip jsDeleteOption pointer" title="' + WCF.Language.get('wcf.poll.button.removeOption') + '" />').click($.proxy(this._removeOption, this)).appendTo($container);
+
+ // insert input field
+ var $input = $('<input type="text" value="' + optionValue + '" maxlength="255" />').keydown($.proxy(this._keyDown, this)).appendTo($container);
+ $input.click(function () {
+ // work-around for some weird focus issue on iOS/Android
+ if (document.activeElement !== this) {
+ this.focus();
+ }
+ });
+
+ if (insertAfter !== null) {
+ $input.focus();
+ }
+
+ WCF.DOMNodeInsertedHandler.execute();
+
+ this._count++;
+ if (this._count === this._maxOptions) {
+ this._container.find('span.jsAddOption').removeClass('pointer').addClass('disabled');
+ }
+ },
- this._createOption(undefined, undefined, $listItem);
- },
-
- /**
- * Removes an option.
- *
- * @param object event
- */
- _removeOption: function(event) {
- $(event.currentTarget).closest('li', this._container[0]).remove();
+ /**
+ * Handles key down events for option input field.
+ *
+ * @param object event
+ */
+ _keyDown: function (event) {
+ // ignore every key except for [Enter]
+ if (event.which !== 13) {
+ return;
+ }
+
+ $(event.currentTarget).parent().children('.jsAddOption').trigger('click');
+
+ event.preventDefault();
+ },
- this._count--;
- this._container.find('span.jsAddOption').addClass('pointer').removeClass('disabled');
+ /**
+ * Adds a new option after current one.
+ *
+ * @param object event
+ */
+ _addOption: function (event) {
+ if (this._count === this._maxOptions) {
+ return false;
+ }
+
+ var $listItem = $(event.currentTarget).closest('li', this._container[0]);
+
+ this._createOption(undefined, undefined, $listItem);
+ },
- if (this._container.children('li').length == 0) {
- this._createOption();
- }
- },
-
- /**
- * Inserts hidden input elements storing the option values.
- *
- * @param {(Event|Object)} event
- */
- _submit: function(event) {
- var $options = [];
- this._container.children('li').each(function (index, listItem) {
- var $listItem = $(listItem);
- var $optionValue = $.trim($listItem.find('input').val());
+ /**
+ * Removes an option.
+ *
+ * @param object event
+ */
+ _removeOption: function (event) {
+ $(event.currentTarget).closest('li', this._container[0]).remove();
- // ignore empty values
- if ($optionValue != '') {
- $options.push($listItem.data('optionID') + '_' + $optionValue);
+ this._count--;
+ this._container.find('span.jsAddOption').addClass('pointer').removeClass('disabled');
+
+ if (this._container.children('li').length == 0) {
+ this._createOption();
}
- });
+ },
- if (typeof event.originalEvent === 'object' && event.originalEvent instanceof Event) {
- // create hidden input fields
- if ($options.length) {
- var $formSubmit = this._container.parents('form').find('.formSubmit');
+ /**
+ * Inserts hidden input elements storing the option values.
+ *
+ * @param {(Event|Object)} event
+ */
+ _submit: function (event) {
+ var $options = [];
+ this._container.children('li').each(function (index, listItem) {
+ var $listItem = $(listItem);
+ var $optionValue = $.trim($listItem.find('input').val());
- for (var $i = 0, $length = $options.length; $i < $length; $i++) {
- $('<input type="hidden" name="pollOptions[' + $i + ']">').val($options[$i]).appendTo($formSubmit);
+ // ignore empty values
+ if ($optionValue != '') {
+ $options.push($listItem.data('optionID') + '_' + $optionValue);
+ }
+ });
+
+ if (typeof event.originalEvent === 'object' && event.originalEvent instanceof Event) {
+ // create hidden input fields
+ if ($options.length) {
+ var $formSubmit = this._container.parents('form').find('.formSubmit');
+
+ for (var $i = 0, $length = $options.length; $i < $length; $i++) {
+ $('<input type="hidden" name="pollOptions[' + $i + ']">').val($options[$i]).appendTo($formSubmit);
+ }
}
}
- }
- else {
- event.poll = { pollOptions: $options };
+ else {
+ event.poll = {pollOptions: $options};
+
+ // get form input fields
+ var parentContainer = this._container.parents('.messageTabMenuContent:eq(0)');
+ parentContainer.find('input').each(function (index, input) {
+ if (input.name) {
+ if (input.type !== 'checkbox' || input.checked) {
+ event.poll[input.name] = input.value;
+ }
+ }
+ });
+ }
+ },
+
+ /**
+ * Resets the poll option form.
+ *
+ * @private
+ */
+ _reset: function () {
+ // reset options
+ /** @type Element */
+ var container = this._container[0];
+ while (container.childElementCount > 1) {
+ container.removeChild(container.children[1]);
+ }
+
+ elBySel('input', container.children[0]).value = '';
- // get form input fields
+ // reset input fields and checkboxes
var parentContainer = this._container.parents('.messageTabMenuContent:eq(0)');
- parentContainer.find('input').each(function(index, input) {
+ parentContainer.find('input').each(function (index, input) {
if (input.name) {
- if (input.type !== 'checkbox' || input.checked) {
- event.poll[input.name] = input.value;
+ if (input.type === 'checkbox') {
+ input.checked = false;
+ }
+ else if (input.type === 'text') {
+ input.value = '';
+ }
+ else if (input.type === 'number') {
+ input.value = input.min;
}
}
});