zy_json.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. var zy_json_opid=1000;
  2. var zy_json_session=new Object;
  3. (function(g, h) {
  4. var j = g.document, navigator = g.navigator, location = g.location;
  5. var k = (function() {
  6. var e = function(a, b) {
  7. return new e.fn.init(a, b, rootjQuery)
  8. }, _jQuery = g.jQuery, _$ = g.$, rootjQuery, quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/, toString = Object.prototype.toString;
  9. e.fn = e.prototype = {
  10. constructor : e,
  11. init : function(a, b, c) {
  12. var d, elem, ret, doc;
  13. return this
  14. },
  15. selector : "",
  16. jquery : "1.6",
  17. length : 0,
  18. size : function() {
  19. return this.length
  20. },
  21. toArray : function() {
  22. return slice.call(this, 0)
  23. }
  24. };
  25. e.fn.init.prototype = e.fn;
  26. e.extend = e.fn.extend = function() {
  27. var a, name, src, copy, copyIsArray, clone, target = arguments[0]
  28. || {}, i = 1, length = arguments.length, deep = false;
  29. if (typeof target === "boolean") {
  30. deep = target;
  31. target = arguments[1] || {};
  32. i = 2
  33. }
  34. if (typeof target !== "object" && !e.isFunction(target)) {
  35. target = {}
  36. }
  37. if (length === i) {
  38. target = this;
  39. --i
  40. }
  41. for (; i < length; i++) {
  42. if ((a = arguments[i]) != null) {
  43. for (name in a) {
  44. src = target[name];
  45. copy = a[name];
  46. if (target === copy) {
  47. continue
  48. }
  49. if (deep
  50. && copy
  51. && (e.isPlainObject(copy) || (copyIsArray = e
  52. .isArray(copy)))) {
  53. if (copyIsArray) {
  54. copyIsArray = false;
  55. clone = src && e.isArray(src) ? src : []
  56. } else {
  57. clone = src && e.isPlainObject(src) ? src : {}
  58. }
  59. target[name] = e.extend(deep, clone, copy)
  60. } else if (copy !== h) {
  61. target[name] = copy
  62. }
  63. }
  64. }
  65. }
  66. return target
  67. };
  68. e.extend({
  69. noConflict : function(a) {
  70. if (g.$ === e) {
  71. g.$ = _$
  72. }
  73. if (a && g.jQuery === e) {
  74. g.jQuery = _jQuery
  75. }
  76. return e
  77. },
  78. isPlainObject : function(a) {
  79. if (!a || e.type(a) !== "object" || a.nodeType
  80. || e.isWindow(a)) {
  81. return false
  82. }
  83. if (a.constructor
  84. && !hasOwn.call(a, "constructor")
  85. && !hasOwn.call(a.constructor.prototype,
  86. "isPrototypeOf")) {
  87. return false
  88. }
  89. var b;
  90. for (b in a) {
  91. }
  92. return b === h || hasOwn.call(a, b)
  93. },
  94. noop : function() {
  95. },
  96. guid : 1
  97. });
  98. rootjQuery = e(j);
  99. return e
  100. })();
  101. k.extend({
  102. ajaxJSONP : function(b) {
  103. var c = 'jsonp' + (++k.guid), script = j.createElement('script');
  104. g[c] = function(a) {
  105. b.success(a);
  106. delete g[c]
  107. };
  108. script.src = b.url.replace(/=\?/, '=' + c);
  109. j.getElementsByTagName("head")[0].appendChild(script)
  110. },
  111. ajaxSettings : {
  112. type : 'GET',
  113. beforeSend : k.noop,
  114. success : k.noop,
  115. error : k.noop,
  116. complete : k.noop,
  117. accepts : {
  118. script : 'text/javascript, application/javascript',
  119. json : 'application/json',
  120. xml : 'application/xml, text/xml',
  121. html : 'text/html',
  122. text : 'text/plain'
  123. },
  124. headers : []
  125. },
  126. ajax : function(b) {
  127. var c = k.extend({}, b);
  128. for (key in k.ajaxSettings)
  129. if (!c[key])
  130. c[key] = k.ajaxSettings[key];
  131. if (/=\?/.test(c.url))
  132. return k.ajaxJSONP(c);
  133. if (!c.url)
  134. c.url = g.location.toString();
  135. if (c.data && !c.contentType)
  136. c.contentType = 'application/x-www-form-urlencoded';
  137. if (k.isPlainObject(c.data))
  138. c.data = k.param(c.data);
  139. if (c.type.match(/get/i) && c.data) {
  140. var d = c.data;
  141. if (c.url.match(/\?.*=/)) {
  142. d = '&' + d
  143. } else if (d[0] != '?') {
  144. d = '?' + d
  145. }
  146. c.url += d
  147. }
  148. var f = c.accepts[c.dataType], xhr = new XMLHttpRequest();
  149. if (!!xhr.onprogress) {
  150. xhr.onprogress = function(a) {
  151. if (c.downCallback) {
  152. c.downCallback(xhr, a)
  153. } else {
  154. return function() {
  155. }
  156. }
  157. }
  158. }
  159. xhr.onreadystatechange = function() {
  160. if (c.readyCallback) {
  161. c.readyCallback(xhr);
  162. return
  163. }
  164. if (xhr.readyState == 4) {
  165. var a, error = false;
  166. if ((xhr.status >= 200 && xhr.status < 300)
  167. || xhr.status == 0) {
  168. if (f == 'application/json') {
  169. try {
  170. a = JSON.parse(xhr.responseText)
  171. } catch (e) {
  172. error = e
  173. }
  174. } else
  175. a = xhr.responseText;
  176. if (error)
  177. c.error(xhr, 'parsererror', error);
  178. else
  179. c.success(a, 'success', xhr)
  180. } else {
  181. error = true;
  182. c.error(xhr, 'error')
  183. }
  184. c.complete(xhr, error ? 'error' : 'success')
  185. }
  186. };
  187. xhr.open(c.type, c.url, true);
  188. if (c.beforeSend(xhr, c) === false) {
  189. xhr.abort();
  190. return false
  191. }
  192. if (c.contentType)
  193. c.headers['Content-Type'] = c.contentType;
  194. for (name in c.headers)
  195. xhr.setRequestHeader(name, c.contentType);
  196. xhr.send(c.data);
  197. return xhr
  198. },
  199. clearLS:function(a)
  200. {
  201. if(window.localStorage)
  202. {
  203. if(a)
  204. window.localStorage.removeItem(a);
  205. else
  206. window.localStorage.clear();
  207. }
  208. },
  209. getJSON : function(a, b,c,d,e,f,g) {
  210. c = c ||'json';
  211. if(window.location.href.substring(0,7).toLowerCase()=="file://")
  212. {
  213. if(a.substring(0,4).toLowerCase()=="http")
  214. {
  215. uexXmlHttpMgr.onData = function (_opid, status, result)
  216. {
  217. var str = result;
  218. var st = parseInt(status);
  219. var json = '';
  220. if(st==0) return;
  221. else if(st==-1)
  222. {
  223. str = '{"status":"-1", "message":"network error!"}';
  224. str = JSON.parse(str);
  225. uexXmlHttpMgr.close(_opid);
  226. if(!(zy_json_session[_opid].errcb===undefined))
  227. zy_json_session[_opid].errcb(str);
  228. delete zy_json_session[_opid];
  229. return;
  230. }
  231. else if(result.indexOf('?(')>=0)
  232. {
  233. var s = result.indexOf('?(');
  234. var e = result.lastIndexOf(")");
  235. str = result.substring(s+2,e);
  236. }
  237. if(c.toLowerCase()=="json"){
  238. try{
  239. json = JSON.parse(str);
  240. }catch(s){
  241. str = '{"status":"-1", "message":"json parse failed!"}';
  242. str = JSON.parse(str);
  243. uexXmlHttpMgr.close(_opid);
  244. if(!(zy_json_session[_opid].errcb===undefined))
  245. zy_json_session[_opid].errcb(str);
  246. delete zy_json_session[_opid];
  247. return;
  248. }
  249. }
  250. else
  251. json = result;
  252. if(zy_json_session[_opid].ol && window.localStorage)
  253. {
  254. window.localStorage[zy_json_session[_opid].url]=str;
  255. }
  256. uexXmlHttpMgr.close(_opid);
  257. if(!(zy_json_session[_opid].cb===undefined))
  258. zy_json_session[_opid].cb(json);
  259. delete zy_json_session[_opid];
  260. }
  261. if(g && window.localStorage)
  262. {
  263. var r=window.localStorage[a];
  264. if(r)
  265. {
  266. r=(c.toLowerCase()=="json")?JSON.parse(r):r;
  267. if(r)
  268. return b(r);
  269. }
  270. }
  271. zy_json_opid++;
  272. zy_json_session[zy_json_opid] = new Object;
  273. zy_json_session[zy_json_opid].cb=b;
  274. zy_json_session[zy_json_opid].errcb= d;
  275. zy_json_session[zy_json_opid].ol=g;
  276. zy_json_session[zy_json_opid].url=a;
  277. uexXmlHttpMgr.open(zy_json_opid, !e?"GET":e, a,60000);
  278. if(f)
  279. {
  280. for(var it in f)
  281. {
  282. uexXmlHttpMgr.setPostData(zy_json_opid,f[it].type,f[it].key,f[it].value);
  283. }
  284. }
  285. uexXmlHttpMgr.send(zy_json_opid);
  286. }
  287. else
  288. {
  289. zy_json_opid++;
  290. zy_json_session[zy_json_opid] = new Object;
  291. zy_json_session[zy_json_opid].cb=b;
  292. zy_json_session[zy_json_opid].errcb= d;
  293. uexWidgetOne.cbError= function(inOpId,inErrorCode,inErrorDes){
  294. {
  295. var str = '{"status":"-1", "message":"read file failed!"}';
  296. str = JSON.parse(str);
  297. uexFileMgr.closeFile(inOpId);
  298. if(!(zy_json_session[inOpId].errcb===undefined))
  299. zy_json_session[inOpId].errcb(str);
  300. delete zy_json_session[inOpId];
  301. return;
  302. }
  303. }
  304. uexFileMgr.cbOpenFile = function(opId,dataType,data){
  305. uexFileMgr.cbReadFile = function(_opid,_Type, _data){
  306. if(_Type==0){
  307. var json = _data;
  308. if(c.toLowerCase()=="json")
  309. {
  310. try{
  311. json = JSON.parse(json);
  312. }catch(s){
  313. var str = '{"status":"-1", "message":"json parse failed!"}';
  314. str = JSON.parse(str);
  315. uexFileMgr.closeFile(_opid);
  316. if(!(zy_json_session[_opid].errcb===undefined))
  317. zy_json_session[_opid].errcb(str);
  318. delete zy_json_session[_opid];
  319. return;
  320. }
  321. }
  322. uexFileMgr.closeFile(_opid);
  323. if(!(zy_json_session[_opid].cb===undefined))
  324. zy_json_session[_opid].cb(json);
  325. delete zy_json_session[_opid];
  326. }
  327. }
  328. uexFileMgr.readFile(opId,-1);
  329. }
  330. uexFileMgr.cbIsFileExistByPath = function(opId,dataType,data){
  331. if(dataType==2){
  332. if(data==1){
  333. uexFileMgr.openFile(zy_json_opid,a,1);
  334. }
  335. else{
  336. var str = '{"status":"-1", "message":"file does not exist!"}';
  337. str = JSON.parse(str);
  338. if(!(zy_json_session[inOpId].errcb===undefined))
  339. zy_json_session[inOpId].errcb(str);
  340. delete zy_json_session[inOpId];
  341. return;
  342. }
  343. }
  344. }
  345. uexFileMgr.isFileExistByPath(a);
  346. }
  347. }
  348. else
  349. {
  350. k.ajax({
  351. url : a,
  352. success : b,
  353. dataType : c,
  354. error: d
  355. });
  356. }
  357. }
  358. });
  359. g.jQuery = g.$ = k
  360. })(window);