user_settings_controller.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /**
  20. * Controller for user settings
  21. * Allows to get them from persist and update them to the persist
  22. *
  23. * @class UserSettingsController
  24. */
  25. App.UserSettingsController = Em.Controller.extend(App.UserPref, {
  26. name: 'userSettingsController',
  27. /**
  28. * @type {object}
  29. */
  30. userSettings: {},
  31. /**
  32. * Each property type is {name: string, defaultValue: *}
  33. * @type {object}
  34. */
  35. userSettingsKeys: function () {
  36. var loginName = App.router.get('loginName');
  37. var prefix = 'admin-settings-';
  38. return {
  39. show_bg: {
  40. name: prefix +'show-bg-' + loginName,
  41. defaultValue: true
  42. },
  43. timezone: {
  44. name: prefix + 'timezone-' + loginName,
  45. defaultValue: ''
  46. }
  47. };
  48. }.property('App.router.loginName'),
  49. /**
  50. * Load some user's setting from the persist
  51. * If <code>persistKey</code> is not provided, all settings are loaded
  52. * @param {string} [persistKey]
  53. * @method dataLoading
  54. * @returns {$.Deferred.promise}
  55. */
  56. dataLoading: function (persistKey) {
  57. var key = persistKey ? this.get('userSettingsKeys.' + persistKey + '.name') : '';
  58. var dfd = $.Deferred();
  59. var self = this;
  60. this.getUserPref(key).complete(function () {
  61. var curPref = self.get('currentPrefObject');
  62. self.set('currentPrefObject', null);
  63. dfd.resolve(curPref);
  64. });
  65. return dfd.promise();
  66. },
  67. getUserPrefSuccessCallback: function (response) {
  68. if (!Em.isNone(response)) {
  69. this.updateUserPrefWithDefaultValues(response);
  70. }
  71. this.set('currentPrefObject', response);
  72. return response;
  73. },
  74. getUserPrefErrorCallback: function (request) {
  75. // this user is first time login
  76. if (404 == request.status) {
  77. this.updateUserPrefWithDefaultValues();
  78. }
  79. },
  80. /**
  81. * Load all current user's settings to the <code>userSettings</code>
  82. * @method getAllUserSettings
  83. */
  84. getAllUserSettings: function () {
  85. var userSettingsKeys = this.get('userSettingsKeys');
  86. var userSettings = {};
  87. this.dataLoading().done(function (json) {
  88. Object.keys(userSettingsKeys).forEach(function (k) {
  89. userSettings[k] = JSON.parse(json[userSettingsKeys[k].name]);
  90. });
  91. });
  92. this.set('userSettings', userSettings);
  93. },
  94. /**
  95. * If user doesn't have any settings stored in the persist,
  96. * default values should be populated there
  97. * @param {object} [response]
  98. * @method updateUserPrefWithDefaultValues
  99. */
  100. updateUserPrefWithDefaultValues: function (response) {
  101. response = response || {};
  102. var keys = this.get('userSettingsKeys');
  103. var self = this;
  104. Object.keys(keys).forEach(function (key) {
  105. if (Em.isNone(response[keys[key].name])) {
  106. self.postUserPref(keys[key].name, keys[key].defaultValue);
  107. }
  108. });
  109. },
  110. /**
  111. * "Short"-key method for post user settings to the persist
  112. * Example:
  113. * real key is something like 'userSettingsKeys.timezone.name'
  114. * but user should call this method with 'timezone'
  115. * @method postUserPref
  116. * @param {string} key
  117. * @param {*} value
  118. * @returns {*}
  119. */
  120. postUserPref: function (key, value) {
  121. return this._super(this.get('userSettingsKeys.' + key + '.name'), value);
  122. },
  123. /**
  124. * Sync <code>userSettingsKeys</code> after each POST-update
  125. * @returns {*}
  126. */
  127. postUserPrefSuccessCallback: function () {
  128. return this.getAllUserSettings();
  129. },
  130. /**
  131. * Open popup with user settings
  132. * @method showSettingsPopup
  133. */
  134. showSettingsPopup: function() {
  135. // Settings only for admins
  136. if (!App.isAccessible('upgrade_ADMIN')) {
  137. return;
  138. }
  139. var self = this;
  140. var curValue = null;
  141. var keys = this.get('userSettingsKeys');
  142. this.dataLoading().done(function (response) {
  143. var initValue = JSON.parse(response[keys.show_bg.name]);
  144. var initTimezone = JSON.parse(response[keys.timezone.name]);
  145. return App.ModalPopup.show({
  146. header: Em.I18n.t('common.userSettings'),
  147. bodyClass: Em.View.extend({
  148. templateName: require('templates/common/settings'),
  149. isNotShowBgChecked: !initValue,
  150. updateValue: function () {
  151. curValue = !this.get('isNotShowBgChecked');
  152. }.observes('isNotShowBgChecked'),
  153. /**
  154. * @type {string[]}
  155. */
  156. timezonesList: [''].concat(moment.tz.names())
  157. }),
  158. /**
  159. * @type {string}
  160. */
  161. selectedTimezone: initTimezone,
  162. primary: Em.I18n.t('common.save'),
  163. onPrimary: function() {
  164. if (Em.isNone(curValue)) {
  165. curValue = initValue;
  166. }
  167. if (!App.get('testMode')) {
  168. self.postUserPref('show_bg', curValue);
  169. self.postUserPref('timezone', this.get('selectedTimezone'));
  170. }
  171. if (this.needsPageRefresh()) {
  172. location.reload();
  173. }
  174. this._super();
  175. },
  176. /**
  177. * Determines if page should be refreshed after user click "Save"
  178. * @returns {boolean}
  179. */
  180. needsPageRefresh: function () {
  181. return initTimezone !== this.get('selectedTimezone');
  182. }
  183. })
  184. });
  185. }
  186. });