PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Public/scripts/ThinkAjax.js

http://iiccms.googlecode.com/
JavaScript | 343 lines | 282 code | 6 blank | 55 comment | 43 complexity | 7a716b69957ee36ffb63924d79183e11 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. // +----------------------------------------------------------------------+
  2. // | ThinkPHP |
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2006 liu21st.com All rights reserved. |
  5. // +----------------------------------------------------------------------+
  6. // | Licensed under the Apache License, Version 2.0 (the 'License'); |
  7. // | you may not use this file except in compliance with the License. |
  8. // | You may obtain a copy of the License at |
  9. // | http://www.apache.org/licenses/LICENSE-2.0 |
  10. // | Unless required by applicable law or agreed to in writing, software |
  11. // | distributed under the License is distributed on an 'AS IS' BASIS, |
  12. // | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
  13. // | implied. See the License for the specific language governing |
  14. // | permissions and limitations under the License. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: liu21st <liu21st@gmail.com> |
  17. // +----------------------------------------------------------------------+
  18. // $Id$
  19. // Ajax for ThinkPHP
  20. document.write("<div id='ThinkAjaxResult' class='ThinkAjax' ></div>");
  21. var m = {
  22. '\b': '\\b',
  23. '\t': '\\t',
  24. '\n': '\\n',
  25. '\f': '\\f',
  26. '\r': '\\r'
  27. };
  28. var ThinkAjax = {
  29. method:'POST', // ??????
  30. bComplete:false, // ????
  31. updateTip:'?????...', // ?????????
  32. updateEffect:{'opacity': [0.1,0.85]}, // ????
  33. image:['','',''], // ?????? ?? ??? ?????
  34. tipTarget:'ThinkAjaxResult', // ??????
  35. showTip:true, // ?????????????
  36. status:0, //?????
  37. info:'', //????
  38. data:'', //????
  39. type:'', // JSON EVAL XML ...
  40. intval:0,
  41. options:{},
  42. debug:false,
  43. activeRequestCount:0,
  44. // Ajax?????
  45. getTransport: function() {
  46. return Try.these(
  47. function() {return new XMLHttpRequest()},
  48. function() {return new ActiveXObject('Msxml2.XMLHTTP')},
  49. function() {return new ActiveXObject('Microsoft.XMLHTTP')}
  50. ) || false;
  51. },
  52. tip:function (tips){
  53. this.options['tip'] = tips;
  54. return this;
  55. },
  56. effect:function (effect){
  57. this.options['effect'] = effect;
  58. return this;
  59. },
  60. target:function (taget){
  61. this.options['target'] = target;
  62. return this;
  63. },
  64. response:function (response){
  65. this.options['response'] = response;
  66. return this;
  67. },
  68. url:function (url){
  69. this.options['url'] = url;
  70. return this;
  71. },
  72. params:function (vars){
  73. this.options['var'] = vars;
  74. return this;
  75. },
  76. loading:function (target,tips,effect){
  77. if ($(target))
  78. {
  79. //var arrayPageSize = getPageSize();
  80. var arrayPageScroll = getPageScroll();
  81. $(target).style.display = 'block';
  82. $(target).style.top = (arrayPageScroll[1] + 'px');
  83. $(target).style.right = '5px';
  84. // ??????
  85. if ($('loader'))
  86. {
  87. $('loader').style.display = 'none';
  88. }
  89. if ('' != this.image[0])
  90. {
  91. $(target).innerHTML = '<IMG SRC="'+this.image[0]+'" BORDER="0" ALT="loading..." align="absmiddle"> '+tips;
  92. }else{
  93. $(target).innerHTML = tips;
  94. }
  95. //??????
  96. var myEffect = $(target).effects();
  97. myEffect.custom(effect);
  98. }
  99. },
  100. ajaxResponse:function(request,target,response){
  101. // ??ThinkPHP????Ajax?????
  102. // ????ThinkPHP????
  103. //alert(request.responseText);
  104. var str = request.responseText;
  105. str = str.replace(/([\x00-\x1f\\"])/g, function (a, b) {
  106. var c = m[b];
  107. if (c) {
  108. return c;
  109. }else{
  110. return b;
  111. }
  112. }) ;
  113. try{
  114. $return = eval('(' + str + ')');
  115. if (this.debug)
  116. {
  117. alert(str);
  118. }
  119. }catch(ex){
  120. if (this.debug)
  121. {
  122. alert("??????JS??:\n\n"+str.substr(0,100));
  123. }
  124. if ($(target) && this.showTip)
  125. {
  126. $(target).innerHTML = "?????????!";
  127. this.intval = window.setTimeout(function (){
  128. var myFx = new Fx.Style(target, 'opacity',{duration:1000}).custom(1,0);
  129. $(target).style.display='none';
  130. },3000);
  131. }
  132. return ;
  133. }
  134. /*
  135. if (this.debug)
  136. {
  137. // ????????eval?????
  138. alert(str);
  139. }
  140. try{
  141. $return = eval('(' + str + ')');
  142. }
  143. catch(e){alert('???????');return;}
  144. */
  145. this.status = $return.status;
  146. this.info = $return.info;
  147. this.data = $return.data;
  148. this.type = $return.type;
  149. if (this.type == 'EVAL' )
  150. {
  151. // ?????????
  152. eval($this.data);
  153. }else{
  154. // ??????
  155. // ????????ajaxReturn??
  156. if (response == undefined)
  157. {
  158. try {(ajaxReturn).apply(this,[this.data,this.status,this.info,this.type]);}
  159. catch (e){}
  160. }else {
  161. try { (response).apply(this,[this.data,this.status,this.info,this.type]);}
  162. catch (e){}
  163. }
  164. }
  165. if ($(target))
  166. {
  167. // ??????
  168. if (this.showTip && this.info!= undefined && this.info!=''){
  169. if (this.status==1)
  170. {
  171. if ('' != this.image[1])
  172. {
  173. $(target).innerHTML = '<IMG SRC="'+this.image[1]+'" BORDER="0" ALT="success..." align="absmiddle"> <span style="color:blue">'+this.info+'</span>';
  174. }else{
  175. $(target).innerHTML = '<span style="color:blue">'+this.info+'</span>';
  176. }
  177. }else{
  178. if ('' != this.image[2])
  179. {
  180. $(target).innerHTML = '<IMG SRC="'+this.image[2]+'" BORDER="0" ALT="error..." align="absmiddle"> <span style="color:red">'+this.info+'</span>';
  181. }else{
  182. $(target).innerHTML = '<span style="color:red">'+this.info+'</span>';
  183. }
  184. }
  185. }
  186. // ??????5?
  187. if (this.showTip)
  188. this.intval = window.setTimeout(function (){
  189. var myFx = new Fx.Style(target, 'opacity',{duration:1000}).custom(1,0);
  190. $(target).style.display='none';
  191. },3000);
  192. }
  193. },
  194. // ??Ajax??
  195. send:function(url,pars,response,target,tips,effect)
  196. {
  197. var xmlhttp = this.getTransport();
  198. url = (url == undefined)?this.options['url']:url;
  199. pars = (pars == undefined)?this.options['var']:pars;
  200. if (target == undefined) {
  201. target = (this.options['target'])?this.options['target']:this.tipTarget;
  202. }
  203. if (effect == undefined) {
  204. effect = (this.options['effect'])?this.options['effect']:this.updateEffect;
  205. }
  206. if (tips == undefined) {
  207. tips = (this.options['tip'])?this.options['tip']: this.updateTip;
  208. }
  209. if (this.showTip)
  210. {
  211. this.loading(target,tips,effect);
  212. }
  213. if (this.intval)
  214. {
  215. window.clearTimeout(this.intval);
  216. }
  217. this.activeRequestCount++;
  218. this.bComplete = false;
  219. try {
  220. if (this.method == "GET")
  221. {
  222. xmlhttp.open(this.method, url+"?"+pars, true);
  223. pars = "";
  224. }
  225. else
  226. {
  227. xmlhttp.open(this.method, url, true);
  228. xmlhttp.setRequestHeader("Method", "POST "+url+" HTTP/1.1");
  229. xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  230. }
  231. var _self = this;
  232. xmlhttp.onreadystatechange = function (){
  233. if (xmlhttp.readyState == 4 ){
  234. if( xmlhttp.status == 200 && !_self.bComplete)
  235. {
  236. _self.bComplete = true;
  237. _self.activeRequestCount--;
  238. _self.ajaxResponse(xmlhttp,target,response);
  239. }
  240. }
  241. }
  242. xmlhttp.send(pars);
  243. }
  244. catch(z) { return false; }
  245. },
  246. // ????Ajax????????????
  247. sendForm:function(formId,url,response,target,tips,effect)
  248. {
  249. vars = Form.serialize(formId);
  250. this.send(url,vars,response,target,tips,effect);
  251. },
  252. // ??Ajax?HTML?????
  253. // event ??????????
  254. // ?? focus blur mouseover mouseout mousedown mouseup submit click dblclick load change keypress keydown keyup
  255. bind:function(source,event,url,vars,response,target,tips,effect)
  256. {
  257. var _self = this;
  258. $(source).addEvent(event,function (){_self.send(url,vars,response,target,tips,effect)});
  259. },
  260. // ?????????Ajax??
  261. load:function(url,vars,response,target,tips,effect)
  262. {
  263. var _self = this;
  264. window.addEvent('load',function (){_self.send(url,vars,response,target,tips,effect)});
  265. },
  266. // ????Ajax??
  267. time:function(url,vars,time,response,target,tips,effect)
  268. {
  269. var _self = this;
  270. myTimer = window.setTimeout(function (){_self.send(url,vars,response,target,tips,effect)},time);
  271. },
  272. // ????Ajax??
  273. repeat:function(url,vars,intervals,response,target,tips,effect)
  274. {
  275. var _self = this;
  276. _self.send(url,vars,response,target,effect);
  277. myTimer = window.setInterval(function (){_self.send(url,vars,response,target,tips,effect)},intervals);
  278. },
  279. sendFile:function(id,url){
  280. var frame = this.createUploadIframe(id);
  281. var form = this.createUploadForm(id,url);
  282. if(form.encoding)
  283. {
  284. form.encoding = 'multipart/form-data';
  285. }
  286. else
  287. {
  288. form.enctype = 'multipart/form-data';
  289. }
  290. form.submit();
  291. },
  292. // ?????IFrame
  293. createUploadIframe: function(id, uri)
  294. {
  295. //create frame
  296. var frameId = 'ThinkUploadFrame' + id;
  297. if(window.ActiveXObject) {
  298. var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
  299. io.src = 'javascript:false';
  300. }else {
  301. var io = document.createElement('iframe');
  302. io.id = frameId;
  303. io.name = frameId;
  304. }
  305. io.style.position = 'absolute';
  306. io.style.top = '-1000px';
  307. io.style.left = '-1000px';
  308. io.style.display = 'none';
  309. document.body.appendChild(io);
  310. return io;
  311. },
  312. // ??????
  313. createUploadForm: function(id,url)
  314. {
  315. //create form
  316. var formId = 'ThinkUploadForm' + id;
  317. var fileId = 'ThinkUploadFile' + id;
  318. var form = document.createElement('form');
  319. form.method = 'POST';
  320. form.url = url;
  321. form.name = formId;
  322. form.id = formId;
  323. form.enctype = "multipart/form-data";
  324. form.target = 'ThinkUploadFrame' + id;
  325. //set attributes
  326. form.style.position = 'absolute';
  327. form.style.top = '-1200px';
  328. form.style.left = '-1200px';
  329. form.style.display = 'none';
  330. var fileElement = document.createElement('input');
  331. fileElement.type = 'file';
  332. fileElement.
  333. form.appendChild(fileElement);
  334. document.body.appendChild(form);
  335. return form;
  336. }
  337. }