Skip to content

Commit 6fa88df

Browse files
committed
Support async loading with providing Promises or thenable functions
Other: - Support `setting.async.loadingIcon` for controlling to show or hide loading icons. - Adapt `setting.async.{autoParam, otherParam, contentType, type, url, dataType, headers, xhrFields}` to `setting.async.load`
1 parent dd003eb commit 6fa88df

File tree

1 file changed

+86
-69
lines changed

1 file changed

+86
-69
lines changed

js/jquery.ztree.core.js

Lines changed: 86 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,6 @@
10451045
nObj.append(html.join(''));
10461046
},
10471047
asyncNode: function (setting, node, isSilent, callback) {
1048-
var i, l;
10491048
var isParent = data.nodeIsParent(setting, node);
10501049
if (node && !isParent) {
10511050
tools.apply(callback);
@@ -1058,80 +1057,98 @@
10581057
}
10591058
if (node) {
10601059
node.isAjaxing = true;
1061-
var icoObj = $$(node, consts.id.ICON, setting);
1062-
icoObj.attr({"style": "", "class": consts.className.BUTTON + " " + consts.className.ICO_LOADING});
1063-
}
1064-
1065-
var tmpParam = {};
1066-
var autoParam = tools.apply(setting.async.autoParam, [setting.treeId, node], setting.async.autoParam);
1067-
for (i = 0, l = autoParam.length; node && i < l; i++) {
1068-
var pKey = autoParam[i].split("="), spKey = pKey;
1069-
if (pKey.length > 1) {
1070-
spKey = pKey[1];
1071-
pKey = pKey[0];
1060+
if (setting.async.loadingIcon === false) {
1061+
var icoObj = $$(node, consts.id.ICON, setting);
1062+
icoObj.attr({"style": "", "class": consts.className.BUTTON + " " + consts.className.ICO_LOADING});
10721063
}
1073-
tmpParam[spKey] = node[pKey];
10741064
}
1075-
var otherParam = tools.apply(setting.async.otherParam, [setting.treeId, node], setting.async.otherParam);
1076-
if (tools.isArray(otherParam)) {
1077-
for (i = 0, l = otherParam.length; i < l; i += 2) {
1078-
tmpParam[otherParam[i]] = otherParam[i + 1];
1065+
1066+
if (setting.async.url) {
1067+
// compatible for ajax
1068+
var i, l;
1069+
var tmpParam = {};
1070+
var autoParam = tools.apply(setting.async.autoParam, [setting.treeId, node], setting.async.autoParam);
1071+
for (i = 0, l = autoParam.length; node && i < l; i++) {
1072+
var pKey = autoParam[i].split("="), spKey = pKey;
1073+
if (pKey.length > 1) {
1074+
spKey = pKey[1];
1075+
pKey = pKey[0];
1076+
}
1077+
tmpParam[spKey] = node[pKey];
10791078
}
1080-
} else {
1081-
for (var p in otherParam) {
1082-
tmpParam[p] = otherParam[p];
1079+
var otherParam = tools.apply(setting.async.otherParam, [setting.treeId, node], setting.async.otherParam);
1080+
if (tools.isArray(otherParam)) {
1081+
for (i = 0, l = otherParam.length; i < l; i += 2) {
1082+
tmpParam[otherParam[i]] = otherParam[i + 1];
1083+
}
1084+
} else {
1085+
for (var p in otherParam) {
1086+
tmpParam[p] = otherParam[p];
1087+
}
10831088
}
1089+
1090+
setting.async.load = function (treeId, node) {
1091+
var deferred = $.Deferred();
1092+
$.ajax({
1093+
contentType: setting.async.contentType,
1094+
cache: false,
1095+
type: setting.async.type,
1096+
url: tools.apply(setting.async.url, [treeId, node], setting.async.url),
1097+
data: setting.async.contentType.indexOf('application/json') > -1 ? JSON.stringify(tmpParam) : tmpParam,
1098+
dataType: setting.async.dataType,
1099+
headers: setting.async.headers,
1100+
xhrFields: setting.async.xhrFields,
1101+
success: function (msg) {
1102+
var newNodes = [];
1103+
try {
1104+
if (!msg || msg.length == 0) {
1105+
newNodes = [];
1106+
} else if (typeof msg == "string") {
1107+
newNodes = eval("(" + msg + ")");
1108+
} else {
1109+
newNodes = msg;
1110+
}
1111+
} catch (err) {
1112+
newNodes = msg;
1113+
}
1114+
1115+
deferred.resolve(newNodes);
1116+
},
1117+
error: function (XMLHttpRequest, textStatus, errorThrown) {
1118+
deferred.reject([XMLHttpRequest, textStatus, errorThrown]);
1119+
}
1120+
});
1121+
1122+
return deferred.promise();
1123+
};
10841124
}
10851125

10861126
var _tmpV = data.getRoot(setting)._ver;
1087-
$.ajax({
1088-
contentType: setting.async.contentType,
1089-
cache: false,
1090-
type: setting.async.type,
1091-
url: tools.apply(setting.async.url, [setting.treeId, node], setting.async.url),
1092-
data: setting.async.contentType.indexOf('application/json') > -1 ? JSON.stringify(tmpParam) : tmpParam,
1093-
dataType: setting.async.dataType,
1094-
headers: setting.async.headers,
1095-
xhrFields: setting.async.xhrFields,
1096-
success: function (msg) {
1097-
if (_tmpV != data.getRoot(setting)._ver) {
1098-
return;
1099-
}
1100-
var newNodes = [];
1101-
try {
1102-
if (!msg || msg.length == 0) {
1103-
newNodes = [];
1104-
} else if (typeof msg == "string") {
1105-
newNodes = eval("(" + msg + ")");
1106-
} else {
1107-
newNodes = msg;
1108-
}
1109-
} catch (err) {
1110-
newNodes = msg;
1111-
}
1127+
setting.async.load(setting.treeId, node).then(function (newNodes) {
1128+
if (_tmpV != data.getRoot(setting)._ver) {
1129+
return;
1130+
}
11121131

1113-
if (node) {
1114-
node.isAjaxing = null;
1115-
node.zAsync = true;
1116-
}
1117-
view.setNodeLineIcos(setting, node);
1118-
if (newNodes && newNodes !== "") {
1119-
newNodes = tools.apply(setting.async.dataFilter, [setting.treeId, node, newNodes], newNodes);
1120-
view.addNodes(setting, node, -1, !!newNodes ? tools.clone(newNodes) : [], !!isSilent);
1121-
} else {
1122-
view.addNodes(setting, node, -1, [], !!isSilent);
1123-
}
1124-
setting.treeObj.trigger(consts.event.ASYNC_SUCCESS, [setting.treeId, node, msg]);
1125-
tools.apply(callback);
1126-
},
1127-
error: function (XMLHttpRequest, textStatus, errorThrown) {
1128-
if (_tmpV != data.getRoot(setting)._ver) {
1129-
return;
1130-
}
1131-
if (node) node.isAjaxing = null;
1132-
view.setNodeLineIcos(setting, node);
1133-
setting.treeObj.trigger(consts.event.ASYNC_ERROR, [setting.treeId, node, XMLHttpRequest, textStatus, errorThrown]);
1132+
if (node) {
1133+
node.isAjaxing = null;
1134+
node.zAsync = true;
1135+
}
1136+
view.setNodeLineIcos(setting, node);
1137+
if (newNodes && newNodes !== "") {
1138+
newNodes = tools.apply(setting.async.dataFilter, [setting.treeId, node, newNodes], newNodes);
1139+
view.addNodes(setting, node, -1, !!newNodes ? tools.clone(newNodes) : [], !!isSilent);
1140+
} else {
1141+
view.addNodes(setting, node, -1, [], !!isSilent);
1142+
}
1143+
setting.treeObj.trigger(consts.event.ASYNC_SUCCESS, [setting.treeId, node, newNodes]);
1144+
tools.apply(callback);
1145+
}, function (errors) {
1146+
if (_tmpV != data.getRoot(setting)._ver) {
1147+
return;
11341148
}
1149+
if (node) node.isAjaxing = null;
1150+
view.setNodeLineIcos(setting, node);
1151+
setting.treeObj.trigger(consts.event.ASYNC_ERROR, [setting.treeId, node].concat(errors));
11351152
});
11361153
return true;
11371154
},
@@ -1342,7 +1359,7 @@
13421359
fontStyle.push(f, ":", fontcss[f], ";");
13431360
}
13441361
html.push("<a id='", node.tId, consts.id.A, "' class='", consts.className.LEVEL, node.level,
1345-
nodeClasses.add ? ' ' + nodeClasses.add.join(' ') : '',
1362+
nodeClasses.add ? ' ' + nodeClasses.add.join(' ') : '',
13461363
"' treeNode", consts.id.A,
13471364
node.click ? " onclick=\"" + node.click + "\"" : "",
13481365
((url != null && url.length > 0) ? " href='" + url + "'" : ""), " target='", view.makeNodeTarget(node), "' style='", fontStyle.join(''),
@@ -2006,7 +2023,7 @@
20062023
var children = data.nodeChildren(setting, root);
20072024
if (children && children.length > 0) {
20082025
view.createNodes(setting, 0, children, null, -1);
2009-
} else if (setting.async.enable && setting.async.url && setting.async.url !== '') {
2026+
} else if (setting.async.enable && (!setting.async.url || (setting.async.load && setting.async.load.then))) {
20102027
view.asyncNode(setting);
20112028
}
20122029
return zTreeTools;
@@ -2016,4 +2033,4 @@
20162033
var zt = $.fn.zTree,
20172034
$$ = tools.$,
20182035
consts = zt.consts;
2019-
})(jQuery);
2036+
})(jQuery);

0 commit comments

Comments
 (0)