|
28 | 28 | // Fancytree core module, and makes sure that we can use the `require()` |
29 | 29 | // syntax with package loaders. |
30 | 30 |
|
31 | | -;(function( factory ) { |
32 | | - if ( typeof define === "function" && define.amd ) { |
| 31 | +(function(factory) { |
| 32 | + if (typeof define === "function" && define.amd) { |
33 | 33 | // AMD. Register as an anonymous module. |
34 | | - define( [ "jquery", "./jquery.fancytree" ], factory ); |
35 | | - } else if ( typeof module === "object" && module.exports ) { |
| 34 | + define(["jquery", "./jquery.fancytree"], factory); |
| 35 | + } else if (typeof module === "object" && module.exports) { |
36 | 36 | // Node/CommonJS |
37 | 37 | require("./jquery.fancytree"); |
38 | 38 | module.exports = factory(require("jquery")); |
39 | 39 | } else { |
40 | 40 | // Browser globals |
41 | | - factory( jQuery ); |
| 41 | + factory(jQuery); |
42 | 42 | } |
43 | | - |
44 | | -}( function( $ ) { |
45 | | - |
46 | | -// Consider to use [strict mode](http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/) |
47 | | -"use strict"; |
48 | | - |
49 | | -// The [coding guidelines](http://contribute.jquery.org/style-guide/js/) |
50 | | -// require jshint compliance. |
51 | | -// But for this sample, we want to allow unused variables for demonstration purpose. |
52 | | - |
53 | | -/*jshint unused:false */ |
54 | | - |
55 | | - |
56 | | -// Adding methods |
57 | | -// -------------- |
58 | | - |
59 | | -// New member functions can be added to the `Fancytree` class. |
60 | | -// This function will be available for every tree instance: |
61 | | -// |
62 | | -// var tree = $("#tree").fancytree("getTree"); |
63 | | -// tree.countSelected(false); |
64 | | - |
65 | | -$.ui.fancytree._FancytreeClass.prototype.countSelected = function(topOnly){ |
66 | | - var tree = this, |
67 | | - treeOptions = tree.options; |
68 | | - |
69 | | - return tree.getSelectedNodes(topOnly).length; |
70 | | -}; |
71 | | - |
72 | | - |
73 | | -// The `FancytreeNode` class can also be easily extended. This would be called |
74 | | -// like |
75 | | -// node.updateCounters(); |
76 | | -// |
77 | | -// It is also good practice to add a docstring comment. |
78 | | -/** |
79 | | - * [ext-childcounter] Update counter badges for `node` and its parents. |
80 | | - * May be called in the `loadChildren` event, to update parents of lazy loaded |
81 | | - * nodes. |
82 | | - * @alias FancytreeNode#updateCounters |
83 | | - * @requires jquery.fancytree.childcounters.js |
84 | | - */ |
85 | | -$.ui.fancytree._FancytreeNodeClass.prototype.updateCounters = function(){ |
86 | | - var node = this, |
87 | | - $badge = $("span.fancytree-childcounter", node.span), |
88 | | - extOpts = node.tree.options.childcounter, |
89 | | - count = node.countChildren(extOpts.deep); |
90 | | - |
91 | | - node.data.childCounter = count; |
92 | | - if( (count || !extOpts.hideZeros) && (!node.isExpanded() || !extOpts.hideExpanded) ) { |
93 | | - if( !$badge.length ) { |
94 | | - $badge = $("<span class='fancytree-childcounter'/>").appendTo($("span.fancytree-icon", node.span)); |
| 43 | +})(function($) { |
| 44 | + // Consider to use [strict mode](http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/) |
| 45 | + "use strict"; |
| 46 | + |
| 47 | + // The [coding guidelines](http://contribute.jquery.org/style-guide/js/) |
| 48 | + // require jshint compliance. |
| 49 | + // But for this sample, we want to allow unused variables for demonstration purpose. |
| 50 | + |
| 51 | + /*jshint unused:false */ |
| 52 | + |
| 53 | + // Adding methods |
| 54 | + // -------------- |
| 55 | + |
| 56 | + // New member functions can be added to the `Fancytree` class. |
| 57 | + // This function will be available for every tree instance: |
| 58 | + // |
| 59 | + // var tree = $("#tree").fancytree("getTree"); |
| 60 | + // tree.countSelected(false); |
| 61 | + |
| 62 | + $.ui.fancytree._FancytreeClass.prototype.countSelected = function(topOnly) { |
| 63 | + var tree = this, |
| 64 | + treeOptions = tree.options; |
| 65 | + |
| 66 | + return tree.getSelectedNodes(topOnly).length; |
| 67 | + }; |
| 68 | + |
| 69 | + // The `FancytreeNode` class can also be easily extended. This would be called |
| 70 | + // like |
| 71 | + // node.updateCounters(); |
| 72 | + // |
| 73 | + // It is also good practice to add a docstring comment. |
| 74 | + /** |
| 75 | + * [ext-childcounter] Update counter badges for `node` and its parents. |
| 76 | + * May be called in the `loadChildren` event, to update parents of lazy loaded |
| 77 | + * nodes. |
| 78 | + * @alias FancytreeNode#updateCounters |
| 79 | + * @requires jquery.fancytree.childcounters.js |
| 80 | + */ |
| 81 | + $.ui.fancytree._FancytreeNodeClass.prototype.updateCounters = function() { |
| 82 | + var node = this, |
| 83 | + $badge = $("span.fancytree-childcounter", node.span), |
| 84 | + extOpts = node.tree.options.childcounter, |
| 85 | + count = node.countChildren(extOpts.deep); |
| 86 | + |
| 87 | + node.data.childCounter = count; |
| 88 | + if ( |
| 89 | + (count || !extOpts.hideZeros) && |
| 90 | + (!node.isExpanded() || !extOpts.hideExpanded) |
| 91 | + ) { |
| 92 | + if (!$badge.length) { |
| 93 | + $badge = $("<span class='fancytree-childcounter'/>").appendTo( |
| 94 | + $("span.fancytree-icon", node.span) |
| 95 | + ); |
| 96 | + } |
| 97 | + $badge.text(count); |
| 98 | + } else { |
| 99 | + $badge.remove(); |
95 | 100 | } |
96 | | - $badge.text(count); |
97 | | - } else { |
98 | | - $badge.remove(); |
99 | | - } |
100 | | - if( extOpts.deep && !node.isTopLevel() && !node.isRoot() ) { |
101 | | - node.parent.updateCounters(); |
102 | | - } |
103 | | -}; |
104 | | - |
105 | | - |
106 | | -// Finally, we can extend the widget API and create functions that are called |
107 | | -// like so: |
108 | | -// |
109 | | -// $("#tree").fancytree("widgetMethod1", "abc"); |
110 | | - |
111 | | -$.ui.fancytree.prototype.widgetMethod1 = function(arg1){ |
112 | | - var tree = this.tree; |
113 | | - return arg1; |
114 | | -}; |
115 | | - |
116 | | - |
117 | | -// Register a Fancytree extension |
118 | | -// ------------------------------ |
119 | | -// A full blown extension, extension is available for all trees and can be |
120 | | -// enabled like so (see also the [live demo](http://wwwendt.de/tech/fancytree/demo/sample-ext-childcounter.html)): |
121 | | -// |
122 | | -// <script src="../src/jquery.fancytree.js"></script> |
123 | | -// <script src="../src/jquery.fancytree.childcounter.js"></script> |
124 | | -// ... |
125 | | -// |
126 | | -// $("#tree").fancytree({ |
127 | | -// extensions: ["childcounter"], |
128 | | -// childcounter: { |
129 | | -// hideExpanded: true |
130 | | -// }, |
131 | | -// ... |
132 | | -// }); |
133 | | -// |
134 | | - |
135 | | - |
136 | | -/* 'childcounter' extension */ |
137 | | -$.ui.fancytree.registerExtension({ |
138 | | -// Every extension must be registered by a unique name. |
139 | | - name: "childcounter", |
140 | | -// Version information should be compliant with [semver](http://semver.org) |
141 | | - version: "@VERSION", |
142 | | - |
143 | | -// Extension specific options and their defaults. |
144 | | -// This options will be available as `tree.options.childcounter.hideExpanded` |
145 | | - |
146 | | - options: { |
147 | | - deep: true, |
148 | | - hideZeros: true, |
149 | | - hideExpanded: false |
150 | | - }, |
151 | | - |
152 | | -// Attributes other than `options` (or functions) can be defined here, and |
153 | | -// will be added to the tree.ext.EXTNAME namespace, in this case `tree.ext.childcounter.foo`. |
154 | | -// They can also be accessed as `this._local.foo` from within the extension |
155 | | -// methods. |
156 | | - foo: 42, |
157 | | - |
158 | | -// Local functions are prefixed with an underscore '_'. |
159 | | -// Callable as `this._local._appendCounter()`. |
160 | | - |
161 | | - _appendCounter: function(bar){ |
162 | | - var tree = this; |
163 | | - }, |
164 | | - |
165 | | -// **Override virtual methods for this extension.** |
166 | | -// |
167 | | -// Fancytree implements a number of 'hook methods', prefixed by 'node...' or 'tree...'. |
168 | | -// with a `ctx` argument (see [EventData](http://www.wwwendt.de/tech/fancytree/doc/jsdoc/global.html#EventData) |
169 | | -// for details) and an extended calling context:<br> |
170 | | -// `this` : the Fancytree instance<br> |
171 | | -// `this._local`: the namespace that contains extension attributes and private methods (same as this.ext.EXTNAME)<br> |
172 | | -// `this._super`: the virtual function that was overridden (member of previous extension or Fancytree) |
173 | | -// |
174 | | -// See also the [complete list of available hook functions](http://www.wwwendt.de/tech/fancytree/doc/jsdoc/Fancytree_Hooks.html). |
175 | | - |
176 | | - /* Init */ |
177 | | -// `treeInit` is triggered when a tree is initalized. We can set up classes or |
178 | | -// bind event handlers here... |
179 | | - treeInit: function(ctx){ |
180 | | - var tree = this, // same as ctx.tree, |
181 | | - opts = ctx.options, |
182 | | - extOpts = ctx.options.childcounter; |
183 | | -// Optionally check for dependencies with other extensions |
184 | | - /* this._requireExtension("glyph", false, false); */ |
185 | | -// Call the base implementation |
186 | | - this._superApply(arguments); |
187 | | -// Add a class to the tree container |
188 | | - this.$container.addClass("fancytree-ext-childcounter"); |
189 | | - }, |
190 | | - |
191 | | -// Destroy this tree instance (we only call the default implementation, so |
192 | | -// this method could as well be omitted). |
193 | | - |
194 | | - treeDestroy: function(ctx){ |
195 | | - this._superApply(arguments); |
196 | | - }, |
197 | | - |
198 | | -// Overload the `renderTitle` hook, to append a counter badge |
199 | | - nodeRenderTitle: function(ctx, title) { |
200 | | - var node = ctx.node, |
201 | | - extOpts = ctx.options.childcounter, |
202 | | - count = (node.data.childCounter == null) ? node.countChildren(extOpts.deep) : +node.data.childCounter; |
203 | | -// Let the base implementation render the title |
204 | | -// We use `_super()` instead of `_superApply()` here, since it is a little bit |
205 | | -// more performant when called often |
206 | | - this._super(ctx, title); |
207 | | -// Append a counter badge |
208 | | - if( (count || ! extOpts.hideZeros) && (!node.isExpanded() || !extOpts.hideExpanded) ){ |
209 | | - $("span.fancytree-icon", node.span).append($("<span class='fancytree-childcounter'/>").text(count)); |
| 101 | + if (extOpts.deep && !node.isTopLevel() && !node.isRoot()) { |
| 102 | + node.parent.updateCounters(); |
210 | 103 | } |
211 | | - }, |
212 | | -// Overload the `setExpanded` hook, so the counters are updated |
213 | | - nodeSetExpanded: function(ctx, flag, callOpts) { |
214 | | - var tree = ctx.tree, |
215 | | - node = ctx.node; |
216 | | -// Let the base implementation expand/collapse the node, then redraw the title |
217 | | -// after the animation has finished |
218 | | - return this._superApply(arguments).always(function(){ |
219 | | - tree.nodeRenderTitle(ctx); |
220 | | - }); |
221 | | - } |
222 | | - |
223 | | -// End of extension definition |
224 | | -}); |
225 | | -// Value returned by `require('jquery.fancytree..')` |
226 | | -return $.ui.fancytree; |
227 | | -})); // End of closure |
| 104 | + }; |
| 105 | + |
| 106 | + // Finally, we can extend the widget API and create functions that are called |
| 107 | + // like so: |
| 108 | + // |
| 109 | + // $("#tree").fancytree("widgetMethod1", "abc"); |
| 110 | + |
| 111 | + $.ui.fancytree.prototype.widgetMethod1 = function(arg1) { |
| 112 | + var tree = this.tree; |
| 113 | + return arg1; |
| 114 | + }; |
| 115 | + |
| 116 | + // Register a Fancytree extension |
| 117 | + // ------------------------------ |
| 118 | + // A full blown extension, extension is available for all trees and can be |
| 119 | + // enabled like so (see also the [live demo](http://wwwendt.de/tech/fancytree/demo/sample-ext-childcounter.html)): |
| 120 | + // |
| 121 | + // <script src="../src/jquery.fancytree.js"></script> |
| 122 | + // <script src="../src/jquery.fancytree.childcounter.js"></script> |
| 123 | + // ... |
| 124 | + // |
| 125 | + // $("#tree").fancytree({ |
| 126 | + // extensions: ["childcounter"], |
| 127 | + // childcounter: { |
| 128 | + // hideExpanded: true |
| 129 | + // }, |
| 130 | + // ... |
| 131 | + // }); |
| 132 | + // |
| 133 | + |
| 134 | + /* 'childcounter' extension */ |
| 135 | + $.ui.fancytree.registerExtension({ |
| 136 | + // Every extension must be registered by a unique name. |
| 137 | + name: "childcounter", |
| 138 | + // Version information should be compliant with [semver](http://semver.org) |
| 139 | + version: "@VERSION", |
| 140 | + |
| 141 | + // Extension specific options and their defaults. |
| 142 | + // This options will be available as `tree.options.childcounter.hideExpanded` |
| 143 | + |
| 144 | + options: { |
| 145 | + deep: true, |
| 146 | + hideZeros: true, |
| 147 | + hideExpanded: false, |
| 148 | + }, |
| 149 | + |
| 150 | + // Attributes other than `options` (or functions) can be defined here, and |
| 151 | + // will be added to the tree.ext.EXTNAME namespace, in this case `tree.ext.childcounter.foo`. |
| 152 | + // They can also be accessed as `this._local.foo` from within the extension |
| 153 | + // methods. |
| 154 | + foo: 42, |
| 155 | + |
| 156 | + // Local functions are prefixed with an underscore '_'. |
| 157 | + // Callable as `this._local._appendCounter()`. |
| 158 | + |
| 159 | + _appendCounter: function(bar) { |
| 160 | + var tree = this; |
| 161 | + }, |
| 162 | + |
| 163 | + // **Override virtual methods for this extension.** |
| 164 | + // |
| 165 | + // Fancytree implements a number of 'hook methods', prefixed by 'node...' or 'tree...'. |
| 166 | + // with a `ctx` argument (see [EventData](http://www.wwwendt.de/tech/fancytree/doc/jsdoc/global.html#EventData) |
| 167 | + // for details) and an extended calling context:<br> |
| 168 | + // `this` : the Fancytree instance<br> |
| 169 | + // `this._local`: the namespace that contains extension attributes and private methods (same as this.ext.EXTNAME)<br> |
| 170 | + // `this._super`: the virtual function that was overridden (member of previous extension or Fancytree) |
| 171 | + // |
| 172 | + // See also the [complete list of available hook functions](http://www.wwwendt.de/tech/fancytree/doc/jsdoc/Fancytree_Hooks.html). |
| 173 | + |
| 174 | + /* Init */ |
| 175 | + // `treeInit` is triggered when a tree is initalized. We can set up classes or |
| 176 | + // bind event handlers here... |
| 177 | + treeInit: function(ctx) { |
| 178 | + var tree = this, // same as ctx.tree, |
| 179 | + opts = ctx.options, |
| 180 | + extOpts = ctx.options.childcounter; |
| 181 | + // Optionally check for dependencies with other extensions |
| 182 | + /* this._requireExtension("glyph", false, false); */ |
| 183 | + // Call the base implementation |
| 184 | + this._superApply(arguments); |
| 185 | + // Add a class to the tree container |
| 186 | + this.$container.addClass("fancytree-ext-childcounter"); |
| 187 | + }, |
| 188 | + |
| 189 | + // Destroy this tree instance (we only call the default implementation, so |
| 190 | + // this method could as well be omitted). |
| 191 | + |
| 192 | + treeDestroy: function(ctx) { |
| 193 | + this._superApply(arguments); |
| 194 | + }, |
| 195 | + |
| 196 | + // Overload the `renderTitle` hook, to append a counter badge |
| 197 | + nodeRenderTitle: function(ctx, title) { |
| 198 | + var node = ctx.node, |
| 199 | + extOpts = ctx.options.childcounter, |
| 200 | + count = |
| 201 | + node.data.childCounter == null |
| 202 | + ? node.countChildren(extOpts.deep) |
| 203 | + : +node.data.childCounter; |
| 204 | + // Let the base implementation render the title |
| 205 | + // We use `_super()` instead of `_superApply()` here, since it is a little bit |
| 206 | + // more performant when called often |
| 207 | + this._super(ctx, title); |
| 208 | + // Append a counter badge |
| 209 | + if ( |
| 210 | + (count || !extOpts.hideZeros) && |
| 211 | + (!node.isExpanded() || !extOpts.hideExpanded) |
| 212 | + ) { |
| 213 | + $("span.fancytree-icon", node.span).append( |
| 214 | + $("<span class='fancytree-childcounter'/>").text(count) |
| 215 | + ); |
| 216 | + } |
| 217 | + }, |
| 218 | + // Overload the `setExpanded` hook, so the counters are updated |
| 219 | + nodeSetExpanded: function(ctx, flag, callOpts) { |
| 220 | + var tree = ctx.tree, |
| 221 | + node = ctx.node; |
| 222 | + // Let the base implementation expand/collapse the node, then redraw the title |
| 223 | + // after the animation has finished |
| 224 | + return this._superApply(arguments).always(function() { |
| 225 | + tree.nodeRenderTitle(ctx); |
| 226 | + }); |
| 227 | + }, |
| 228 | + |
| 229 | + // End of extension definition |
| 230 | + }); |
| 231 | + // Value returned by `require('jquery.fancytree..')` |
| 232 | + return $.ui.fancytree; |
| 233 | +}); // End of closure |
0 commit comments