Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 1 | define(['menu/item', 'util'], function (itemClass) { |
| 2 | |
| 3 | var loc = KorAP.Locale; |
| 4 | |
| 5 | return { |
| 6 | |
| 7 | /** |
| 8 | * Create new menu item. |
| 9 | * Pass two parameters: value and type. |
| 10 | * the value may be localized by a name in |
| 11 | * KorAP.Locale with the prefix 'VC_', |
| 12 | * e.g. 'VC_subTitle'. |
| 13 | */ |
| 14 | create : function (params) { |
| 15 | return Object.create(itemClass) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame^] | 16 | .upgradeTo(this) |
| 17 | ._init(params); |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 18 | }, |
| 19 | |
| 20 | // Initialize item object |
| 21 | _init : function (params) { |
| 22 | if (params[0] === undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame^] | 23 | throw new Error("Missing parameters"); |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 24 | |
| 25 | this._id = params[0]; |
| 26 | this._name = params[1]; |
| 27 | this._desc = params[2]; |
| 28 | |
| 29 | this._lcField = ' ' + this._name.toLowerCase(); |
| 30 | this._lcField += ' ' + this._desc.toLowerCase(); |
| 31 | |
| 32 | return this; |
| 33 | }, |
| 34 | |
| 35 | /** |
| 36 | * Override click event by passing all clicks |
| 37 | * to the menu object. |
| 38 | */ |
| 39 | onclick : function (e) { |
| 40 | this.menu().release( |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame^] | 41 | this._id, |
| 42 | this._name |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 43 | ); |
| 44 | e.halt(); |
| 45 | }, |
| 46 | |
| 47 | /** |
| 48 | * Get the name of the item. |
| 49 | */ |
| 50 | name : function () { |
| 51 | return this._name; |
| 52 | }, |
| 53 | |
| 54 | /** |
| 55 | * Get the identifier of the item. |
| 56 | */ |
| 57 | id : function () { |
| 58 | return this._id; |
| 59 | }, |
| 60 | |
| 61 | /** |
| 62 | * Get the description of the item. |
| 63 | */ |
| 64 | desc : function () { |
| 65 | return this._desc; |
| 66 | }, |
| 67 | |
| 68 | /** |
| 69 | * Get the HTML element associated with the item. |
| 70 | */ |
| 71 | element : function () { |
| 72 | |
| 73 | // already defined |
| 74 | if (this._element !== undefined) |
Akron | e4961b1 | 2017-05-10 21:04:46 +0200 | [diff] [blame^] | 75 | return this._element; |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 76 | |
| 77 | // Create list item |
| 78 | var li = document.createElement("li"); |
| 79 | li.setAttribute("data-type", this._type); |
| 80 | li.setAttribute("data-key", this._key); |
| 81 | |
| 82 | // Connect action |
| 83 | li["onclick"] = this.onclick.bind(this); |
| 84 | |
Akron | 1ff3ac2 | 2016-04-28 16:30:45 +0200 | [diff] [blame] | 85 | li.appendChild(document.createTextNode(this._name)); |
Akron | 48b1e4d | 2015-06-17 18:47:01 +0200 | [diff] [blame] | 86 | return this._element = li; |
| 87 | } |
| 88 | } |
| 89 | }); |