Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aui-tree-io.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. AUI.add('aui-tree-io', function(A) {
  2. var Lang = A.Lang,
  3. isFunction = Lang.isFunction,
  4. isString = Lang.isString,
  5. EVENT_IO_REQUEST_SUCCESS = 'ioRequestSuccess',
  6. CONTENT_BOX = 'contentBox',
  7. IO = 'io',
  8. OWNER_TREE = 'ownerTree',
  9. LOADED = 'loaded',
  10. LOADING = 'loading',
  11. NODE = 'node',
  12. TREE = 'tree',
  13. getCN = A.getClassName,
  14. CSS_TREE_NODE_IO_LOADING = getCN(TREE, NODE, IO, LOADING);
  15. function TreeViewIO(config) {
  16. var instance = this;
  17. instance.publish(
  18. EVENT_IO_REQUEST_SUCCESS,
  19. {
  20. defaultFn: instance._onIOSuccessDefault
  21. }
  22. );
  23. }
  24. TreeViewIO.ATTRS = {
  25. /**
  26. * IO options for the current TreeNode load the children.
  27. *
  28. * @attribute io
  29. * @default Default IO Configuration.
  30. * @type Object
  31. */
  32. io: {
  33. lazyAdd: false,
  34. value: null,
  35. setter: function(v) {
  36. return this._setIO(v);
  37. }
  38. }
  39. };
  40. TreeViewIO.prototype = {
  41. initializer: function() {
  42. var instance = this;
  43. instance.publish(
  44. );
  45. },
  46. /**
  47. * Create nodes.
  48. *
  49. * @method createNodes
  50. * @param nodes
  51. */
  52. createNodes: function(nodes) {
  53. var instance = this;
  54. var paginator = instance.get('paginator');
  55. A.Array.each(
  56. A.Array(nodes),
  57. function(node) {
  58. var childrenLength = instance.getChildrenLength();
  59. if (paginator && paginator.total <= childrenLength) {
  60. return;
  61. }
  62. instance.appendChild(
  63. instance.createNode(node)
  64. );
  65. }
  66. );
  67. instance._syncPaginatorUI(nodes);
  68. },
  69. /**
  70. * Initialize the IO transaction setup on the <a
  71. * href="TreeNode.html#config_io">io</a> attribute.
  72. *
  73. * @method initIO
  74. */
  75. initIO: function() {
  76. var instance = this;
  77. var io = instance.get(IO);
  78. if (isFunction(io.cfg.data)) {
  79. io.cfg.data = io.cfg.data.call(instance, instance);
  80. }
  81. instance._syncPaginatorIOData(io);
  82. if (isFunction(io.loader)) {
  83. var loader = A.bind(io.loader, instance);
  84. // apply loader in the TreeNodeIO scope
  85. loader(io.url, io.cfg, instance);
  86. }
  87. else {
  88. A.io.request(io.url, io.cfg);
  89. }
  90. },
  91. /**
  92. * IO Start handler.
  93. *
  94. * @method ioStartHandler
  95. */
  96. ioStartHandler: function() {
  97. var instance = this;
  98. var contentBox = instance.get(CONTENT_BOX);
  99. instance.set(LOADING, true);
  100. contentBox.addClass(CSS_TREE_NODE_IO_LOADING);
  101. },
  102. /**
  103. * IO Complete handler.
  104. *
  105. * @method ioCompleteHandler
  106. */
  107. ioCompleteHandler: function() {
  108. var instance = this;
  109. var contentBox = instance.get(CONTENT_BOX);
  110. instance.set(LOADING, false);
  111. instance.set(LOADED, true);
  112. contentBox.removeClass(CSS_TREE_NODE_IO_LOADING);
  113. },
  114. /**
  115. * IO Success handler.
  116. *
  117. * @method ioSuccessHandler
  118. */
  119. ioSuccessHandler: function() {
  120. var instance = this;
  121. var io = instance.get(IO);
  122. var args = Array.prototype.slice.call(arguments);
  123. var length = args.length;
  124. // if using the first argument as the JSON object
  125. var nodes = args[1];
  126. // if using (event, id, o) yui callback syntax
  127. if (length >= 3) {
  128. var o = args[2];
  129. // try to convert responseText to JSON
  130. try {
  131. nodes = A.JSON.parse(o.responseText);
  132. }
  133. catch(e) {}
  134. }
  135. var formatter = io.formatter;
  136. if (formatter) {
  137. nodes = formatter(nodes);
  138. }
  139. instance.createNodes(nodes);
  140. instance.fire(EVENT_IO_REQUEST_SUCCESS, nodes);
  141. },
  142. /**
  143. * IO Failure handler.
  144. *
  145. * @method ioFailureHandler
  146. */
  147. ioFailureHandler: function() {
  148. var instance = this;
  149. instance.fire('ioRequestFailure');
  150. instance.set(LOADING, false);
  151. instance.set(LOADED, false);
  152. },
  153. _onIOSuccessDefault: function(event) {
  154. var instance = this;
  155. var ownerTree = instance.get(OWNER_TREE);
  156. if (ownerTree && ownerTree.ddDelegate) {
  157. ownerTree.ddDelegate.syncTargets();
  158. }
  159. },
  160. /**
  161. * Setter for <a href="TreeNodeIO.html#config_io">io</a>.
  162. *
  163. * @method _setIO
  164. * @protected
  165. * @param {Object} v
  166. * @return {Object}
  167. */
  168. _setIO: function(v) {
  169. var instance = this;
  170. if (!v) {
  171. return null;
  172. }
  173. else if (isString(v)) {
  174. v = { url: v };
  175. }
  176. v = v || {};
  177. v.cfg = v.cfg || {};
  178. v.cfg.on = v.cfg.on || {};
  179. var defCallbacks = {
  180. start: A.bind(instance.ioStartHandler, instance),
  181. complete: A.bind(instance.ioCompleteHandler, instance),
  182. success: A.bind(instance.ioSuccessHandler, instance),
  183. failure: A.bind(instance.ioFailureHandler, instance)
  184. };
  185. A.each(defCallbacks, function(fn, name) {
  186. var userFn = v.cfg.on[name];
  187. fn.defaultFn = true;
  188. if (isFunction(userFn)) {
  189. // wrapping user callback and default callback, invoking both handlers
  190. var wrappedFn = A.bind(
  191. function() {
  192. fn.apply(instance, arguments);
  193. userFn.apply(instance, arguments);
  194. },
  195. instance
  196. );
  197. wrappedFn.wrappedFn = true;
  198. v.cfg.on[name] = wrappedFn;
  199. }
  200. else {
  201. // get from defCallbacks map
  202. v.cfg.on[name] = fn;
  203. }
  204. });
  205. return v;
  206. }
  207. };
  208. A.TreeViewIO = TreeViewIO;
  209. }, '@VERSION@' ,{requires:['aui-io','json'], skinnable:false});