Guest User

Untitled

a guest
Jul 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.64 KB | None | 0 0
  1. /**
  2. * CaseWindow
  3. */
  4.  
  5. var CaseWindow,
  6. CASE_WINDOW = 'caseWindow',
  7. ATTRS = {},
  8. VIEWS = {},
  9.  
  10. USER_ID = 'userID',
  11. CASE_ID = 'caseID',
  12. CASE_PATH = 'casePath',
  13. CASE_INFO_PATH = 'caseInfoPath',
  14.  
  15. CASE_RESOURCE = 'caseResource',
  16. CASE_INFO_RESOURCE = 'caseInfoResource',
  17.  
  18. TITLE = 'title',
  19. STEP = 'step',
  20. INFO_NODES = 'infoNodes',
  21. STATE = 'state',
  22. DIAGNOSES = 'diagnoses',
  23. CONTENT = 'content',
  24.  
  25. HOST = 'host',
  26. VIEW = 'View',
  27.  
  28. // states
  29. INTRO = 'intro',
  30. STEPS = 'steps',
  31. DIAGNOSIS = 'diagnosis',
  32. DONE = 'done',
  33.  
  34. // views
  35. INTRO_VIEW = INTRO+VIEW,
  36. STEPS_VIEW = STEPS+VIEW,
  37. DIAGNOSIS_VIEW = DIAGNOSIS+VIEW,
  38. DONE_VIEW = DONE+VIEW,
  39.  
  40. // actions
  41. START = 'start',
  42. MORE = 'more',
  43. READY = 'ready',
  44. DIAGNOSE = 'diagnose',
  45.  
  46. CHANGE = 'Change',
  47.  
  48. E_CASE_DATA = 'caseData',
  49.  
  50. YLang = Y.Lang,
  51. isValue = YLang.isValue,
  52. isArray = YLang.isArray,
  53. isString = YLang.isString,
  54. isNumber = YLang.isNumber;
  55.  
  56. // *** Attributes *** //
  57.  
  58. ATTRS[ USER_ID ] = { validator: isString, initOnly: true };
  59. ATTRS[ CASE_ID ] = { validator: isString, initOnly: true };
  60. ATTRS[ CASE_PATH ] = { validator: isString, initOnly: true };
  61. ATTRS[ CASE_INFO_PATH ] = { validator: isString, initOnly: true };
  62.  
  63. ATTRS[ CASE_RESOURCE ] = { valueFn: '_initCaseResource', writeOnce: true };
  64. ATTRS[ CASE_INFO_RESOURCE ] = { valueFn: '_initCaseInfoResource', writeOnce: true };
  65.  
  66. ATTRS[ TITLE ] = { validator: isString, writeOnce: true };
  67. ATTRS[ STEP ] = { validator: isNumber };
  68. ATTRS[ INFO_NODES ] = { writeOnce: true };
  69. ATTRS[ STATE ] = { validator: isString };
  70. ATTRS[ DIAGNOSES ] = { validator: isArray, getter: '_getDiagnoses' };
  71. ATTRS[ CONTENT ] = {};
  72.  
  73. // *** CaseWindow *** //
  74.  
  75. CaseWindow = Y.Base.create(CASE_WINDOW, Y.Base, [], {
  76.  
  77. // *** Prototype *** //
  78.  
  79. // *** Lifecycle Methods *** //
  80.  
  81. initializer : function () {
  82.  
  83. // these attributes much have values
  84. this._requireAttrs([ USER_ID, CASE_ID, CASE_PATH, CASE_INFO_PATH ]);
  85.  
  86. // publish events
  87. this.publish(E_CASE_DATA, { defaultFn: this._defCaseDataFn });
  88.  
  89. // attribute change event handlers
  90. this.after(STATE+CHANGE, this._afterStateChange);
  91. this.after(CONTENT+CHANGE, this._afterContentChange);
  92.  
  93. // get initial data
  94. this.get(CASE_RESOURCE).GET();
  95. },
  96.  
  97. // *** Public Methods *** //
  98.  
  99. showView : function (newView, prevView) {
  100.  
  101. if (prevView) {
  102. this.unplug(prevView);
  103. }
  104. this.plug(newView);
  105. },
  106.  
  107. start : function () {
  108.  
  109. this.get(CASE_RESOURCE).POST({ entity: { action: START } });
  110. },
  111.  
  112. more : function () {
  113.  
  114. this.get(CASE_RESOURCE).POST({ entity: {
  115. action : MORE,
  116. step : this.get(STEP),
  117. diagnoses : this.get(DIAGNOSES)
  118. }});
  119. },
  120.  
  121. ready : function () {
  122.  
  123. this.get(CASE_RESOURCE).POST({ entity: {
  124. action : READY,
  125. step : this.get(STEP),
  126. diagnoses : this.get(DIAGNOSES)
  127. }});
  128. },
  129.  
  130. diagnose : function (diagnosis, answers) {
  131.  
  132. this.get(CASE_RESOURCE).POST({ entity: {
  133. action : DIAGNOSE,
  134. diagnosis : diagnosis,
  135. answers : answers
  136. }});
  137. },
  138.  
  139. // *** Private Methods *** //
  140.  
  141. _requireAttrs : function (attrs) {
  142.  
  143. Y.each(attrs, Y.bind(function(attr){
  144. if ( ! isValue(this.get(attr))) {
  145. Y.error('A CaseWindow needs to be configured with: ' + attr);
  146. }
  147. }, this));
  148. },
  149.  
  150. _initCaseResource : function () {
  151.  
  152. var uri = this.get(CASE_PATH) + '{userID}/{caseID}/';
  153.  
  154. return new Y.Resource({
  155. uri : Y.Lang.sub(uri, {
  156. userID : this.get(USER_ID),
  157. caseID : this.get(CASE_ID)
  158. }),
  159. headers : {
  160. 'Accept' : 'application/json',
  161. 'Content-Type' : 'application/json'
  162. },
  163. on : {
  164. success : Y.bind(function(e){
  165. this.fire(E_CASE_DATA, { data: e.entity });
  166. }, this)
  167. }
  168. });
  169. },
  170.  
  171. _initCaseInfoResource : function () {
  172.  
  173. return new Y.Resource({
  174. uri : this.get(CASE_INFO_PATH),
  175. headers : { Accept: 'application/json' },
  176. on : {
  177. success : Y.bind(function(e){
  178.  
  179. var info = [];
  180. Y.each(e.entity.info, function(i){
  181. info.push(Y.Node.create(i));
  182. });
  183.  
  184. this.set(TITLE, e.entity.title);
  185. this.set(INFO_NODES, Y.all(info));
  186.  
  187. }, this)
  188. }
  189. });
  190. },
  191.  
  192. _getDiagnoses : function (v) {
  193.  
  194. var differential = this[STEPS_VIEW] ? this[STEPS_VIEW].getComponent('differential') : null,
  195. diagnoses;
  196.  
  197. if (differential) {
  198. diagnoses = [];
  199. differential.each(function(diagnosis){
  200. diagnoses.push({
  201. text : diagnosis.get('text'),
  202. value : diagnosis.get('value')
  203. });
  204. });
  205. return diagnoses;
  206. } else {
  207. return v;
  208. }
  209. },
  210.  
  211. _defCaseDataFn : function (e) {
  212.  
  213. var data = e.data;
  214.  
  215. this.set(STATE, data.state);
  216. this.set(STEP, data.step);
  217. this.set(DIAGNOSES, data.diagnoses);
  218. },
  219.  
  220. _afterStateChange : function (e) {
  221.  
  222. var VIEWS = CaseWindow.VIEWS,
  223. prevView = VIEWS[ e.prevVal ? (e.prevVal + VIEW) : null ],
  224. newView = VIEWS[ e.newVal + VIEW ];
  225.  
  226. this.showView(newView, prevView);
  227. },
  228.  
  229. _afterContentChange : function (e) {
  230.  
  231. var content = Y.one('#content'),
  232. temp;
  233.  
  234. if (e.prevVal) {
  235. temp = Y.Markout('body').div().getNode().hide().append(e.newVal);
  236. content.transition('fadeOut', { duration: 0.5 }, function(){
  237. this.empty(true).setContent(temp.get('children')).transition('fadeIn', { duration: 0.5 });
  238. });
  239. } else {
  240. content.setStyle('opacity', 0).setContent(e.newVal).transition('fadeIn', { duration: 0.5 });
  241. }
  242. }
  243.  
  244. }, {
  245.  
  246. // *** Static *** //
  247.  
  248. ATTRS : ATTRS,
  249. VIEWS : VIEWS
  250.  
  251. });
  252.  
  253. // *** IntroView *** //
  254.  
  255. VIEWS[ INTRO_VIEW ] = Y.Base.create(INTRO_VIEW, Y.Plugin.Base, [], {
  256.  
  257. initializer : function () {
  258.  
  259. var host = this.get(HOST),
  260. content = Y.Markout();
  261.  
  262. content.button().getNode().set('text', 'start!').on('click', Y.bind(function(e){
  263. e.preventDefault();
  264. host.start();
  265. }, this));
  266.  
  267. host.set(CONTENT, content.getNode());
  268. }
  269.  
  270. }, { NS: INTRO_VIEW });
  271.  
  272. // *** StepsView *** //
  273.  
  274. VIEWS[ STEPS_VIEW ] = Y.Base.create(STEPS_VIEW, Y.Plugin.Base, [Y.BaseComponentMgr], {
  275.  
  276. initializer : function () {
  277.  
  278. var host = this.get(HOST),
  279. content = Y.Markout(),
  280. infoViewerNode, differentialNode;
  281.  
  282. infoViewerNode = content.div({ id: 'infoViewer', className: 'section' })
  283. .div({ className: 'yui3-infoviewer-loading yui3-infoviewer-content' }).getNode();
  284.  
  285. differentialNode = content.div({ id: 'differential', className: 'section' }).getNode();
  286.  
  287. host.set(CONTENT, content.getNode());
  288.  
  289. this.addComponent('infoViewer', {
  290. requires : ['infoviewer'],
  291. initializer : function(){
  292. return new Y.InfoViewer({
  293. srcNode : infoViewerNode,
  294. infoNodes : host.get(INFO_NODES),
  295. step : host.get(STEP),
  296. render : true
  297. });
  298. }
  299. });
  300.  
  301. this.addComponent('differential', {
  302. requires : ['differential'],
  303. initializer : function(){
  304. return new Y.Differential({
  305. children : host.get(DIAGNOSES),
  306. render : differentialNode
  307. });
  308. }
  309. });
  310.  
  311. // get case info, create InfoViewer, and bind it to step changes
  312. host.get(CASE_INFO_RESOURCE).GET({ on: { success: Y.bind(function(){
  313. this.useComponent('infoViewer', 'differential', function(infoViewer, differential){
  314.  
  315. this.afterHostEvent(STEP+CHANGE, function(e){
  316. infoViewer.set(STEP, e.newVal);
  317. });
  318.  
  319. differential.on('more', Y.bind(host.more, host));
  320. differential.on('ready', Y.bind(host.ready, host));
  321. this.afterHostEvent(DIAGNOSES+CHANGE, function(e){
  322. differential.removeAll();
  323. differential.add(e.newVal);
  324. });
  325. // TODO remove this
  326. Y.Markout(differential.get('contentBox')).button().getNode().set('text', 'add').on('click', function(e){
  327. e.preventDefault();
  328. differential.add({});
  329. });
  330.  
  331. });
  332. }, this)}});
  333. }
  334.  
  335. }, { NS: STEPS_VIEW });
  336.  
  337. //*** DiagnosisView *** //
  338.  
  339. VIEWS[ DIAGNOSIS_VIEW ] = Y.Base.create(DIAGNOSIS_VIEW, Y.Plugin.Base, [Y.BaseComponentMgr], {
  340.  
  341. initializer : function () {
  342.  
  343. var host = this.get(HOST),
  344. content = Y.Markout(),
  345. form, fieldset;
  346.  
  347. form = content.form({ id: 'diagnosis', method: 'POST', action: '' });
  348. fieldset = form.fieldset();
  349. fieldset.legend().getNode().hide().set('text', 'diagnosis');
  350. fieldset.div({ className: 'controls' }).button().text("diagnose!");
  351.  
  352. host.set(CONTENT, content.getNode());
  353.  
  354. this.addComponent('diagnosis', {
  355. requires : ['diagnosis'],
  356. initializer : function(){
  357. return new Y.Diagnosis({ render: fieldset.getNode() });
  358. }
  359. });
  360.  
  361. this.useComponent('diagnosis', function(diagnosis){
  362.  
  363. form.getNode().on('submit', Y.bind(function(e){
  364. e.preventDefault();
  365. host.diagnose({
  366. text : diagnosis.get('text'),
  367. value : diagnosis.get('value')
  368. }, []);
  369. }, this));
  370.  
  371. });
  372. }
  373.  
  374. }, { NS: DIAGNOSIS_VIEW });
  375.  
  376. //*** DoneView *** //
  377.  
  378. VIEWS[ DONE_VIEW ] = Y.Base.create(DONE_VIEW, Y.Plugin.Base, [], {
  379.  
  380. initializer : function () {
  381.  
  382. this.get(HOST).set(CONTENT, Y.Markout().p().getNode().set('text', 'done!'));
  383. }
  384.  
  385. }, { NS: DONE_VIEW });
  386.  
  387. // *** Namespace *** //
  388.  
  389. Y.CaseWindow = CaseWindow;
Add Comment
Please, Sign In to add comment