http_utils.js 6.0 KB

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