wizard_watcher_controller.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.WizardWatcherController = Em.Controller.extend(App.UserPref, {
  20. name: 'wizardWatcherController',
  21. /**
  22. * @const
  23. */
  24. PREF_KEY: 'wizard-data',
  25. /**
  26. * name of user who working with wizard
  27. * @type {string|null}
  28. */
  29. wizardUser: null,
  30. /**
  31. * @type {string|null}
  32. */
  33. controllerName: null,
  34. /**
  35. * @type {Function}
  36. */
  37. mock: Em.K,
  38. /**
  39. * define whether Wizard is running
  40. * @type {boolean}
  41. */
  42. isWizardRunning: Em.computed.bool('wizardUser'),
  43. /**
  44. * @type {string}
  45. */
  46. wizardDisplayName: function() {
  47. if (this.get('controllerName')) {
  48. return Em.I18n.t('wizard.inProgress').format(App.router.get(this.get('controllerName')).get('displayName'));
  49. }
  50. return "";
  51. }.property('controllerName'),
  52. /**
  53. * define whether logged in user is the one who started wizard
  54. * @type {boolean}
  55. */
  56. isNonWizardUser: function() {
  57. return this.get('isWizardRunning') && this.get('wizardUser') !== App.router.get('loginName');
  58. }.property('App.router.loginName', 'wizardUser').volatile(),
  59. /**
  60. * set user who launched wizard
  61. * @returns {$.ajax}
  62. */
  63. setUser: function(controllerName) {
  64. return this.postUserPref(this.get('PREF_KEY'), {
  65. userName: App.router.get('loginName'),
  66. controllerName: controllerName
  67. });
  68. },
  69. /**
  70. * reset user who launched wizard
  71. * @returns {$.ajax}
  72. */
  73. resetUser: function() {
  74. return this.postUserPref(this.get('PREF_KEY'), null);
  75. },
  76. /**
  77. * get user who launched wizard
  78. * @returns {$.ajax}
  79. */
  80. getUser: function() {
  81. return this.getUserPref(this.get('PREF_KEY'));
  82. },
  83. getUserPrefSuccessCallback: function(data) {
  84. if (Em.isNone(data)) {
  85. this.set('wizardUser', null);
  86. this.set('controllerName', null);
  87. } else {
  88. this.set('wizardUser', data.userName);
  89. this.set('controllerName', data.controllerName);
  90. }
  91. },
  92. getUserPrefErrorCallback: function () {
  93. this.resetUser();
  94. }
  95. });