http_utils.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. let paramValue = (typeof params == "string")?
  64. params : utils.jsons(params);
  65. req.send(paramValue);
  66. }
  67. };
  68. /**
  69. * Construct a complete and valid backend URL
  70. */
  71. this.url = function(uri,options,wid)
  72. {
  73. // console.log("Calling url");
  74. wid = (wid == undefined ? __wid : wid);
  75. return (options & __FORCE_GET ? '/GET' : '')+
  76. (options & __NO_USERNAME ? '' : '/'+__user)+
  77. (uri.charAt(0) == '/' ? uri : '/'+uri)+
  78. (options & __NO_WID ? '' : '?wid='+wid);
  79. };
  80. /**
  81. * Returns a file browser icon given the specified filename
  82. */
  83. this.getFileIcon = function(fname)
  84. {
  85. // console.log("Calling getFileIcon " + fname);
  86. var src = '';
  87. if( fname.match(/\/$/) )
  88. src = 'client/media/fileb_folder.png';
  89. else if( fname.match(/\.pattern\.metamodel$/) )
  90. src = 'client/media/fileb_patternmm.png';
  91. else if( fname.match(/\..*Icons\.metamodel$/) )
  92. src = 'client/media/fileb_csmm.png';
  93. else if( fname.match(/\.metamodel$/) )
  94. src = 'client/media/fileb_asmm.png';
  95. else if( fname.match(/\.model$/) )
  96. src = 'client/media/fileb_m.png';
  97. else
  98. src = 'client/media/fileb_unknown.png';
  99. var span = $('<span>'),
  100. img = $('<img>'),
  101. txt = GUIUtils.getTextSpan(
  102. fname.match(/\/$/) ? fname.substring(0,fname.length-1) : fname,
  103. 'default_style clickable');
  104. img.attr("src", src)
  105. .attr("class", 'clickable');
  106. span.attr("class", 'fileb_icon');
  107. // img.attr("class", 'clickable');
  108. txt.css("padding", '5px');
  109. txt.attr('id', fname.replace("/", ""));
  110. span.append(img);
  111. span.append(txt);
  112. return span;
  113. };
  114. /**
  115. * Returns the new file icon
  116. */
  117. this.getNewFileIcon = function(oninput,caption)
  118. {
  119. // console.log("Calling get new file icon " + caption);
  120. var span = $('<span></span>'),
  121. img = $('<img></img>'),
  122. txt = GUIUtils.getTextSpan(
  123. caption || '&lt;new file&gt;',
  124. 'default_style clickable');
  125. img.attr("src", 'client/media/fileb_newf.png');
  126. span.addClass('fileb_icon');
  127. img.addClass('clickable');
  128. txt.css("padding", '5px');
  129. txt.css("fontStyle", 'italic');
  130. txt.attr("contentEditable", true);
  131. // JQuery does not support HTML5 oninput
  132. txt.keyup( oninput );
  133. txt.attr('id', 'new_file');
  134. span.append(img);
  135. span.append(txt);
  136. return span;
  137. };
  138. /**
  139. * Gets the selected entry in a Select input
  140. */
  141. this.getSelectorSelection = function(select)
  142. {
  143. var selection = [];
  144. var options = select.children("option");
  145. for( var i = 0; i < options.length; i++)
  146. if( $(options[i]).prop("selected") )
  147. selection.push( $(options[i]).html() );
  148. return selection;
  149. };
  150. /* given a path to an image, produce a data uri that 'describes' the image
  151. NOTE:: due to browser restrictions, cross-domain images need to be processed
  152. by the backend */
  153. this.imageToDataURI = function(url,callback)
  154. {
  155. if( url.match('^\/'+__user+'/') )
  156. {
  157. var canvas = $('<canvas>'),
  158. img = $('<img>');
  159. img.onload(
  160. function()
  161. {
  162. canvas.attr("width", img.attr("width") );
  163. canvas.attr("height", img.attr("height") );
  164. canvas.get().getContext('2d').drawImage(img, 0, 0);
  165. callback(canvas.get().toDataURL());
  166. });
  167. img.attr("src", url);
  168. }
  169. else
  170. httpReq(
  171. 'GET',
  172. '/datauri?target='+encodeURIComponent(url),
  173. undefined,
  174. function(statusCode,resp)
  175. {
  176. if( ! utils.isHttpSuccessCode(statusCode) )
  177. callback(__DEFAULT_IMG_DATAURI);
  178. else
  179. callback(resp);
  180. });
  181. };
  182. /**
  183. * Removes all children from a node
  184. */
  185. this.removeChildren = function(node)
  186. {
  187. node.empty();
  188. // while(node.children().length > 0 )
  189. // node.first().remove();
  190. };
  191. /**
  192. * Attempt to evaluate user code.
  193. * I don't understand why this is called "safeEval"
  194. * though, considering this will execute
  195. * anything passed in
  196. */
  197. this.safeEval = function(code)
  198. {
  199. var _context = {
  200. 'username':__user,
  201. 'wid':__wid,
  202. 'mms':utils.keys(__loadedToolbars).filter(__isIconMetamodel)};
  203. try {eval(code); return {};}
  204. catch(err)
  205. {
  206. if( err['$err'] ) return err;
  207. else return {'$uerr':err};
  208. }
  209. };
  210. return this;
  211. }();