http_utils.js 5.7 KB

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