http_utils.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*******************************************************************************
  2. AToMPM - A Tool for Multi-Paradigm Modelling
  3. Copyright (c) 2011 Raphael Mannadiar (raphael.mannadiar@mail.mcgill.ca)
  4. Modified by Conner Hansen (chansen@crimson.ua.edu)
  5. This file is part of AToMPM.
  6. AToMPM is free software: you can redistribute it and/or modify it under the
  7. terms of the GNU Lesser General Public License as published by the Free Software
  8. Foundation, either version 3 of the License, or (at your option) any later
  9. version.
  10. AToMPM is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with AToMPM. If not, see <http://www.gnu.org/licenses/>.
  15. *******************************************************************************/
  16. //Todo: replace this with JQuery
  17. //This remains outside for easier replacement later down the line
  18. /**
  19. * Basic element getter
  20. */
  21. //function $(id)
  22. //{
  23. // return document.getElementById(id);
  24. //}
  25. /**
  26. * The HTTP Utils object is a utility function that allows for
  27. * AToMPM to send HTTP requests
  28. */
  29. HttpUtils = function(){
  30. /**
  31. * Generates an HTTP request
  32. */
  33. this.httpReq = function(method,url,params,onresponse,sync)
  34. {
  35. console.debug('??? '+method+' '+url, params);
  36. var req = new XMLHttpRequest();
  37. var onreadystatechange = function(ev) {
  38. if( req.readyState == 4 )
  39. {
  40. console.debug(method+' '+url+' >> '+req.status);
  41. if( req.status == 0 )
  42. WindowManagement.openDialog(__FATAL_ERROR,'lost connection to back-end');
  43. else if( onresponse )
  44. onresponse(req.status,req.responseText);
  45. else if( ! utils.isHttpSuccessCode(req.status) )
  46. WindowManagement.openDialog(_ERROR,req.responseText);
  47. }
  48. };
  49. if( method == 'GET' || method == 'DELETE' )
  50. {
  51. // console.log(method);
  52. // console.log(url);
  53. // console.log(params);
  54. // console.log(onresponse);
  55. // console.log(sync);
  56. req.open(method, url+(params ? params : ''), !sync);
  57. req.onreadystatechange = onreadystatechange;
  58. req.send(null);
  59. }
  60. else if( method == 'POST' || method == 'PUT' )
  61. {
  62. // console.debug(method);
  63. // console.debug(url);
  64. // console.debug(params);
  65. // console.debug(onresponse);
  66. // console.debug(sync);
  67. // console.debug(utils.jsons(params));
  68. req.open(method, url, !sync);
  69. req.onreadystatechange = onreadystatechange;
  70. req.send(utils.jsons(params));
  71. }
  72. };
  73. /**
  74. * Construct a complete and valid backend URL
  75. */
  76. this.url = function(uri,options,wid)
  77. {
  78. // console.log("Calling url");
  79. wid = (wid == undefined ? __wid : wid);
  80. return (options & __FORCE_GET ? '/GET' : '')+
  81. (options & __NO_USERNAME ? '' : '/'+__user)+
  82. (uri.charAt(0) == '/' ? uri : '/'+uri)+
  83. (options & __NO_WID ? '' : '?wid='+wid);
  84. };
  85. /**
  86. * Returns a file browser icon given the specified filename
  87. */
  88. this.getFileIcon = function(fname)
  89. {
  90. // console.log("Calling getFileIcon " + fname);
  91. var src = '';
  92. if( fname.match(/\/$/) )
  93. src = 'client/media/fileb_folder.png';
  94. else if( fname.match(/\.pattern\.metamodel$/) )
  95. src = 'client/media/fileb_patternmm.png';
  96. else if( fname.match(/\..*Icons\.metamodel$/) )
  97. src = 'client/media/fileb_csmm.png';
  98. else if( fname.match(/\.metamodel$/) )
  99. src = 'client/media/fileb_asmm.png';
  100. else if( fname.match(/\.model$/) )
  101. src = 'client/media/fileb_m.png';
  102. else
  103. src = 'client/media/fileb_unknown.png';
  104. var span = $('<span>'),
  105. img = $('<img>'),
  106. txt = GUIUtils.getTextSpan(
  107. fname.match(/\/$/) ? fname.substring(0,fname.length-1) : fname,
  108. 'default_style clickable');
  109. img.attr("src", src)
  110. .attr("class", 'clickable');
  111. span.attr("class", 'fileb_icon');
  112. // img.attr("class", 'clickable');
  113. txt.css("padding", '5px');
  114. span.append(img);
  115. span.append(txt);
  116. return span;
  117. };
  118. /**
  119. * Returns the new file icon
  120. */
  121. this.getNewFileIcon = function(oninput,caption)
  122. {
  123. // console.log("Calling get new file icon " + caption);
  124. var span = $('<span></span>'),
  125. img = $('<img></img>'),
  126. txt = GUIUtils.getTextSpan(
  127. caption || '&lt;new file&gt;',
  128. 'default_style clickable');
  129. img.attr("src", 'client/media/fileb_newf.png');
  130. span.addClass('fileb_icon');
  131. img.addClass('clickable');
  132. txt.css("padding", '5px');
  133. txt.css("fontStyle", 'italic');
  134. txt.attr("contentEditable", true);
  135. // JQuery does not support HTML5 oninput
  136. txt.keyup( oninput );
  137. span.append(img);
  138. span.append(txt);
  139. return span;
  140. };
  141. /**
  142. * Gets the selected entry in a Select input
  143. */
  144. this.getSelectorSelection = function(select)
  145. {
  146. var selection = [];
  147. var options = select.children("option");
  148. for( var i = 0; i < options.length; i++)
  149. if( $(options[i]).prop("selected") )
  150. selection.push( $(options[i]).html() );
  151. return selection;
  152. };
  153. /* given a path to an image, produce a data uri that 'describes' the image
  154. NOTE:: due to browser restrictions, cross-domain images need to be processed
  155. by the backend */
  156. this.imageToDataURI = function(url,callback)
  157. {
  158. if( url.match('^\/'+__user+'/') )
  159. {
  160. var canvas = $('<canvas>'),
  161. img = $('<img>');
  162. img.onload(
  163. function()
  164. {
  165. canvas.attr("width", img.attr("width") );
  166. canvas.attr("height", img.attr("height") );
  167. canvas.get().getContext('2d').drawImage(img, 0, 0);
  168. callback(canvas.get().toDataURL());
  169. });
  170. img.attr("src", url);
  171. }
  172. else
  173. httpReq(
  174. 'GET',
  175. '/datauri?target='+encodeURIComponent(url),
  176. undefined,
  177. function(statusCode,resp)
  178. {
  179. if( ! utils.isHttpSuccessCode(statusCode) )
  180. callback(__DEFAULT_IMG_DATAURI);
  181. else
  182. callback(resp);
  183. });
  184. };
  185. /**
  186. * Removes all children from a node
  187. */
  188. this.removeChildren = function(node)
  189. {
  190. node.empty();
  191. // while(node.children().length > 0 )
  192. // node.first().remove();
  193. };
  194. /**
  195. * Attempt to evaluate user code.
  196. * I don't understand why this is called "safeEval"
  197. * though, considering this will execute
  198. * anything passed in
  199. */
  200. this.safeEval = function(code)
  201. {
  202. var _context = {
  203. 'username':__user,
  204. 'wid':__wid,
  205. 'mms':utils.keys(__loadedToolbars).filter(__isIconMetamodel)};
  206. try {eval(code); return {};}
  207. catch(err)
  208. {
  209. if( err['$err'] ) return err;
  210. else return {'$uerr':err};
  211. }
  212. };
  213. return this;
  214. }();