extra_trees.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Fancytree
  4. *
  5. * Demo JS code for extra_trees.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var Fancytree = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Uniform
  15. var _componentFancytree = function() {
  16. if (!$().fancytree) {
  17. console.warn('Warning - fancytree_all.min.js is not loaded.');
  18. return;
  19. }
  20. // Basic setup
  21. // ------------------------------
  22. // Basic example
  23. $('.tree-default').fancytree({
  24. init: function(event, data) {
  25. $('.has-tooltip .fancytree-title').tooltip();
  26. }
  27. });
  28. // Load JSON data
  29. $('.tree-ajax').fancytree({
  30. source: {
  31. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  32. },
  33. init: function(event, data) {
  34. $('.has-tooltip .fancytree-title').tooltip();
  35. }
  36. });
  37. // Embed JSON data
  38. $('.tree-json').fancytree({
  39. init: function(event, data) {
  40. $('.has-tooltip .fancytree-title').tooltip();
  41. }
  42. });
  43. // Child counter
  44. $('.tree-child-count').fancytree({
  45. extensions: ['childcounter'],
  46. source: {
  47. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  48. },
  49. childcounter: {
  50. deep: true,
  51. hideZeros: true,
  52. hideExpanded: true
  53. },
  54. init: function(event, data) {
  55. $('.has-tooltip .fancytree-title').tooltip();
  56. }
  57. });
  58. // Drag and drop support
  59. $('.tree-drag').fancytree({
  60. extensions: ['dnd'],
  61. source: {
  62. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  63. },
  64. dnd: {
  65. autoExpandMS: 400,
  66. focusOnClick: true,
  67. preventVoidMoves: true, // Prevent dropping nodes 'before self', etc.
  68. preventRecursiveMoves: true, // Prevent dropping nodes on own descendants
  69. dragStart: function(node, data) {
  70. return true;
  71. },
  72. dragEnter: function(node, data) {
  73. return true;
  74. },
  75. dragDrop: function(node, data) {
  76. // This function MUST be defined to enable dropping of items on the tree.
  77. data.otherNode.moveTo(node, data.hitMode);
  78. }
  79. },
  80. init: function(event, data) {
  81. $('.has-tooltip .fancytree-title').tooltip();
  82. }
  83. });
  84. // Editable nodes
  85. $('.tree-editable').fancytree({
  86. extensions: ['edit'],
  87. source: {
  88. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  89. },
  90. edit: {
  91. adjustWidthOfs: 0,
  92. inputCss: {minWidth: '0'},
  93. triggerStart: ['f2', 'dblclick', 'shift+click', 'mac+enter'],
  94. save: function(event, data) {
  95. alert('save ' + data.input.val()); // Save data.input.val() or return false to keep editor open
  96. }
  97. }
  98. });
  99. //
  100. // Selectable nodes
  101. //
  102. // Single selection
  103. $('.tree-radio').fancytree({
  104. checkbox: true,
  105. selectMode: 1,
  106. source: {
  107. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  108. }
  109. });
  110. // Multiple selection
  111. $('.tree-checkbox').fancytree({
  112. checkbox: true,
  113. selectMode: 2,
  114. source: {
  115. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  116. }
  117. });
  118. // Disable selections
  119. $('.tree-checkbox-options').fancytree({
  120. checkbox: true,
  121. selectMode: 2
  122. });
  123. // Hierarchical select
  124. $('.tree-checkbox-hierarchical').fancytree({
  125. checkbox: true,
  126. selectMode: 3
  127. });
  128. //
  129. // Toggle checkbox state
  130. //
  131. // Initialize
  132. $('.tree-checkbox-toggle').fancytree({
  133. checkbox: true,
  134. selectMode: 2,
  135. source: {
  136. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  137. }
  138. });
  139. // Initialize switchery toggle
  140. var switcherySelect = document.querySelector('.form-input-switchery');
  141. var initSelect = new Switchery(switcherySelect);
  142. // Change checkbox states
  143. switcherySelect.onchange = function() {
  144. if(switcherySelect.checked) {
  145. $('.tree-checkbox-toggle').fancytree('getTree').visit(function(node){
  146. node.setSelected(true);
  147. });
  148. return false;
  149. }
  150. else {
  151. $('.tree-checkbox-toggle').fancytree('getTree').visit(function(node){
  152. node.setSelected(false);
  153. });
  154. return false;
  155. }
  156. };
  157. // Advanced examples
  158. // ------------------------------
  159. //
  160. // Toggle state
  161. //
  162. // Initialize switchery toggle
  163. var switchery = document.querySelector('#tree-disabled');
  164. var init = new Switchery(switchery);
  165. // Do something on state change
  166. switchery.onchange = function() {
  167. if(switchery.checked) {
  168. $('.tree-toggle').fancytree('disable');
  169. }
  170. else {
  171. $('.tree-toggle').fancytree('enable');
  172. }
  173. };
  174. // Initialize
  175. $('.tree-toggle').fancytree({
  176. source: {
  177. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  178. },
  179. init: function(event, data) {
  180. $('.has-tooltip .fancytree-title').tooltip();
  181. }
  182. });
  183. //
  184. // Sorting
  185. //
  186. // Initialize
  187. $('.tree-sorting').fancytree();
  188. // Sort tree
  189. $('.sort-tree').on('click', function() {
  190. var node = $('.tree-sorting').fancytree('getRootNode');
  191. node.sortChildren(null, true);
  192. });
  193. // Sort active nodes
  194. $('.sort-branch').on('click', function() {
  195. var node = $('.tree-sorting').fancytree('getActiveNode');
  196. // Custom compare function (optional) that sorts case insensitive
  197. var cmp = function(a, b) {
  198. a = a.title.toLowerCase();
  199. b = b.title.toLowerCase();
  200. return a > b ? 1 : a < b ? -1 : 0;
  201. };
  202. node.sortChildren(cmp, false);
  203. });
  204. //
  205. // Tree persistence
  206. //
  207. // Initialize
  208. $('.tree-persistence').fancytree({
  209. extensions: ['persist'],
  210. source: {
  211. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  212. },
  213. checkbox: true,
  214. persist: {
  215. overrideSource: false, // true: cookie takes precedence over `source` data attributes.
  216. store: 'auto' // 'cookie', 'local': use localStore, 'session': sessionStore
  217. },
  218. postProcess: function(event, data) {
  219. var prefix = data.node.getIndexHier() + '.';
  220. $.each(data.response, function(idx, childEntry) {
  221. if( childEntry.key == null ) {
  222. childEntry.key = prefix + (idx + 1);
  223. childEntry.title += ' (' + childEntry.key + ')';
  224. }
  225. })
  226. }
  227. });
  228. var tree5 = $('.tree-persistence').fancytree('getTree');
  229. // Reset cookies on button click
  230. $('.reset-cookies').on('click', function() {
  231. tree5.clearCookies();
  232. });
  233. //
  234. // Table tree
  235. //
  236. $('.tree-table').fancytree({
  237. extensions: ['table'],
  238. checkbox: true,
  239. table: {
  240. indentation: 20, // indent 20px per node level
  241. nodeColumnIdx: 2, // render the node title into the 2nd column
  242. checkboxColumnIdx: 0 // render the checkboxes into the 1st column
  243. },
  244. source: {
  245. url: '../../../../global_assets/demo_data/fancytree/fancytree.json'
  246. },
  247. lazyLoad: function(event, data) {
  248. data.result = {url: 'ajax-sub2.json'}
  249. },
  250. renderColumns: function(event, data) {
  251. var node = data.node,
  252. $tdList = $(node.tr).find('>td');
  253. // (index #0 is rendered by fancytree by adding the checkbox)
  254. $tdList.eq(1).text(node.getIndexHier()).addClass('alignRight');
  255. // (index #2 is rendered by fancytree)
  256. $tdList.eq(3).text(node.key);
  257. $tdList.eq(4).addClass('text-center').html('<input type="checkbox" class="form-input-styled" name="like" value="' + node.key + '">');
  258. // Style checkboxes
  259. $('.form-input-styled').uniform();
  260. }
  261. });
  262. // Handle custom checkbox clicks
  263. $('.tree-table').on('input[name=like]', 'click', function(e) {
  264. var node = $.ui.fancytree.getNode(e),
  265. $input = $(e.target);
  266. e.stopPropagation(); // prevent fancytree activate for this row
  267. if($input.is(':checked')){
  268. alert('like ' + $input.val());
  269. }
  270. else{
  271. alert('dislike ' + $input.val());
  272. }
  273. });
  274. };
  275. //
  276. // Return objects assigned to module
  277. //
  278. return {
  279. init: function() {
  280. _componentFancytree();
  281. }
  282. }
  283. }();
  284. // Initialize module
  285. // ------------------------------
  286. document.addEventListener('DOMContentLoaded', function() {
  287. Fancytree.init();
  288. });