treeview.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*if ($('.tree > ul') && !mytreebranch) {
  2. var mytreebranch = $('.tree').find('li:has(ul)').addClass('parent_li').attr('role', 'treeitem').find(' > span').attr('title', 'Collapse this branch');
  3. $('.tree > ul').attr('role', 'tree').find('ul').attr('role', 'group');
  4. mytreebranch.on('click', function (e) {
  5. var children = $(this).parent('li.parent_li').find(' > ul > li');
  6. if (children.is(':visible')) {
  7. children.hide('fast');
  8. $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
  9. }
  10. else {
  11. children.show('fast');
  12. $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
  13. }
  14. e.stopPropagation();
  15. });
  16. }*/
  17. /**
  18. * A jQuery plugin boilerplate.
  19. * Author: Jonathan Nicol @f6design
  20. */
  21. ;(function($) {
  22. // Change this to your plugin name.
  23. var pluginName = 'treeView';
  24. /**
  25. * Plugin object constructor.
  26. * Implements the Revealing Module Pattern.
  27. */
  28. function Plugin(element, options) {
  29. // References to DOM and jQuery versions of element.
  30. var el = element;
  31. var $el = $(element);
  32. options = $.extend({}, $.fn[pluginName].defaults, options);
  33. /**
  34. * Initialize plugin.
  35. */
  36. function init() {
  37. // Add any initialization logic here...
  38. var mytreebranch = $('.tree').find('li:has(ul)').addClass('parent_li').attr('role', 'treeitem').find(' > span').attr('title', 'Collapse this branch');
  39. $('.tree > ul').attr('role', 'tree').find('ul').attr('role', 'group');
  40. mytreebranch.on('click', function (e) {
  41. var children = $(this).parent('li.parent_li').find(' > ul > li');
  42. if (children.is(':visible')) {
  43. children.hide('fast');
  44. $(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');
  45. }
  46. else {
  47. children.show('fast');
  48. $(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');
  49. }
  50. e.stopPropagation();
  51. });
  52. hook('onInit');
  53. }
  54. /**
  55. * Example Public Method
  56. */
  57. function fooPublic() {
  58. // Code goes here...
  59. }
  60. /**
  61. * Get/set a plugin option.
  62. * Get usage: $('#el').demoplugin('option', 'key');
  63. * Set usage: $('#el').demoplugin('option', 'key', value);
  64. */
  65. function option (key, val) {
  66. if (val) {
  67. options[key] = val;
  68. } else {
  69. return options[key];
  70. }
  71. }
  72. /**
  73. * Destroy plugin.
  74. * Usage: $('#el').demoplugin('destroy');
  75. */
  76. function destroy() {
  77. // Iterate over each matching element.
  78. $el.each(function() {
  79. var el = this;
  80. var $el = $(this);
  81. // Add code to restore the element to its original state...
  82. hook('onDestroy');
  83. // Remove Plugin instance from the element.
  84. $el.removeData('plugin_' + pluginName);
  85. });
  86. }
  87. /**
  88. * Callback hooks.
  89. * Usage: In the defaults object specify a callback function:
  90. * hookName: function() {}
  91. * Then somewhere in the plugin trigger the callback:
  92. * hook('hookName');
  93. */
  94. function hook(hookName) {
  95. if (options[hookName] !== undefined) {
  96. // Call the user defined function.
  97. // Scope is set to the jQuery element we are operating on.
  98. options[hookName].call(el);
  99. }
  100. }
  101. // Initialize the plugin instance.
  102. init();
  103. // Expose methods of Plugin we wish to be public.
  104. return {
  105. option: option,
  106. destroy: destroy,
  107. fooPublic: fooPublic
  108. };
  109. }
  110. /**
  111. * Plugin definition.
  112. */
  113. $.fn[pluginName] = function(options) {
  114. // If the first parameter is a string, treat this as a call to
  115. // a public method.
  116. if (typeof arguments[0] === 'string') {
  117. var methodName = arguments[0];
  118. var args = Array.prototype.slice.call(arguments, 1);
  119. var returnVal;
  120. this.each(function() {
  121. // Check that the element has a plugin instance, and that
  122. // the requested public method exists.
  123. if ($.data(this, 'plugin_' + pluginName) && typeof $.data(this, 'plugin_' + pluginName)[methodName] === 'function') {
  124. // Call the method of the Plugin instance, and Pass it
  125. // the supplied arguments.
  126. returnVal = $.data(this, 'plugin_' + pluginName)[methodName].apply(this, args);
  127. } else {
  128. throw new Error('Method ' + methodName + ' does not exist on jQuery.' + pluginName);
  129. }
  130. });
  131. if (returnVal !== undefined){
  132. // If the method returned a value, return the value.
  133. return returnVal;
  134. } else {
  135. // Otherwise, returning 'this' preserves chainability.
  136. return this;
  137. }
  138. // If the first parameter is an object (options), or was omitted,
  139. // instantiate a new instance of the plugin.
  140. } else if (typeof options === "object" || !options) {
  141. return this.each(function() {
  142. // Only allow the plugin to be instantiated once.
  143. if (!$.data(this, 'plugin_' + pluginName)) {
  144. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  145. }
  146. });
  147. }
  148. };
  149. // Default plugin options.
  150. $.fn[pluginName].defaults = {
  151. onInit: function() {},
  152. onDestroy: function() {},
  153. element: $('.tree > ul')
  154. };
  155. })(jQuery);
  156. $('.tree').treeView({
  157. element: $('.tree > ul')
  158. });