You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
4.9 KiB

  1. /**
  2. * These are the default callback functions that users may (or may not) use.
  3. *
  4. * Note that all of them take only one parameter. Don't be fooled by this, the
  5. * expected parameter is actually an array containing the real parameters. These
  6. * are checked before use.
  7. *
  8. * Look at the comments for help.
  9. */
  10. // movim_append(div, text)
  11. function movim_append(params)
  12. {
  13. if(params.length < 2) {
  14. return;
  15. }
  16. var wrapper= document.createElement('div');
  17. wrapper.innerHTML = params[1];
  18. var nodes = wrapper.childNodes;
  19. target = document.getElementById(params[0]);
  20. if(target) {
  21. for(i = 0; i < nodes.length; i++) {
  22. target.appendChild(nodes[i]);
  23. }
  24. }
  25. }
  26. // movim_prepend(div, text)
  27. function movim_prepend(params)
  28. {
  29. if(params.length < 2) {
  30. return;
  31. }
  32. var wrapper= document.createElement('div');
  33. wrapper.innerHTML = params[1];
  34. var nodes = wrapper.childNodes;
  35. target = document.getElementById(params[0]);
  36. if(target) {
  37. for(i = 0; i < nodes.length; i++) {
  38. target.insertBefore(nodes[i],target.childNodes[0]);
  39. }
  40. }
  41. }
  42. // movim_fill(div, text)
  43. function movim_fill(params)
  44. {
  45. if(params.length < 2) {
  46. return;
  47. }
  48. target = document.getElementById(params[0]);
  49. if(target) {
  50. target.innerHTML = params[1];
  51. }
  52. }
  53. // movim_delete(div)
  54. function movim_delete(params)
  55. {
  56. target = document.getElementById(params[0]);
  57. target.parentNode.removeChild(target);
  58. }
  59. // movim_drop()
  60. function movim_drop(params)
  61. {
  62. // log('movim_drop called.');
  63. }
  64. var movim_xmlhttp;
  65. /***********************************************************************
  66. * MOVIM RPC class.
  67. *
  68. * Implements an abstraction to access MOVIM's RPC system. This includes
  69. * facilities to simply call functions.
  70. *
  71. * This also includes functions to make arrays etc.
  72. */
  73. function MovimRPC()
  74. {
  75. /* Methods */
  76. /**
  77. * Generates a new XMLHttpRequest object in a portable fashion.
  78. */
  79. this.make_xmlhttp = function()
  80. {
  81. if (window.XMLHttpRequest) {// code for real browsers
  82. return new XMLHttpRequest();
  83. } else {// code for IE6, IE5
  84. return new ActiveXObject("Microsoft.XMLHTTP");
  85. }
  86. };
  87. /**
  88. * Sends data to the movim server through ajax.
  89. *
  90. * The provided mode determines what will become of the returned data. It
  91. * can either be processed by a callback function provided as modeopt or
  92. * it can append, prepend or fill the contents of the element which ID is
  93. * modeopt.
  94. */
  95. this.commit = function()
  96. {
  97. movim_xmlhttp = this.make_xmlhttp();
  98. movim_xmlhttp.open('POST', 'jajax.php', true);
  99. //var handler = this.handle_rpc;
  100. var handler = this.handle_rpc_json;
  101. movim_xmlhttp.onreadystatechange = function()
  102. {
  103. handler(movim_xmlhttp.response);
  104. if(movim_xmlhttp.readyState == 4 && movim_xmlhttp.status == 500) {
  105. movim_disconnect('&err=internal');
  106. }
  107. };
  108. movim_xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
  109. //var data = this.generate_xml();
  110. var json = this.generate_json();
  111. movim_xmlhttp.send(json);
  112. };
  113. /**
  114. * What widget do we call?
  115. */
  116. this.set_widget = function(widgetname)
  117. {
  118. this.widget = widgetname;
  119. };
  120. /**
  121. * What function to call in Movim?
  122. */
  123. this.set_func = function(funcname)
  124. {
  125. this.func = funcname;
  126. };
  127. /**
  128. * Adds a parameter to the called function.
  129. */
  130. this.add_param = function(param)
  131. {
  132. this.params.push(param);
  133. };
  134. /**
  135. * Sets all movim call parameters at once.
  136. */
  137. this.set_call = function(widget, func, params)
  138. {
  139. this.set_widget(widget);
  140. this.set_func(func);
  141. this.params = params;
  142. };
  143. /**
  144. * Handles returns (xmlrpc)
  145. */
  146. this.handle_rpc_json = function(json)
  147. {
  148. if(json != null) {
  149. var funcalls = eval(json);
  150. if(funcalls != null) {
  151. for(h = 0; h < funcalls.length; h++) {
  152. //console.log(funcalls);
  153. var funcall = funcalls[h];
  154. try {
  155. window[funcall.func](funcall.params);
  156. }
  157. catch(err) {
  158. log("Error caught: " + err.toString());
  159. }
  160. }
  161. }
  162. }
  163. }
  164. /**
  165. * Generates the JSON document corresponding to the provided parameters.
  166. */
  167. this.generate_json = function()
  168. {
  169. return JSON.stringify(this);
  170. };
  171. /* Properties */
  172. this.widget = '';
  173. this.func = '';
  174. this.params = [];
  175. }
  176. /**
  177. * Putting it all together.
  178. */
  179. function movim_ajaxSend(widget, func, parameters)
  180. {
  181. rpc.set_call(widget, func, parameters);
  182. rpc.commit();
  183. }
  184. var rpc = new MovimRPC(); // Initialising global rpc handler.