assist.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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() {
  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)) {
  51. options.onDataReceived(_cached.data);
  52. _returnCached = true;
  53. }
  54. $.ajax({
  55. type: "GET",
  56. url: _url + "?" + Math.random(),
  57. success: function (data) {
  58. var _obj = {
  59. data: data,
  60. timestamp: (new Date()).getTime()
  61. }
  62. $.totalStorage(_cachePath, _obj);
  63. if (!_returnCached) {
  64. options.onDataReceived($.totalStorage(_cachePath).data);
  65. }
  66. },
  67. async: options.sync == "undefined"
  68. });
  69. }
  70. self.options = options;
  71. self.getData = function (path) {
  72. self.path(path);
  73. self.options.firstLevel = null;
  74. self.options.secondLevel = null;
  75. if (path) {
  76. if (path.indexOf("/") > -1) {
  77. self.options.firstLevel = path.split("/")[0];
  78. self.options.secondLevel = path.split("/")[1];
  79. }
  80. else {
  81. self.options.firstLevel = path;
  82. }
  83. }
  84. jsonCalls();
  85. }
  86. // ko observables
  87. self.isLoading = ko.observable(true);
  88. self.path = ko.observable();
  89. self.selectedMainObject = ko.observable();
  90. self.mainObjects = ko.observableArray([]);
  91. self.firstLevelObjects = ko.observable({});
  92. }