handsontable_context.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Handsontable - Excel-like tables with extensive funtionality
  4. *
  5. * Demo JS code for handsontable_context.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var HotContextMenu = function() {
  11. //
  12. // Setup module components
  13. //
  14. // HOT context menu examples
  15. var _componentHotContextMenu = function() {
  16. if (typeof Handsontable == 'undefined') {
  17. console.warn('Warning - handsontable.min.js is not loaded.');
  18. return;
  19. }
  20. // Basic configuration
  21. // ------------------------------
  22. // Add sample data for all examples
  23. var car_data = [
  24. {car: "Mercedes", model: "GL500", year: "11/01/2015", price: 32500, share: 0.64},
  25. {car: "Chevrolet", model: "Camaro", year: "11/02/2015", price: 42400, share: 0.37},
  26. {car: "Dodge", model: "Charger", year: "11/03/2015", price: 24900, share: 0.33},
  27. {car: "Hummer", model: "H3", year: "11/04/2015", price: 54000, share: 0.15},
  28. {car: "Chevrolet", model: "Tahoe", year: "11/05/2015", price: 29300, share: 0.27},
  29. {car: "Toyota", model: "Land Cruiser", year: "11/06/2015", price: 54500, share: 0.43},
  30. {car: "Nissan", model: "GTR", year: "11/07/2015", price: 44900, share: 0.35},
  31. {car: "Porsche", model: "Cayenne", year: "11/08/2015", price: 35000, share: 0.63},
  32. {car: "Volkswagen", model: "Touareg", year: "11/09/2015", price: 41000, share: 0.15},
  33. {car: "BMW", model: "X5", year: "11/10/2015", price: 48800, share: 0.35},
  34. {car: "Audi", model: "Q7", year: "11/11/2015", price: 21000, share: 0.53},
  35. {car: "Cadillac", model: "Escalade", year: "11/12/2015", price: 63900, share: 0.38},
  36. {car: "Suzuki", model: "SX4", year: "11/13/2015", price: 23700, share: 0.8},
  37. {car: "Opel", model: "Insignia", year: "11/14/2015", price: 43900, share: 0.27},
  38. {car: "Ford", model: "Explorer", year: "11/15/2015", price: 68900, share: 0.17}
  39. ];
  40. // Define element
  41. var hot_context_basic = document.getElementById('hot_context_basic');
  42. // Initialize with options
  43. var hot_context_basic_init = new Handsontable(hot_context_basic, {
  44. data: car_data,
  45. rowHeaders: true,
  46. stretchH: 'all',
  47. colHeaders: ['Brand', 'Model', 'Date', 'Price', 'Market share'],
  48. contextMenu: true,
  49. columns: [
  50. {
  51. data: 'car'
  52. },
  53. {
  54. data: 'model'
  55. },
  56. {
  57. data: 'year',
  58. type: 'date',
  59. dateFormat: 'MM/DD/YYYY'
  60. },
  61. {
  62. data: 'price',
  63. type: 'numeric',
  64. className: 'htLeft',
  65. numericFormat: {
  66. pattern: '$0,0.00'
  67. }
  68. },
  69. {
  70. data: 'share',
  71. type: 'numeric',
  72. className: 'htLeft',
  73. numericFormat: {
  74. pattern: '0%'
  75. },
  76. width: 50
  77. }
  78. ]
  79. });
  80. // Specific options
  81. // ------------------------------
  82. // Define element
  83. var hot_context_specific = document.getElementById('hot_context_specific');
  84. // Initialize with options
  85. var hot_context_specific_init = new Handsontable(hot_context_specific, {
  86. data: car_data,
  87. rowHeaders: true,
  88. stretchH: 'all',
  89. colHeaders: ['Brand', 'Model', 'Date', 'Price', 'Market share'],
  90. contextMenu: ['row_above', 'row_below', 'remove_row'],
  91. columns: [
  92. {
  93. data: 'car'
  94. },
  95. {
  96. data: 'model'
  97. },
  98. {
  99. data: 'year',
  100. type: 'date',
  101. dateFormat: 'MM/DD/YYYY'
  102. },
  103. {
  104. data: 'price',
  105. type: 'numeric',
  106. className: 'htLeft',
  107. numericFormat: {
  108. pattern: '$0,0.00'
  109. }
  110. },
  111. {
  112. data: 'share',
  113. type: 'numeric',
  114. className: 'htLeft',
  115. numericFormat: {
  116. pattern: '0%'
  117. },
  118. width: 50
  119. }
  120. ]
  121. });
  122. // Custom configuration
  123. // ------------------------------
  124. // Define element
  125. var hot_context_custom = document.getElementById('hot_context_custom')
  126. // Initialize with options
  127. var hot_context_custom_init = new Handsontable(hot_context_custom, {
  128. data: car_data,
  129. rowHeaders: true,
  130. stretchH: 'all',
  131. colHeaders: ['Brand', 'Model', 'Date', 'Price', 'Market share'],
  132. columns: [
  133. {
  134. data: 'car'
  135. },
  136. {
  137. data: 'model'
  138. },
  139. {
  140. data: 'year',
  141. type: 'date',
  142. dateFormat: 'MM/DD/YYYY'
  143. },
  144. {
  145. data: 'price',
  146. type: 'numeric',
  147. className: 'htLeft',
  148. numericFormat: {
  149. pattern: '$0,0.00'
  150. }
  151. },
  152. {
  153. data: 'share',
  154. type: 'numeric',
  155. className: 'htLeft',
  156. numericFormat: {
  157. pattern: '0%'
  158. },
  159. width: 50
  160. }
  161. ]
  162. });
  163. // Update settings for context menu
  164. hot_context_custom_init.updateSettings({
  165. contextMenu: {
  166. callback: function (key, options) {
  167. if (key === 'about') {
  168. setTimeout(function () {
  169. // timeout is used to make sure the menu collapsed before alert is shown
  170. alert("This is a context menu with default and custom options mixed");
  171. }, 100);
  172. }
  173. },
  174. items: {
  175. "row_above": {
  176. disabled: function () {
  177. // if first row, disable this option
  178. return hot_context_custom_init.getSelected()[0] === 0;
  179. }
  180. },
  181. "row_below": {},
  182. "hsep1": "---------",
  183. "remove_row": {
  184. name: 'Remove this row, ok?',
  185. disabled: function () {
  186. // if first row, disable this option
  187. return hot_context_custom_init.getSelected()[0] === 0
  188. }
  189. },
  190. "hsep2": "---------",
  191. "about": {name: 'About this menu'}
  192. }
  193. }
  194. });
  195. // Copy-paste configuration
  196. // ------------------------------
  197. // Define element
  198. var hot_context_copy = document.getElementById('hot_context_copy');
  199. var sheetclip = new SheetClip();
  200. var clipboardCache = '';
  201. // Initialize with options
  202. var hot_context_copy_init = new Handsontable(hot_context_copy, {
  203. data: car_data,
  204. rowHeaders: true,
  205. stretchH: 'all',
  206. colHeaders: ['Brand', 'Model', 'Date', 'Price', 'Market share'],
  207. columns: [
  208. {
  209. data: 'car'
  210. },
  211. {
  212. data: 'model'
  213. },
  214. {
  215. data: 'year',
  216. type: 'date',
  217. dateFormat: 'MM/DD/YYYY'
  218. },
  219. {
  220. data: 'price',
  221. type: 'numeric',
  222. className: 'htLeft',
  223. numericFormat: {
  224. pattern: '$0,0.00'
  225. }
  226. },
  227. {
  228. data: 'share',
  229. type: 'numeric',
  230. className: 'htLeft',
  231. numericFormat: {
  232. pattern: '0%'
  233. },
  234. width: 50
  235. }
  236. ],
  237. afterCopy: function(changes) {
  238. clipboardCache = sheetclip.stringify(changes);
  239. },
  240. afterCut: function(changes) {
  241. clipboardCache = sheetclip.stringify(changes);
  242. },
  243. afterPaste: function(changes) {
  244. clipboardCache = sheetclip.stringify(changes);
  245. },
  246. contextMenu: [
  247. 'row_above',
  248. 'row_below',
  249. 'remove_row',
  250. '---------',
  251. 'copy',
  252. 'cut',
  253. {
  254. key: 'paste',
  255. name: 'Paste',
  256. disabled: function() {
  257. return clipboardCache.length === 0;
  258. },
  259. callback: function() {
  260. var plugin = this.getPlugin('copyPaste');
  261. this.listen();
  262. plugin.paste(clipboardCache);
  263. }
  264. }
  265. ]
  266. });
  267. };
  268. //
  269. // Return objects assigned to module
  270. //
  271. return {
  272. init: function() {
  273. _componentHotContextMenu();
  274. }
  275. }
  276. }();
  277. // Initialize module
  278. // ------------------------------
  279. document.addEventListener('DOMContentLoaded', function() {
  280. HotContextMenu.init();
  281. });