| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- AUI.add('aui-tree-io', function(A) {
- var Lang = A.Lang,
- isFunction = Lang.isFunction,
- isString = Lang.isString,
-
- EVENT_IO_REQUEST_SUCCESS = 'ioRequestSuccess',
-
- CONTENT_BOX = 'contentBox',
- IO = 'io',
- OWNER_TREE = 'ownerTree',
- LOADED = 'loaded',
- LOADING = 'loading',
- NODE = 'node',
- TREE = 'tree',
-
- getCN = A.getClassName,
-
- CSS_TREE_NODE_IO_LOADING = getCN(TREE, NODE, IO, LOADING);
-
- function TreeViewIO(config) {
- var instance = this;
-
- instance.publish(
- EVENT_IO_REQUEST_SUCCESS,
- {
- defaultFn: instance._onIOSuccessDefault
- }
- );
- }
-
-
- TreeViewIO.ATTRS = {
- /**
- * IO options for the current TreeNode load the children.
- *
- * @attribute io
- * @default Default IO Configuration.
- * @type Object
- */
- io: {
- lazyAdd: false,
- value: null,
- setter: function(v) {
- return this._setIO(v);
- }
- }
- };
-
- TreeViewIO.prototype = {
- initializer: function() {
- var instance = this;
-
- instance.publish(
-
- );
- },
-
- /**
- * Create nodes.
- *
- * @method createNodes
- * @param nodes
- */
- createNodes: function(nodes) {
- var instance = this;
-
- var paginator = instance.get('paginator');
-
- A.Array.each(
- A.Array(nodes),
- function(node) {
- var childrenLength = instance.getChildrenLength();
-
- if (paginator && paginator.total <= childrenLength) {
- return;
- }
-
- instance.appendChild(
- instance.createNode(node)
- );
- }
- );
-
- instance._syncPaginatorUI(nodes);
- },
-
- /**
- * Initialize the IO transaction setup on the <a
- * href="TreeNode.html#config_io">io</a> attribute.
- *
- * @method initIO
- */
- initIO: function() {
- var instance = this;
-
- var io = instance.get(IO);
-
- if (isFunction(io.cfg.data)) {
- io.cfg.data = io.cfg.data.call(instance, instance);
- }
-
- instance._syncPaginatorIOData(io);
-
- if (isFunction(io.loader)) {
- var loader = A.bind(io.loader, instance);
-
- // apply loader in the TreeNodeIO scope
- loader(io.url, io.cfg, instance);
- }
- else {
- A.io.request(io.url, io.cfg);
- }
- },
-
- /**
- * IO Start handler.
- *
- * @method ioStartHandler
- */
- ioStartHandler: function() {
- var instance = this;
-
- var contentBox = instance.get(CONTENT_BOX);
-
- instance.set(LOADING, true);
-
- contentBox.addClass(CSS_TREE_NODE_IO_LOADING);
- },
-
- /**
- * IO Complete handler.
- *
- * @method ioCompleteHandler
- */
- ioCompleteHandler: function() {
- var instance = this;
-
- var contentBox = instance.get(CONTENT_BOX);
-
- instance.set(LOADING, false);
- instance.set(LOADED, true);
-
- contentBox.removeClass(CSS_TREE_NODE_IO_LOADING);
- },
-
- /**
- * IO Success handler.
- *
- * @method ioSuccessHandler
- */
- ioSuccessHandler: function() {
- var instance = this;
-
- var io = instance.get(IO);
-
- var args = Array.prototype.slice.call(arguments);
- var length = args.length;
-
- // if using the first argument as the JSON object
- var nodes = args[1];
-
- // if using (event, id, o) yui callback syntax
- if (length >= 3) {
- var o = args[2];
- // try to convert responseText to JSON
- try {
- nodes = A.JSON.parse(o.responseText);
- }
- catch(e) {}
- }
-
- var formatter = io.formatter;
-
- if (formatter) {
- nodes = formatter(nodes);
- }
-
- instance.createNodes(nodes);
-
- instance.fire(EVENT_IO_REQUEST_SUCCESS, nodes);
- },
-
- /**
- * IO Failure handler.
- *
- * @method ioFailureHandler
- */
- ioFailureHandler: function() {
- var instance = this;
-
- instance.fire('ioRequestFailure');
-
- instance.set(LOADING, false);
- instance.set(LOADED, false);
- },
-
- _onIOSuccessDefault: function(event) {
- var instance = this;
-
- var ownerTree = instance.get(OWNER_TREE);
-
- if (ownerTree && ownerTree.ddDelegate) {
- ownerTree.ddDelegate.syncTargets();
- }
- },
-
- /**
- * Setter for <a href="TreeNodeIO.html#config_io">io</a>.
- *
- * @method _setIO
- * @protected
- * @param {Object} v
- * @return {Object}
- */
- _setIO: function(v) {
- var instance = this;
-
- if (!v) {
- return null;
- }
- else if (isString(v)) {
- v = { url: v };
- }
-
- v = v || {};
- v.cfg = v.cfg || {};
- v.cfg.on = v.cfg.on || {};
-
- var defCallbacks = {
- start: A.bind(instance.ioStartHandler, instance),
- complete: A.bind(instance.ioCompleteHandler, instance),
- success: A.bind(instance.ioSuccessHandler, instance),
- failure: A.bind(instance.ioFailureHandler, instance)
- };
-
- A.each(defCallbacks, function(fn, name) {
- var userFn = v.cfg.on[name];
-
- fn.defaultFn = true;
-
- if (isFunction(userFn)) {
- // wrapping user callback and default callback, invoking both handlers
- var wrappedFn = A.bind(
- function() {
- fn.apply(instance, arguments);
- userFn.apply(instance, arguments);
- },
- instance
- );
-
- wrappedFn.wrappedFn = true;
-
- v.cfg.on[name] = wrappedFn;
- }
- else {
- // get from defCallbacks map
- v.cfg.on[name] = fn;
- }
-
- });
-
- return v;
- }
- };
-
- A.TreeViewIO = TreeViewIO;
-
- }, '@VERSION@' ,{requires:['aui-io','json'], skinnable:false});
|