blob: d8db4cdac228231c8de6bb766ae43a6a516b219a [file] [log] [blame]
Akronad894832016-06-08 18:24:48 +02001define(
Akronc82513b2016-06-11 11:22:36 +02002 ['menu', 'selectMenu/item'],
3 function (menuClass, selectMenuItemClass) {
Akronad894832016-06-08 18:24:48 +02004
5 return {
6 create : function (element) {
Akronad894832016-06-08 18:24:48 +02007
Akronaba7a5a2016-08-15 21:58:33 +02008 var select = element.getElementsByTagName('select')[0];
Akronad894832016-06-08 18:24:48 +02009
Akronaba7a5a2016-08-15 21:58:33 +020010 if (select === null)
11 return;
Akronad894832016-06-08 18:24:48 +020012
Akronaba7a5a2016-08-15 21:58:33 +020013 // Prepare list before object upgras
14 var list = [];
15 var options = select.getElementsByTagName('option');
Akronad894832016-06-08 18:24:48 +020016
Akronaba7a5a2016-08-15 21:58:33 +020017 // Iterate through options list
18 for (var i = 0; i < options.length; i++) {
Akronad894832016-06-08 18:24:48 +020019
Akronaba7a5a2016-08-15 21:58:33 +020020 // Get option item and add to list
21 var item = options.item(i);
22 var opt = [
23 item.textContent,
24 item.getAttribute('value')
25 ];
Akronad894832016-06-08 18:24:48 +020026
Akronaba7a5a2016-08-15 21:58:33 +020027 // If the item has an attribute - list
28 if (item.hasAttribute('desc'))
29 opt.push(item.getAttribute('desc'));
Akronad894832016-06-08 18:24:48 +020030
Akronaba7a5a2016-08-15 21:58:33 +020031 list.push(opt);
32 };
Akronad894832016-06-08 18:24:48 +020033
Akronaba7a5a2016-08-15 21:58:33 +020034 // Create object with list
35 var obj = Object.create(menuClass).upgradeTo(this)
36 ._init(list, {
37 itemClass : selectMenuItemClass
38 });
Akronad894832016-06-08 18:24:48 +020039
Akronaba7a5a2016-08-15 21:58:33 +020040 obj._container = element;
41 obj._select = select;
42 obj._select.style.display = 'none';
Akronad894832016-06-08 18:24:48 +020043
Akronaba7a5a2016-08-15 21:58:33 +020044 // Create title
45 obj._title = obj._container.appendChild(document.createElement('span'));
46 obj._title.appendChild(document.createTextNode(''));
47 obj._container.appendChild(obj.element());
Akronad894832016-06-08 18:24:48 +020048
Akronaba7a5a2016-08-15 21:58:33 +020049 // Show the menu
50 obj._container.addEventListener('click', obj.showSelected.bind(obj));
Akron6bb71582016-06-10 20:41:08 +020051
Akronaba7a5a2016-08-15 21:58:33 +020052 // Add index information to each item
53 for (i in obj._items) {
54 obj._items[i]._index = i;
55 };
Akron6bb71582016-06-10 20:41:08 +020056
Akronaba7a5a2016-08-15 21:58:33 +020057 // This is only domspecific
58 obj.element().addEventListener('blur', function (e) {
59 this.menu.hide();
60 this.menu.showTitle();
61 });
Akron6bb71582016-06-10 20:41:08 +020062
Akronaba7a5a2016-08-15 21:58:33 +020063 // In case another tool changes
64 // the option via JS - this needs
65 // to be reflected!
66 select.addEventListener('change', function (e) {
67 this.showTitle();
68 }.bind(obj));
Akron6bb71582016-06-10 20:41:08 +020069
Akronaba7a5a2016-08-15 21:58:33 +020070 // Show the title
71 obj.showTitle();
72 return obj;
Akron6bb71582016-06-10 20:41:08 +020073 },
74
Akronaba7a5a2016-08-15 21:58:33 +020075 /**
76 * Get or set the selection index
77 */
Akron6bb71582016-06-10 20:41:08 +020078 select : function (index) {
Akronaba7a5a2016-08-15 21:58:33 +020079 if (arguments.length > 0) {
80 this._selected = index;
81 this._select.selectedIndex = index;
82 };
Akron6bb71582016-06-10 20:41:08 +020083
Akronaba7a5a2016-08-15 21:58:33 +020084 return this._selected || 0;
Akron6bb71582016-06-10 20:41:08 +020085 },
86
Akronaba7a5a2016-08-15 21:58:33 +020087 /**
88 * Show the select menu
89 */
Akron6bb71582016-06-10 20:41:08 +020090 showSelected : function () {
Akronaba7a5a2016-08-15 21:58:33 +020091 this._title.style.display = 'none';
92 this._selected = this._select.selectedIndex;
93 this.show(this._selected);
94 this.focus();
Akron6bb71582016-06-10 20:41:08 +020095 },
96
Akronaba7a5a2016-08-15 21:58:33 +020097 /**
98 * Show the title
99 */
Akron6bb71582016-06-10 20:41:08 +0200100 showTitle : function () {
Akronaba7a5a2016-08-15 21:58:33 +0200101
102 // Get the selection context
103 var s = this.select();
104 this._title.textContent = this.item(
105 this.select()
106 ).title();
107 this._title.style.display = 'inline';
Akronad894832016-06-08 18:24:48 +0200108 }
109 }
110 }
111);