http_utils.js 6.1 KB

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