init.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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: shred initClient()
  6. /* initialize client
  7. PART 1: setup connection to backend
  8. 0. extract GET parameters, if any, from URL
  9. 1. setup socket and its event handlers (to handle changelogs etc.), then
  10. connect socket
  11. 2. on socket connection,
  12. a) if no GET params or if modelshare params,
  13. i. spawn new csworker
  14. ii. on spawn, subscribe to new csworker changes
  15. a) otherwise (screenshare), subscribe to specified csworker changes
  16. 3. on reception of socket messages,
  17. a) if message has a status code (i.e., response to csworker subscription)
  18. i. load mainmenu button model
  19. * CASE 1: no GET params *
  20. ii. spawn and subscribe to new asworker
  21. iii. get user preferences and autoload toolbars and model, if any
  22. iv. enable collaboration links
  23. v. launch autosave daemon
  24. * CASE 2: modelshare *
  25. ii. clone state of given csworker and subscribe to given asworker
  26. iii. update client state based on our csworker's updated state
  27. iv. enable collaboration links
  28. v. launch autosave daemon
  29. * CASE 3: screenshare *
  30. ii. retrieve state of given csworker
  31. iii. update client state based on our csworker's updated state
  32. iv. enable collaboration links
  33. v. launch autosave daemon
  34. a) otherwise, handle changelog
  35. PART 2: setup frontend
  36. 1. setup various GUI details and behaviour statecharts
  37. 2. setup exit prompt */
  38. function __initClient()
  39. {
  40. /** PART 1 **/
  41. var params = {};
  42. window.location.search.substring(1).split('&').forEach(
  43. function(arg)
  44. {
  45. var _arg = arg.split('=');
  46. params[_arg[0]] = _arg[1];
  47. });
  48. var socket = io.connect(
  49. window.location.hostname,{'port':8124,'reconnect':false});
  50. socket.on('message',
  51. function(msg)
  52. {
  53. console.debug(' >> '+utils.jsons(msg));
  54. if( msg['statusCode'] != undefined )
  55. {
  56. if( msg['statusCode'] == 201 )
  57. {
  58. _loadToolbar(__MAINMENU_PATH);
  59. if( window.location.search == '' )
  60. HttpUtils.httpReq(
  61. 'PUT',
  62. '/aswSubscription?wid='+__wid,
  63. undefined,
  64. function(statusCode,resp)
  65. {
  66. console.debug(statusCode);
  67. console.debug(resp);
  68. __aswid = utils.jsonp(resp)['data'];
  69. _getUserPreferences(
  70. function(prefs)
  71. {
  72. console.debug("Get User Preferences in Init.js (96)");
  73. console.debug(prefs);
  74. __prefs = prefs;
  75. prefs['autoloaded-toolbars']['value'].
  76. forEach(_loadToolbar);
  77. if( prefs['autoloaded-model']['value'] != '' )
  78. _loadModel(prefs['autoloaded-model']['value']);
  79. Collaboration.enableCollaborationLinks();
  80. __launchAutosave();
  81. });
  82. });
  83. else if( 'aswid' in params && 'cswid' in params )
  84. {
  85. __user = params['host'];
  86. HttpUtils.httpReq(
  87. 'PUT',
  88. '/aswSubscription?wid='+__wid,
  89. {'aswid':params['aswid'],
  90. 'cswid':params['cswid']},
  91. function(statusCode,resp)
  92. {
  93. resp = utils.jsonp(resp);
  94. __handleState(resp['data'],resp['sequence#']);
  95. Collaboration.enableCollaborationLinks();
  96. __launchAutosave();
  97. });
  98. }
  99. else
  100. {
  101. __wid = params['cswid'];
  102. __user = params['host'];
  103. HttpUtils.httpReq(
  104. 'GET',
  105. '/current.state?wid='+params['cswid'],
  106. undefined,
  107. function(statusCode,resp)
  108. {
  109. resp = utils.jsonp(resp);
  110. __handleState(resp['data'],resp['sequence#']);
  111. Collaboration.enableCollaborationLinks();
  112. __launchAutosave();
  113. });
  114. }
  115. }
  116. else
  117. WindowManagement.openDialog(__FATAL_ERROR, 'failed to connect to back-end');
  118. }
  119. else
  120. __handleChangelog(
  121. msg['data']['changelog'],
  122. msg['data']['sequence#'],
  123. msg['data']['hitchhiker']);
  124. });
  125. socket.on('disconnect',
  126. function()
  127. {
  128. WindowManagement.openDialog(__FATAL_ERROR, 'lost connection to back-end');
  129. });
  130. socket.on('connect',
  131. function()
  132. {
  133. if( window.location.search == '' ||
  134. ('aswid' in params && 'cswid' in params) )
  135. HttpUtils.httpReq(
  136. 'POST',
  137. '/csworker',
  138. undefined,
  139. function(statusCode,resp)
  140. {
  141. __wid = resp;
  142. socket.emit(
  143. 'message',
  144. {'method':'POST','url':'/changeListener?wid='+__wid});
  145. });
  146. else if( 'cswid' in params )
  147. socket.emit(
  148. 'message',
  149. {'method':'POST','url':'/changeListener?wid='+params['cswid']});
  150. else
  151. WindowManagement.openDialog(__FATAL_ERROR, 'invalid URL parameters '+
  152. utils.jsons(params));
  153. });
  154. /** PART 2 **/
  155. $('#a_logout').title = 'logout '+__user;
  156. __canvas = Raphael(GUIUtils.$$('div_canvas'),__CANVAS_SIZE,__CANVAS_SIZE);
  157. __canvas.canvas.setAttribute('vector-effect','non-scaling-stroke');
  158. __canvas.canvas.setAttribute('xmlns:xlink','http://www.w3.org/1999/xlink');
  159. BehaviorManager.setActiveBehaviourStatechart(__SC_DIALOG,true);
  160. BehaviorManager.setActiveBehaviourStatechart(__SC_CANVAS,true);
  161. WindowManagement.setWindowTitle();
  162. window.onbeforeunload =
  163. /* prompt on non-logout exit */
  164. function(ev)
  165. {
  166. if( __user == undefined )
  167. return;
  168. else if( __prefs['confirm-exit']['value'] && ! __isSaved() )
  169. return __EXITWARNING;
  170. };
  171. }