PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/js/jQuery-contextMenu/documentation/docs/items.md

https://gitlab.com/abhijit13/taasika
Markdown | 515 lines | 393 code | 122 blank | 0 comment | 0 complexity | 29781b0b1adedf83e043b39521f011bd MD5 | raw file
  1. ---
  2. currentMenu: items
  3. ---
  4. # Items
  5. <!-- START doctoc generated TOC please keep comment here to allow auto update -->
  6. <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
  7. - [options.items](#optionsitems)
  8. - [name](#name)
  9. - [callback](#callback)
  10. - [className](#classname)
  11. - [icon](#icon)
  12. - [disabled](#disabled)
  13. - [visible](#visible)
  14. - [type](#type)
  15. - [events](#events)
  16. - [value](#value)
  17. - [selected](#selected)
  18. - [radio](#radio)
  19. - [options](#options)
  20. - [height](#height)
  21. - [items](#items)
  22. - [accesskey](#accesskey)
  23. <!-- END doctoc generated TOC please keep comment here to allow auto update -->
  24. The items map contains the commands to list in the menu. Each command has a unique key identifying an item object. The value may either be an item (properties explained below), or a string (which will insert a separator, disregarding the string's content). It is also possible to define a seperator the same as an item, and use the `type`:`cm_separator` to define it.
  25. ```javascript
  26. var items = {
  27. firstCommand: itemOptions,
  28. separator1: "-----",
  29. separator2: { "type": "cm_separator" },
  30. command2: itemOptions
  31. }
  32. ```
  33. Since 2.3 it is also possible to use a promise as item, so you can build submenu's based on a snynchronous promis.
  34. Check out the [demo using a promise](demo/async-promise.md) for an example how to use this. The example uses jQuery deferred, but any promise should do. Promised can only be used in combination with the [build option](docs#build).
  35. ## options.items
  36. ### name
  37. Specify the human readable name of the command in the menu. This is used as the label for the option.
  38. `name`: `string`
  39. #### Example
  40. ```javascript
  41. var items = {
  42. firstCommand: {
  43. name: "Copy"
  44. }
  45. }
  46. ```
  47. ### isHtmlName
  48. When truthy, the defined `name` value is HTML.
  49. The value will be rendered using `$.html()` instead of `$.text()`.
  50. __Note: Cannot be used with the [accesskey](#accesskey) option in the same item.__
  51. `isHtmlName`: `boolean`
  52. #### Example
  53. ```javascript
  54. var items = {
  55. firstCommand: {
  56. name: "Copy <span style='font-weight: bold'>Text</span>",
  57. isHtmlName: true
  58. }
  59. }
  60. ```
  61. ### callback
  62. Specifies the callback to execute if clicked on
  63. The Callback is executed in the context of the triggering object. The first argument is the key of the command. The second argument is the options object. The Callback may return false to prevent the menu from being hidden.
  64. If no callback and no default callback is specified, the item will not have an action
  65. `callback`: `function(itemKey, opt, rootMenu, originalEvent)`
  66. #### Example
  67. ```javascript
  68. var items = {
  69. firstCommand: {
  70. name: "Copy",
  71. callback: function(itemKey, opt, rootMenu, originalEvent){
  72. // Alert the key of the item and the trigger element's id.
  73. alert("Clicked on " + itemKey + " on element " + opt.$trigger.id);
  74. // Do not close the menu after clicking an item
  75. return false;
  76. }
  77. }
  78. }
  79. ```
  80. ### className
  81. Specifies additional classNames to add to the menu item. Seperate multiple classes by using spaces.
  82. `className`: `string`
  83. #### Example
  84. ```javascript
  85. var items = {
  86. firstCommand: {
  87. name: "Copy",
  88. className: 'contextmenu-item-custom contextmenu-item-custom__highlight'
  89. }
  90. }
  91. ```
  92. ### icon
  93. Specifies the icon class to set for the item.
  94. When using a string icons must be defined in CSS with selectors like `.context-menu-item.context-menu-icon-edit`, where `edit` is the icon class specified.
  95. When using a callback you can return a class string to use that as the class on the item. You can also modify the element by using the `$itemElement` argument.
  96. `icon`: `string` or `function(opt, $itemElement, itemKey, item)`
  97. #### Example
  98. ```javascript
  99. var items = {
  100. firstCommand: {
  101. name: "Copy",
  102. icon: function(opt, $itemElement, itemKey, item){
  103. // Set the content to the menu trigger selector and add an bootstrap icon to the item.
  104. $itemElement.html('<span class="glyphicon glyphicon-star" aria-hidden="true"></span> ' + opt.selector);
  105. // Add the context-menu-icon-updated class to the item
  106. return 'context-menu-icon-updated';
  107. }
  108. },
  109. secondCommand: {
  110. name: "Paste",
  111. icon: "paste" // Class context-menu-icon-paste is used on the menu item.
  112. }
  113. }
  114. ```
  115. ### disabled
  116. <!-- @todo options object -->
  117. Specifies if the command is disabled (`true`) or enabled (`false`).
  118. May be a callback returning a `boolean`. The callback is executed in the context of the triggering object (so this inside the function refers to the element the context menu was shown for). The first argument is the `key` of the command. The second argument is the `options object`.
  119. `disabled`: `boolean` or `function(itemKey, opt)`
  120. #### Example
  121. ```javascript
  122. var items = {
  123. firstCommand: {
  124. name: "Copy",
  125. disabled: function(key, opt){
  126. // Disable this item if the menu was triggered on a div
  127. if(opt.$trigger.nodeName === 'div'){
  128. return true;
  129. }
  130. }
  131. }
  132. }
  133. ```
  134. ### visible
  135. <!-- @todo options object -->
  136. Specifies if the command is visible (`true`) or not (`false`).
  137. May be a callback returning a boolean. The callback is executed in the context of the triggering object (so this inside the function refers to the element the context menu was shown for). The first argument is the key of the command. The second argument is the `options object`.
  138. `visible`: `boolean` or `function(itemKey, opt)`
  139. #### Example
  140. ```javascript
  141. var items = {
  142. firstCommand: {
  143. name: "Copy",
  144. visible: function(key, opt){
  145. // Hide this item if the menu was triggered on a div
  146. if(opt.$trigger.nodeName === 'div'){
  147. return false;
  148. }
  149. }
  150. }
  151. }
  152. ```
  153. ### type
  154. Specifies the type of the command.
  155. `type`: `null`, `undefined`, `text`, `textarea`, `checkbox`, `radio`, `select`, `html` default: `null`
  156. Value | Description
  157. ---- | ----
  158. `null`, `undefined` , `""` | The command is a simple clickable item.
  159. `"text"` | Makes the command an `<input>` of type `text`.<br>The name followed by the `<input>` are encapsulated in a `<label>`.
  160. `"textarea"` | Makes the command a `<textarea>`. <br>The name followed by the `<input>` are encapsulated in a `<label>`.
  161. `"checkbox"` | Makes the command an `<input>` of type checkbox. <br>The name preceeded by the `<input>` are encapsulated in a `<label>`. <br>The checkbox-element is moved to the icon space
  162. `"radio"` | Makes the command an `<input>` of type radio. <br>The name preceeded by the `<input>` are encapsulated in a `<label>`. <br>The radio-element is moved to the icon space
  163. `"select"` | Makes the command a `<select>`. <br>The name followed by the `<select>` are encapsulated in a `<label>`.
  164. `"html"` | Makes an non-command element. When you select `type: 'html'` add the html to the `html` property. So: `{ item: { type: 'html', html: '<span>html!</span>' } }`. You can also just use the item name with the [`isHtmlName`](isHtmlName) property.
  165. #### Example
  166. ```javascript
  167. $.contextMenu({
  168. selector: 'span.context-menu',
  169. items: {
  170. name: {
  171. name: "Text",
  172. type: 'text',
  173. value: "Hello World",
  174. events: {
  175. keyup: function(e) {
  176. // add some fancy key handling here?
  177. window.console && console.log('key: '+ e.keyCode);
  178. }
  179. }
  180. },
  181. sep1: "---------",
  182. // <input type="checkbox">
  183. yesno: {
  184. name: "Boolean",
  185. type: 'checkbox',
  186. selected: true
  187. },
  188. sep2: "---------",
  189. // <input type="radio">
  190. radio1: {
  191. name: "Radio1",
  192. type: 'radio',
  193. radio: 'radio',
  194. value: '1'
  195. },
  196. radio2: {
  197. name: "Radio2",
  198. type: 'radio',
  199. radio: 'radio',
  200. value: '2',
  201. selected: true
  202. },
  203. sep3: "---------",
  204. // <select>
  205. select: {
  206. name: "Select",
  207. type: 'select',
  208. options: {1: 'one', 2: 'two', 3: 'three'},
  209. selected: 2
  210. },
  211. // <textarea>
  212. area1: {
  213. name: "Textarea with height",
  214. type: 'textarea',
  215. value: "Hello World",
  216. height: 40
  217. },
  218. area2: {
  219. name: "Textarea",
  220. type: 'textarea',
  221. value: "Hello World"
  222. },
  223. sep4: "---------",
  224. key: {
  225. name: "Something Clickable",
  226. callback: $.noop
  227. }
  228. }
  229. });
  230. ```
  231. ### events
  232. Events to register on `<input>` elements. The contents of the options object are passed to jQuery event.data.
  233. __Only used with [types](#type) `text`, `textarea`, `radio`, `checkbox` and `select`.__
  234. `events`: `object`
  235. #### Example
  236. ```javascript
  237. $.contextMenu({
  238. selector: 'span.context-menu',
  239. events: {
  240. command1: {
  241. name: "Foobar",
  242. type: "text",
  243. events: {
  244. keyup: function(e){
  245. alert(e.keyCode);
  246. alert(e.data.$trigger.attr("id"));
  247. }
  248. }
  249. }
  250. }
  251. });
  252. ```
  253. ### value
  254. The value of the `<input>` element.
  255. __Only used with [types](#type) `text`, `textarea`, `radio`.__
  256. `value`: `string`
  257. #### Example
  258. ```javascript
  259. $.contextMenu({
  260. selector: 'span.context-menu',
  261. command1: {
  262. name: "Foobar",
  263. type: "text",
  264. value: "default value"
  265. }
  266. });
  267. ```
  268. ### selected
  269. The selected option of a `select` element and the checked property for `checkbox` and `radio` types.
  270. __Only used with [types](#type) `select`, `checkbox`, `radio`.__
  271. `selected`: `string` or `boolean`
  272. Value | Description
  273. ---- | ----
  274. `boolean` | Use with `checkbox` and `radio` to check.
  275. `string` | Use with `select` to select that option.
  276. #### Example
  277. ```javascript
  278. $.contextMenu({
  279. selector: 'span.context-menu',
  280. items: {
  281. // <select>
  282. select: {
  283. name: "Select",
  284. type: 'select',
  285. options: {1: 'one', 2: 'two', 3: 'three'},
  286. selected: "2"
  287. }
  288. }
  289. });
  290. ```
  291. ### radio
  292. Specifies the group of the radio elements.
  293. __Only used with [type](#type) `radio`.__
  294. `radio`: `string`
  295. #### Example
  296. ```javascript
  297. $.contextMenu({
  298. selector: 'span.context-menu',
  299. items: {
  300. // <input type="radio">
  301. radio1: {
  302. name: "Radio1",
  303. type: 'radio',
  304. radio: 'radio',
  305. value: '1'
  306. },
  307. radio2: {
  308. name: "Radio2",
  309. type: 'radio',
  310. radio: 'radio',
  311. value: '2',
  312. selected: true
  313. }
  314. }
  315. });
  316. ```
  317. ### options
  318. Specifies the `<option>` elements for the `<select>` element.
  319. __Only used with [type](#type) `select`.__
  320. `options`: `object`
  321. #### Example
  322. ```javascript
  323. $.contextMenu({
  324. selector: 'span.context-menu',
  325. items: {
  326. // <select>
  327. select: {
  328. name: "Select",
  329. type: 'select',
  330. options: {1: 'one', 2: 'two', 3: 'three'},
  331. selected: "2"
  332. }
  333. }
  334. });
  335. ```
  336. ### height
  337. The height in pixel `<textarea>` element. If not specified, the height is defined by CSS.
  338. __Only used with [type](#type) `textarea`.__
  339. `height`: `int`
  340. #### Example
  341. ```javascript
  342. $.contextMenu({
  343. selector: 'span.context-menu',
  344. items: {
  345. // <select>
  346. myTextarea: {
  347. name: "Textarea",
  348. type: 'textarea',
  349. height: 200
  350. }
  351. }
  352. });
  353. ```
  354. ### items
  355. Commands to show in a sub-menu. You can nest as many as you like.
  356. `items`: `object`
  357. #### Example
  358. ```javascript
  359. $.contextMenu({
  360. selector: 'span.context-menu',
  361. items: {
  362. // <select>
  363. myItemWithSubmenu: {
  364. name: "Textarea",
  365. {
  366. items {
  367. mySubmenu {
  368. name: "Command 1"
  369. callback: function(key, opt){
  370. alert("Clicked on " + key);
  371. }
  372. }
  373. }
  374. }
  375. }
  376. }
  377. });
  378. ```
  379. ### accesskey
  380. Character(s) to be used as accesskey.
  381. Considering `a b c` $.contextMenu will first try to use »a« as the accesskey, if already taken, it'll fall through to »b«. Words are reduced to the first character, so »hello world« is treated as »h w«.
  382. Note: Accesskeys are treated unique throughout one menu. This means an item in a sub-menu can't occupy the same accesskey as an item in the main menu.
  383. `accesskey`: `string`
  384. #### Example
  385. ```javascript
  386. $.contextMenu({
  387. selector: 'span.context-menu',
  388. accesskey: 'a'
  389. callback: function(itemKey, opt){
  390. alert('I pressed a!');
  391. }
  392. });
  393. ```