summaryrefslogtreecommitdiff
path: root/js/form/Select.js
blob: 0c73cd52cc2c059bc51ea508c723ef21d8fc1b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* eslint-disable prefer-rest-params */
/* global define */
// FIXME: there probably is a better, more dojo-like notation for custom data- properties
define(["dojo/_base/declare",
	"dijit/form/Select",
	"dojo/_base/lang", // lang.hitch
	"dijit/MenuItem",
	"dijit/MenuSeparator",
	"dojo/aspect",
	], function (declare, select, lang, MenuItem, MenuSeparator, aspect) {
	return declare("fox.form.Select", select, {
		focus: function() {
			return; // Stop dijit.form.Select from keeping focus after closing the menu
		},
		startup: function() {
         this.inherited(arguments);

			if (this.attr('data-dropdown-skip-first') == 'true') {
				aspect.before(this, "_loadChildren", () => {
					this.options = this.options.splice(1);
				});
			}
		},
		// hook invoked when dropdown MenuItem is clicked
		onItemClick: function(/*item, menu*/) {
			//
		},
		_setValueAttr: function(/*anything*/ newValue, /*Boolean?*/ priorityChange){
			if (this.attr('data-prevent-value-change') == 'true' && newValue != '')
				return;

			this.inherited(arguments);
		},
		// the only difference from dijit/form/Select is _onItemClicked() handler
		_getMenuItemForOption: function(/*_FormSelectWidget.__SelectOption*/ option){
         // summary:
         //    For the given option, return the menu item that should be
         //    used to display it.  This can be overridden as needed
         if (!option.value && !option.label){
            // We are a separator (no label set for it)
            return new MenuSeparator({ownerDocument: this.ownerDocument});
         } else {
            // Just a regular menu option
            const click = lang.hitch(this, "_setValueAttr", option);
            const item = new MenuItem({
               option: option,
               label: (this.labelType === 'text' ? (option.label || '').toString()
                  .replace(/&/g, '&amp;').replace(/</g, '&lt;') :
                  option.label) || this.emptyLabel,
               onClick: () => {
						this.onItemClick(item, this.dropDown);

						click();
					},
               ownerDocument: this.ownerDocument,
               dir: this.dir,
               textDir: this.textDir,
               disabled: option.disabled || false
            });
            item.focusNode.setAttribute("role", "option");

            return item;
         }
      },
	});
});