user_settings_controller.js 7.0 KB

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