application.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.ApplicationController = Em.Controller.extend({
  20. name: 'applicationController',
  21. clusterName: function () {
  22. return (App.router.get('clusterController.clusterName') || 'My Cluster');
  23. }.property('App.router.clusterController.clusterName'),
  24. clusterDisplayName: function () {
  25. var name = this.get('clusterName');
  26. var displayName = name.length > 13 ? name.substr(0, 10) + "..." : name;
  27. return displayName;
  28. }.property('clusterName'),
  29. isClusterDataLoaded: function() {
  30. return App.router.get('clusterController.isLoaded') && App.router.get('loggedIn');
  31. }.property('App.router.clusterController.isLoaded','App.router.loggedIn'),
  32. init: function(){
  33. this._super();
  34. },
  35. dataLoading: function () {
  36. var dfd = $.Deferred();
  37. var self = this;
  38. this.getUserPref(this.persistKey()).done(function () {
  39. var curPref = self.get('currentPrefObject');
  40. self.set('currentPrefObject', null);
  41. dfd.resolve(curPref);
  42. });
  43. return dfd.promise();
  44. },
  45. persistKey: function () {
  46. var loginName = App.router.get('loginName');
  47. return 'admin-settings-show-bg-' + loginName;
  48. },
  49. currentPrefObject: null,
  50. /**
  51. * get persist value from server with persistKey
  52. */
  53. getUserPref: function(key){
  54. return App.ajax.send({
  55. name: 'settings.get.user_pref',
  56. sender: this,
  57. data: {
  58. key: key
  59. },
  60. success: 'getUserPrefSuccessCallback',
  61. error: 'getUserPrefErrorCallback'
  62. });
  63. },
  64. getUserPrefSuccessCallback: function (response, request, data) {
  65. if (response != null) {
  66. console.log('Got persist value from server with key ' + data.key + '. Value is: ' + response);
  67. this.set('currentPrefObject', response);
  68. return response;
  69. }
  70. },
  71. getUserPrefErrorCallback: function (request, ajaxOptions, error) {
  72. // this user is first time login
  73. if (request.status == 404) {
  74. console.log('Persist did NOT find the key');
  75. this.set('currentPrefObject', true);
  76. this.postUserPref(this.persistKey(), true);
  77. return true;
  78. }
  79. },
  80. /**
  81. * post persist key/value to server, value is object
  82. */
  83. postUserPref: function (key, value) {
  84. var keyValuePair = {};
  85. keyValuePair[key] = JSON.stringify(value);
  86. App.ajax.send({
  87. 'name': 'settings.post.user_pref',
  88. 'sender': this,
  89. 'beforeSend': 'postUserPrefBeforeSend',
  90. 'data': {
  91. 'keyValuePair': keyValuePair
  92. }
  93. });
  94. },
  95. postUserPrefBeforeSend: function(request, ajaxOptions, data){
  96. console.log('BeforeSend to persist: persistKeyValues', data.keyValuePair);
  97. },
  98. showSettingsPopup: function() {
  99. // Settings only for admins
  100. if (!App.get('isAdmin')) return;
  101. var self = this;
  102. var curValue = null;
  103. this.dataLoading().done(function (initValue) {
  104. App.ModalPopup.show({
  105. header: Em.I18n.t('common.userSettings'),
  106. bodyClass: Em.View.extend({
  107. templateName: require('templates/common/settings'),
  108. isNotShowBgChecked: !initValue,
  109. updateValue: function () {
  110. curValue = !this.get('isNotShowBgChecked');
  111. }.observes('isNotShowBgChecked')
  112. }),
  113. primary: Em.I18n.t('common.save'),
  114. onPrimary: function() {
  115. if (curValue == null) {
  116. curValue = initValue;
  117. }
  118. var key = self.persistKey();
  119. if (!App.testMode) {
  120. self.postUserPref(key, curValue);
  121. }
  122. this.hide();
  123. },
  124. onSecondary: function() {
  125. this.hide();
  126. }
  127. })
  128. });
  129. }
  130. });