http_utils.js 6.1 KB

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