http_utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. span.append(img);
  104. span.append(txt);
  105. return span;
  106. };
  107. /**
  108. * Returns the new file icon
  109. */
  110. this.getNewFileIcon = function(oninput,caption)
  111. {
  112. // console.log("Calling get new file icon " + caption);
  113. var span = $('<span></span>'),
  114. img = $('<img></img>'),
  115. txt = GUIUtils.getTextSpan(
  116. caption || '&lt;new file&gt;',
  117. 'default_style clickable');
  118. img.attr("src", 'client/media/fileb_newf.png');
  119. span.addClass('fileb_icon');
  120. img.addClass('clickable');
  121. txt.css("padding", '5px');
  122. txt.css("fontStyle", 'italic');
  123. txt.attr("contentEditable", true);
  124. // JQuery does not support HTML5 oninput
  125. txt.keyup( oninput );
  126. span.append(img);
  127. span.append(txt);
  128. return span;
  129. };
  130. /**
  131. * Gets the selected entry in a Select input
  132. */
  133. this.getSelectorSelection = function(select)
  134. {
  135. var selection = [];
  136. var options = select.children("option");
  137. for( var i = 0; i < options.length; i++)
  138. if( $(options[i]).prop("selected") )
  139. selection.push( $(options[i]).html() );
  140. return selection;
  141. };
  142. /* given a path to an image, produce a data uri that 'describes' the image
  143. NOTE:: due to browser restrictions, cross-domain images need to be processed
  144. by the backend */
  145. this.imageToDataURI = function(url,callback)
  146. {
  147. if( url.match('^\/'+__user+'/') )
  148. {
  149. var canvas = $('<canvas>'),
  150. img = $('<img>');
  151. img.onload(
  152. function()
  153. {
  154. canvas.attr("width", img.attr("width") );
  155. canvas.attr("height", img.attr("height") );
  156. canvas.get().getContext('2d').drawImage(img, 0, 0);
  157. callback(canvas.get().toDataURL());
  158. });
  159. img.attr("src", url);
  160. }
  161. else
  162. httpReq(
  163. 'GET',
  164. '/datauri?target='+encodeURIComponent(url),
  165. undefined,
  166. function(statusCode,resp)
  167. {
  168. if( ! utils.isHttpSuccessCode(statusCode) )
  169. callback(__DEFAULT_IMG_DATAURI);
  170. else
  171. callback(resp);
  172. });
  173. };
  174. /**
  175. * Removes all children from a node
  176. */
  177. this.removeChildren = function(node)
  178. {
  179. node.empty();
  180. // while(node.children().length > 0 )
  181. // node.first().remove();
  182. };
  183. /**
  184. * Attempt to evaluate user code.
  185. * I don't understand why this is called "safeEval"
  186. * though, considering this will execute
  187. * anything passed in
  188. */
  189. this.safeEval = function(code)
  190. {
  191. var _context = {
  192. 'username':__user,
  193. 'wid':__wid,
  194. 'mms':utils.keys(__loadedToolbars).filter(__isIconMetamodel)};
  195. try {eval(code); return {};}
  196. catch(err)
  197. {
  198. if( err['$err'] ) return err;
  199. else return {'$uerr':err};
  200. }
  201. };
  202. return this;
  203. }();