PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/js/jquery/jquery.helper.js

https://github.com/robertleeplummerjr/bluebox
JavaScript | 390 lines | 257 code | 37 blank | 96 comment | 112 complexity | 2779b67ae04f7865e347c5c5a093d6a3 MD5 | raw file
  1. /*
  2. * jQuery PHP Plugin
  3. * version: 0.8.3 (16/03/2009)
  4. * author: Anton Shevchuk (http://anton.shevchuk.name)
  5. * @requires jQuery v1.2.1 or later
  6. *
  7. * Examples and documentation at: http://jquery.hohli.com/
  8. * Dual licensed under the MIT and GPL licenses:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. * http://www.gnu.org/licenses/gpl.html
  11. *
  12. * Revision: $Id$
  13. */
  14. (function($) {
  15. $.extend({
  16. php: function (url, params) {
  17. // do an ajax post request
  18. $.ajax({
  19. // AJAX-specified URL
  20. url: url,
  21. // JSON
  22. type: "POST",
  23. data: params,
  24. dataType : "json",
  25. /* Handlers */
  26. // Handle the beforeSend event
  27. beforeSend: function(){
  28. return php.beforeSend();
  29. },
  30. // Handle the success event
  31. success: function(data, textStatus){
  32. return php.success(data, textStatus);
  33. },
  34. // Handle the error event
  35. error: function (xmlEr, typeEr, except) {
  36. return php.error(xmlEr, typeEr, except);
  37. },
  38. // Handle the complete event
  39. complete: function (XMLHttpRequest, textStatus) {
  40. return php.complete(XMLHttpRequest, textStatus);
  41. }
  42. });
  43. }
  44. });
  45. php = {
  46. /**
  47. * beforeSend
  48. */
  49. beforeSend:function() {
  50. return true;
  51. },
  52. /**
  53. * success
  54. * parse AJAX response
  55. * @param object response
  56. * @param string textStatus
  57. */
  58. success:function (response, textStatus) {
  59. // call jQuery methods
  60. for (var i=0;i<response['q'].length; i++) {
  61. var selector = $(response['q'][i]['s']);
  62. var methods = response['q'][i]['m'];
  63. var arguments = response['q'][i]['a'];
  64. for (var j=0;j<methods.length; j++) {
  65. try {
  66. var method = methods[j];
  67. var argument = arguments[j];
  68. /**
  69. * This section added by K Anderson. The intent it to convert all
  70. * responses into a function if avaliable in the window or attempt
  71. * to convert to objects. In this way all arguments are standardized.
  72. *
  73. * TODO: Write some protection/security for the eval
  74. * TODO: Currently you can not have a string and window.function() have the same name!
  75. */
  76. funcIdentifier = 'function';
  77. for (var k = 0; k < argument.length; k++)
  78. {
  79. arg = argument[k];
  80. if (typeof(window[argument[k]]) != "undefined") {
  81. argument[k] = window[argument[k]];
  82. } else {
  83. try {
  84. possibleFunc = eval('(' + argument[k] + ')');
  85. if (typeof(possibleFunc) != "undefined" && typeof(possibleFunc) != "xml") {
  86. argument[k] = possibleFunc;
  87. }
  88. }
  89. catch (error) {
  90. }
  91. }
  92. }
  93. if (method && method!= '' && method!= 'undefined') {
  94. switch (true) {
  95. // exception for 'ready', 'map', 'queue'
  96. case (method == 'ready' || method == 'map' || method == 'queue'):
  97. selector = selector[method](argument[0]);
  98. break;
  99. // exception for 'bind' and 'one'
  100. case ((method == 'bind' || method == 'one') && argument.length == 3):
  101. selector = selector[method](argument[0],argument[1],argument[2]);
  102. break;
  103. // exception for 'toggle' and 'hover'
  104. case ((method == 'toggle' || method == 'hover') && argument.length == 2):
  105. selector = selector[method](argument[0], argument[1]);
  106. break;
  107. // exception for 'filter'
  108. case (method == 'filter' && argument.length == 1):
  109. // try run method
  110. selector = selector[method](argument[0]);
  111. break;
  112. // exception for effects with callback
  113. case (( method == 'show' || method == 'hide'
  114. || method == 'slideDown' || method == 'slideUp' || method == 'slideToggle'
  115. || method == 'fadeIn' || method == 'fadeOut'
  116. ) && argument.length == 2):
  117. selector = selector[method](argument[0],argument[1]);
  118. break;
  119. // exception for events with callback
  120. case (( method == 'blur' || method == 'change'
  121. || method == 'click' || method == 'dblclick'
  122. || method == 'error' || method == 'focus'
  123. || method == 'keydown' || method == 'keypress' || method == 'keyup'
  124. || method == 'load' || method == 'unload'
  125. || method == 'mousedown' || method == 'mousemove' || method == 'mouseout'
  126. || method == 'mouseover' || method == 'mouseup'
  127. || method == 'resize' || method == 'scroll'
  128. || method == 'select' || method == 'submit'
  129. ) && argument.length == 1):
  130. selector = selector[method](argument[0]);
  131. break;
  132. // exception for 'fadeTo' with callback
  133. case (method == 'fadeTo' && argument.length == 3):
  134. selector = selector[method](argument[0],argument[1],argument[2]);
  135. break;
  136. // exception for 'animate' with callback
  137. case (method == 'animate' && argument.length == 4):
  138. selector = selector[method](argument[0],argument[1],argument[2],argument[3]);
  139. break;
  140. // universal
  141. case (argument.length == 0):
  142. selector = selector[method]();
  143. break;
  144. case (argument.length == 1):
  145. selector = selector[method](argument[0]);
  146. break;
  147. case (argument.length == 2):
  148. selector = selector[method](argument[0],argument[1]);
  149. break;
  150. case (argument.length == 3):
  151. selector = selector[method](argument[0],argument[1],argument[2]);
  152. break;
  153. case (argument.length == 4):
  154. selector = selector[method](argument[0],argument[1],argument[2],argument[3]);
  155. break;
  156. case (argument.length == 5):
  157. selector = selector[method](argument[0],argument[1],argument[2],argument[3],argument[4]);
  158. break;
  159. case (argument.length == 6):
  160. selector = selector[method](argument[0],argument[1],argument[2],argument[3],argument[4],argument[5]);
  161. break;
  162. default:
  163. selector = selector[method](argument);
  164. break;
  165. }
  166. }
  167. } catch (error) {
  168. // if is error
  169. debug.warn('onAction: $("'+ response['q'][i]['s'] +'").'+ method +'("'+ argument +'")\n'
  170. +' in file: ' + error.fileName + '\n'
  171. +' on line: ' + error.lineNumber +'\n'
  172. +' error: ' + error.message);
  173. }
  174. }
  175. }
  176. // predefined actions named as
  177. // Methods of ObjResponse in PHP side
  178. $.each(response['a'], function (func, params) {
  179. for (var i=0;i<params.length; i++) {
  180. try {
  181. php[func](params[i]);
  182. } catch (error) {
  183. // if is error
  184. debug.warn('onAction: ' + func + '('+ params[i] +')\n'
  185. +' in file: ' + error.fileName + '\n'
  186. +' on line: ' + error.lineNumber +'\n'
  187. +' error: ' + error.message);
  188. }
  189. }
  190. });
  191. },
  192. /**
  193. * error
  194. *
  195. * @param object xmlEr
  196. * @param object typeEr
  197. * @param object except
  198. */
  199. error:function (xmlEr, typeEr, except) {
  200. var exObj = except ? except : false;
  201. $('#php-error').remove();
  202. var printCss =
  203. "<style type='text/css'>" +
  204. "#php-error{ width:640px; position:absolute; top:4px; right:4px; border:1px solid #f00; }"+
  205. "#php-error .php-title{ width:636px; height:26px; position:relative; line-height:26px; background-color:#f66; color:#fff; font-weight:bold; font-size:12px;padding-left:4px; }"+
  206. "#php-error .php-more { width:20px; height:20px; position:absolute; top:2px; right:24px; line-height:20px; text-align:center; cursor:pointer; border:1px solid #f00; background-color:#fee; color:#333; }"+
  207. "#php-error .php-close{ width:20px; height:20px; position:absolute; top:2px; right:2px; line-height:20px; text-align:center; cursor:pointer; border:1px solid #f00; background-color:#fee; color:#333; }"+
  208. "#php-error .php-desc { width:636px; position:relative; background-color:#fee;padding-left:4px;}"+
  209. "#php-error .php-content{ display:none;}"+
  210. "#php-error textarea{ width:634px;height:400px;overflow:auto;padding:2px;}"+
  211. "</style>";
  212. // error report for popup window coocking
  213. var printStr =
  214. "<div id='php-error'>"+
  215. "<div class='php-title'>Error in AJAX request"+
  216. "<div class='php-more'>&raquo;</div>"+
  217. "<div class='php-close'>X</div>"+
  218. "</div>"+
  219. "<div class='php-desc'>";
  220. printStr += "<b>XMLHttpRequest exchange</b>: ";
  221. // XMLHttpRequest.readyState status
  222. switch (xmlEr.readyState) {
  223. case 0:
  224. readyStDesc = "not initialize";
  225. break;
  226. case 1:
  227. readyStDesc = "open";
  228. break;
  229. case 2:
  230. readyStDesc = "data transfer";
  231. break;
  232. case 3:
  233. readyStDesc = "loading";
  234. break;
  235. case 4:
  236. readyStDesc = "finish";
  237. break;
  238. default:
  239. return "unknown state";
  240. }
  241. printStr += readyStDesc+" ("+xmlEr.readyState+")";
  242. printStr += "<br/>\n";
  243. if (exObj!=false) {
  244. printStr += "exception was catch: "+except.toString();
  245. printStr += "<br/>\n";
  246. }
  247. // add http status description
  248. printStr += "<b>HTTP status</b>: "+xmlEr.status +" - "+xmlEr.statusText;
  249. printStr += "<br/>\n";
  250. // add response text
  251. printStr += "<b>Response text</b> (<small><a href='#' class='php-more2'>show more information &raquo;</a></small>):";
  252. printStr += "</div>\n";
  253. printStr += "<div class='php-content'><textarea>"+ xmlEr.responseText+"</textarea></div>";
  254. printStr += "</div>" ;
  255. $(document.body).append(printCss);
  256. $(document.body).append(printStr);
  257. $('#php-error .php-more').hover(
  258. function(){
  259. $(this).css('background-color','#fff')
  260. },
  261. function(){
  262. $(this).css('background-color','#fee')
  263. });
  264. $('#php-error .php-more').click(function(){
  265. $('#php-error .php-content').slideToggle();
  266. });
  267. $('#php-error .php-more2').click(function(){
  268. $('#php-error .php-content').slideToggle();
  269. return false;
  270. });
  271. $('#php-error .php-close').click(function(){
  272. $('#php-error').fadeOut('fast',function(){$('#php-error').remove()})
  273. });
  274. $('#php-error .php-close').hover(
  275. function(){
  276. $(this).css('background-color','#fff')
  277. },
  278. function(){
  279. $(this).css('background-color','#fee')
  280. });
  281. },
  282. /**
  283. * complete
  284. *
  285. * @param object XMLHttpRequest
  286. * @param String textStatus
  287. */
  288. complete:function(XMLHttpRequest, textStatus) {
  289. return true;
  290. },
  291. /* Static actions */
  292. /**
  293. * addMessage
  294. * system messages callback handler
  295. * @param object data
  296. */
  297. addMessage:function(data) {
  298. // call registered or default func
  299. var message = data.msg || "";
  300. var callBackFunc = data.callback || "defaultCallBack";
  301. var callBackParams = data.params || {};
  302. php.messages[callBackFunc](message, callBackParams);
  303. },
  304. /**
  305. * addError
  306. * system errors callback handler
  307. * @param object data
  308. */
  309. addError:function(data) {
  310. // call registered or default func
  311. var message = data.msg || "";
  312. var callBackFunc = data.callback || "defaultCallBack";
  313. var callBackParams = data.params || {};
  314. php.errors[callBackFunc](message, callBackParams);
  315. },
  316. /**
  317. * addData
  318. *
  319. * @param object data
  320. */
  321. addData:function(data) {
  322. // call registered or default func
  323. var callBackFunc = data.callback || "defaultCallBack";
  324. php.data[callBackFunc](data.k, data.v);
  325. },
  326. /**
  327. * evalScript
  328. * @param object data
  329. */
  330. evalScript:function(data) {
  331. // why foo?
  332. var func = data.foo || '';
  333. eval(func);
  334. },
  335. /* Default realization of callback functions */
  336. data : {
  337. defaultCallBack : function (key, value){
  338. alert("Server response: " + key + " = " + value);
  339. }
  340. },
  341. messages : {
  342. defaultCallBack : function (msg, params){
  343. alert("Server response message: " + msg);
  344. }
  345. },
  346. errors : {
  347. defaultCallBack : function (msg, params){
  348. alert("Server response error: " + msg);
  349. }
  350. }
  351. };
  352. // end of php actions
  353. })(jQuery);