/libraries/pear/HTML/AJAX/js/serializer/haSerializer.js

https://github.com/jaws-project/jaws · JavaScript · 301 lines · 274 code · 0 blank · 27 comment · 38 complexity · 7dbff2525f281f79a2189d126bb77317 MD5 · raw file

  1. /**
  2. * HTML_AJAX_Serialize_HA - custom serialization
  3. *
  4. * This class is used with the JSON serializer and the HTML_AJAX_Action php class
  5. * to allow users to easily write data handling and dom manipulation related to
  6. * ajax actions directly from their php code
  7. *
  8. * See Main.js for Author/license details
  9. */
  10. function HTML_AJAX_Serialize_HA() { }
  11. HTML_AJAX_Serialize_HA.prototype =
  12. {
  13. /**
  14. * Takes data from JSON - which should be parseable into a nice array
  15. * reads the action to take and pipes it to the right method
  16. *
  17. * @param string payload incoming data from php
  18. * @return true on success, false on failure
  19. */
  20. unserialize: function(payload)
  21. {
  22. var actions = eval(payload);
  23. for(var i = 0; i < actions.length; i++)
  24. {
  25. var action = actions[i];
  26. switch(action.action)
  27. {
  28. case 'prepend':
  29. this._prependAttr(action.id, action.attributes);
  30. break;
  31. case 'append':
  32. this._appendAttr(action.id, action.attributes);
  33. break;
  34. case 'assign':
  35. this._assignAttr(action.id, action.attributes);
  36. break;
  37. case 'clear':
  38. this._clearAttr(action.id, action.attributes);
  39. break;
  40. case 'create':
  41. this._createNode(action.id, action.tag, action.attributes, action.type);
  42. break;
  43. case 'replace':
  44. this._replaceNode(action.id, action.tag, action.attributes);
  45. break;
  46. case 'remove':
  47. this._removeNode(action.id);
  48. break;
  49. case 'script':
  50. this._insertScript(action.data);
  51. break;
  52. case 'alert':
  53. this._insertAlert(action.data);
  54. break;
  55. }
  56. }
  57. },
  58. /* Dispatch Methods */
  59. _prependAttr: function(id, attributes)
  60. {
  61. var node = document.getElementById(id);
  62. this._setAttrs(node, attributes, 'prepend');
  63. },
  64. _appendAttr: function(id, attributes)
  65. {
  66. var node = document.getElementById(id);
  67. this._setAttrs(node, attributes, 'append');
  68. },
  69. _assignAttr: function(id, attributes)
  70. {
  71. var node = document.getElementById(id);
  72. this._setAttrs(node, attributes);
  73. },
  74. _clearAttr: function(id, attributes)
  75. {
  76. var node = document.getElementById(id);
  77. for(var i = 0; i < attributes.length; i++)
  78. {
  79. if(attributes[i] == 'innerHTML')
  80. {
  81. HTML_AJAX_Util.setInnerHTML(node, '');
  82. }
  83. // value can't be removed
  84. else if(attributes[i] == 'value')
  85. {
  86. node.value = '';
  87. }
  88. // I'd use hasAttribute first but IE is stupid stupid stupid
  89. else
  90. {
  91. try
  92. {
  93. node.removeAttribute(attributes[i]);
  94. }
  95. catch(e)
  96. {
  97. node[i] = undefined;
  98. }
  99. }
  100. }
  101. },
  102. _createNode: function(id, tag, attributes, type)
  103. {
  104. var newnode = document.createElement(tag);
  105. this._setAttrs(newnode, attributes);
  106. switch(type)
  107. {
  108. case 'append':
  109. document.getElementById(id).appendChild(newnode);
  110. break
  111. case 'prepend':
  112. var parent = document.getElementById(id);
  113. var sibling = parent.firstChild;
  114. parent.insertBefore(newnode, sibling);
  115. break;
  116. case 'insertBefore':
  117. var sibling = document.getElementById(id);
  118. var parent = sibling.parentNode;
  119. parent.insertBefore(newnode, sibling);
  120. break;
  121. //this one is tricky, if it's the last one we use append child...ewww
  122. case 'insertAfter':
  123. var sibling = document.getElementById(id);
  124. var parent = sibling.parentNode;
  125. var next = sibling.nextSibling;
  126. if(next == null)
  127. {
  128. parent.appendChild(newnode);
  129. }
  130. else
  131. {
  132. parent.insertBefore(newnode, next);
  133. }
  134. break;
  135. }
  136. },
  137. _replaceNode: function(id, tag, attributes)
  138. {
  139. var node = document.getElementById(id);
  140. var parent = node.parentNode;
  141. var newnode = document.createElement(tag);
  142. this._setAttrs(newnode, attributes);
  143. parent.replaceChild(newnode, node);
  144. },
  145. _removeNode: function(id)
  146. {
  147. var node = document.getElementById(id);
  148. if(node)
  149. {
  150. var parent = node.parentNode;
  151. parent.removeChild(node);
  152. }
  153. },
  154. _insertScript: function(data)
  155. {
  156. eval(data);
  157. },
  158. _insertAlert: function(data)
  159. {
  160. alert(data);
  161. },
  162. /* Helper Methods */
  163. // should we move this to HTML_AJAX_Util???, just does the - case which we need for style
  164. _camelize: function(instr)
  165. {
  166. var p = instr.split('-');
  167. var out = p[0];
  168. for(var i = 1; i < p.length; i++) {
  169. out += p[i].charAt(0).toUpperCase()+p[i].substring(1);
  170. }
  171. return out;
  172. },
  173. _setAttrs: function(node, attributes, type)
  174. {
  175. switch(type)
  176. {
  177. case 'prepend':
  178. for (var i in attributes)
  179. {
  180. // innerHTML is extremely flakey - use util method for it
  181. if(i == 'innerHTML')
  182. {
  183. HTML_AJAX_Util.setInnerHTML(node, attributes[i], 'append');
  184. }
  185. //IE doesn't support setAttribute on style so we need to break it out and set each property individually
  186. else if(i == 'style')
  187. {
  188. var styles = [];
  189. if(attributes[i].indexOf(';'))
  190. {
  191. styles = attributes[i].split(';');
  192. }
  193. else
  194. {
  195. styles.push(attributes[i]);
  196. }
  197. for(var i = 0; i < styles.length; i++)
  198. {
  199. var r = styles[i].match(/^\s*(.+)\s*:\s*(.+)\s*$/);
  200. if(r)
  201. {
  202. node.style[this._camelize(r[1])] = r[2] + node.style[this._camelize(r[1])];
  203. }
  204. }
  205. }
  206. else
  207. {
  208. try
  209. {
  210. node[i] = attributes[i] + node[i];
  211. }
  212. catch(e){}
  213. node.setAttribute(i, attributes[i] + node[i]);
  214. }
  215. }
  216. break;
  217. case 'append':
  218. {
  219. for (var i in attributes)
  220. {
  221. // innerHTML is extremely flakey - use util method for it
  222. if(i == 'innerHTML')
  223. {
  224. HTML_AJAX_Util.setInnerHTML(node, attributes[i], 'append');
  225. }
  226. //IE doesn't support setAttribute on style so we need to break it out and set each property individually
  227. else if(i == 'style')
  228. {
  229. var styles = [];
  230. if(attributes[i].indexOf(';'))
  231. {
  232. styles = attributes[i].split(';');
  233. }
  234. else
  235. {
  236. styles.push(attributes[i]);
  237. }
  238. for(var i = 0; i < styles.length; i++)
  239. {
  240. var r = styles[i].match(/^\s*(.+)\s*:\s*(.+)\s*$/);
  241. if(r)
  242. {
  243. node.style[this._camelize(r[1])] = node.style[this._camelize(r[1])] + r[2];
  244. }
  245. }
  246. }
  247. else
  248. {
  249. try
  250. {
  251. node[i] = node[i] + attributes[i];
  252. }
  253. catch(e){}
  254. node.setAttribute(i, node[i] + attributes[i]);
  255. }
  256. }
  257. break;
  258. }
  259. default:
  260. {
  261. for (var i in attributes)
  262. {
  263. //innerHTML hack bailout
  264. if(i == 'innerHTML')
  265. {
  266. HTML_AJAX_Util.setInnerHTML(node, attributes[i]);
  267. }
  268. else if(i == 'style')
  269. {
  270. var styles = [];
  271. if(attributes[i].indexOf(';'))
  272. {
  273. styles = attributes[i].split(';');
  274. }
  275. else
  276. {
  277. styles.push(attributes[i]);
  278. }
  279. for(var i = 0; i < styles.length; i++)
  280. {
  281. var r = styles[i].match(/^\s*(.+)\s*:\s*(.+)\s*$/);
  282. if(r)
  283. {
  284. node.style[this._camelize(r[1])] = r[2];
  285. }
  286. }
  287. }
  288. else
  289. {
  290. try
  291. {
  292. node[i] = attributes[i];
  293. }
  294. catch(e){}
  295. node.setAttribute(i, attributes[i]);
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }