123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 'use strict';
- var escapeHTML = require('../Utils').escapeHTML;
- var domify = require('min-dom').domify;
- var forEach = require('lodash/forEach');
- var entryFieldDescription = require('./EntryFieldDescription');
- var isList = function(list) {
- return !(!list || Object.prototype.toString.call(list) !== '[object Array]');
- };
- var addEmptyParameter = function(list) {
- return list.concat([ { name: '', value: '' } ]);
- };
- var createOption = function(option) {
- return '<option value="' + option.value + '">' + option.name + '</option>';
- };
- /**
- * @param {Object} options
- * @param {string} options.id
- * @param {string} [options.label]
- * @param {Array<Object>} options.selectOptions
- * @param {string} options.modelProperty
- * @param {boolean} options.emptyParameter
- * @param {function} options.disabled
- * @param {function} options.hidden
- * @param {Object} defaultParameters
- *
- * @return {Object}
- */
- var selectbox = function(options, defaultParameters) {
- var resource = defaultParameters,
- label = options.label || resource.id,
- selectOptions = options.selectOptions || [ { name: '', value: '' } ],
- modelProperty = options.modelProperty,
- emptyParameter = options.emptyParameter,
- canBeDisabled = !!options.disabled && typeof options.disabled === 'function',
- canBeHidden = !!options.hidden && typeof options.hidden === 'function',
- description = options.description;
- if (emptyParameter) {
- selectOptions = addEmptyParameter(selectOptions);
- }
- resource.html =
- '<label for="activiti-' + escapeHTML(resource.id) + '"' +
- (canBeDisabled ? 'data-disable="isDisabled" ' : '') +
- (canBeHidden ? 'data-show="isHidden" ' : '') +
- '>' + escapeHTML(label) + '</label>' +
- '<select id="activiti-' + escapeHTML(resource.id) + '-select" name="' +
- escapeHTML(modelProperty) + '"' +
- (canBeDisabled ? 'data-disable="isDisabled" ' : '') +
- (canBeHidden ? 'data-show="isHidden" ' : '') +
- ' data-value>';
- if (isList(selectOptions)) {
- forEach(selectOptions, function(option) {
- resource.html += '<option value="' + escapeHTML(option.value) + '">' +
- (option.name ? escapeHTML(option.name) : '') + '</option>';
- });
- }
- resource.html += '</select>';
- // add description below select box entry field
- if (description && typeof options.showCustomInput !== 'function') {
- resource.html += entryFieldDescription(description);
- }
- /**
- * Fill the select box options dynamically.
- *
- * Calls the defined function #selectOptions in the entry to get the
- * values for the options and set the value to the inputNode.
- *
- * @param {djs.model.Base} element
- * @param {HTMLElement} entryNode
- * @param {EntryDescriptor} inputNode
- * @param {Object} inputName
- * @param {Object} newValue
- */
- resource.setControlValue = function(element, entryNode, inputNode, inputName, newValue) {
- if (typeof selectOptions === 'function') {
- var options = selectOptions(element, inputNode);
- if (options) {
- // remove existing options
- while (inputNode.firstChild) {
- inputNode.removeChild(inputNode.firstChild);
- }
- // add options
- forEach(options, function(option) {
- var template = domify(createOption(option));
- inputNode.appendChild(template);
- });
- }
- }
- // set select value
- if (newValue !== undefined) {
- inputNode.value = newValue;
- }
- };
- if (canBeDisabled) {
- resource.isDisabled = function() {
- return options.disabled.apply(resource, arguments);
- };
- }
- if (canBeHidden) {
- resource.isHidden = function() {
- return !options.hidden.apply(resource, arguments);
- };
- }
- resource.cssClasses = ['bpp-dropdown'];
- return resource;
- };
- module.exports = selectbox;
|