assist.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. var Assist = function (options) {
  17. var self = this;
  18. function hasExpired(timestamp) {
  19. if (!timestamp) {
  20. return true;
  21. }
  22. var TIME_TO_LIVE_IN_MILLIS = 86400000; // 1 day
  23. return (new Date()).getTime() - timestamp > TIME_TO_LIVE_IN_MILLIS;
  24. }
  25. function getTotalStoragePrefix() {
  26. var _app = "";
  27. if (typeof options.app != "undefined") {
  28. _app = options.app;
  29. }
  30. if (typeof options.user != "undefined") {
  31. return _app + "_" + options.user + "_";
  32. }
  33. return (_app != "" ? _app + "_" : "");
  34. }
  35. function jsonCalls(force) {
  36. if (typeof options.baseURL == "undefined" || options.baseURL == null) {
  37. console.error("Assist should always have a baseURL set to work fine.");
  38. return null;
  39. }
  40. var _url = options.baseURL;
  41. if (options.firstLevel != null) {
  42. _url += options.firstLevel;
  43. }
  44. if (options.secondLevel != null) {
  45. _url += "/" + options.secondLevel;
  46. }
  47. var _cachePath = getTotalStoragePrefix() + _url;
  48. var _cached = $.totalStorage(_cachePath);
  49. var _returnCached = false;
  50. if (_cached != null && !hasExpired(_cached.timestamp) && typeof force == "undefined") {
  51. options.onDataReceived(_cached.data);
  52. _returnCached = true;
  53. }
  54. if (options.onError) {
  55. $.ajaxSetup({
  56. error: function(x, e) {
  57. if (x.status == 500) {
  58. self.hasErrors(true);
  59. options.onError(e);
  60. }
  61. }
  62. });
  63. }
  64. if (! _returnCached){
  65. $.ajax({
  66. type: "GET",
  67. url: _url + "?" + Math.random(),
  68. success: function (data) {
  69. if (data.error){
  70. if (typeof options.failsSilentlyOn == "undefined" || (data.code != null && options.failsSilentlyOn.indexOf(data.code) == -1)){
  71. $.jHueNotify.error(data.error);
  72. }
  73. }
  74. else {
  75. var _obj = {
  76. data: data,
  77. timestamp: (new Date()).getTime()
  78. }
  79. $.totalStorage(_cachePath, _obj);
  80. options.onDataReceived($.totalStorage(_cachePath).data);
  81. }
  82. },
  83. error: function (error) {
  84. $(document).trigger('error', error);
  85. }
  86. });
  87. }
  88. }
  89. self.options = options;
  90. self.getData = function (path, force) {
  91. self.path(path);
  92. self.options.firstLevel = null;
  93. self.options.secondLevel = null;
  94. if (path) {
  95. if (path.indexOf("/") > -1) {
  96. self.options.firstLevel = path.split("/")[0];
  97. self.options.secondLevel = path.split("/")[1];
  98. }
  99. else {
  100. self.options.firstLevel = path;
  101. }
  102. }
  103. jsonCalls(force);
  104. }
  105. // ko observables
  106. self.isLoading = ko.observable(true);
  107. self.hasErrors = ko.observable(false);
  108. self.path = ko.observable();
  109. self.selectedMainObject = ko.observable();
  110. self.mainObjects = ko.observableArray([]);
  111. self.firstLevelObjects = ko.observable({});
  112. self.filter = ko.observable("");
  113. self.filter.extend({ rateLimit: 150 });
  114. self.filteredFirstLevelObjects = ko.computed(function(){
  115. return ko.utils.arrayFilter(Object.keys(self.firstLevelObjects()), function(item) {
  116. return item.toLowerCase().indexOf(self.filter()) > -1;
  117. });
  118. });
  119. }