home.vm.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. function HomeViewModel(json_tags, json_docs) {
  17. var self = this;
  18. var ALL_DOCUMENTS = json_docs;
  19. self.tags = ko.mapping.fromJS(json_tags);
  20. self.documents = ko.observableArray([]);
  21. self.page = ko.observable(1);
  22. self.documentsPerPage = ko.observable(50);
  23. self.selectedDoc = ko.observable(ko.mapping.fromJS({
  24. perms: {
  25. read: {
  26. users: [],
  27. groups: []
  28. }
  29. }
  30. }));
  31. self.selectedTag = ko.observable({});
  32. self.selectedTagForDelete = ko.observable({
  33. name: ''
  34. });
  35. self.trash = ko.computed(function () {
  36. return self.tags.trash;
  37. });
  38. self.history = ko.computed(function () {
  39. return self.tags.history;
  40. });
  41. self.myTags = ko.computed(function () {
  42. return self.tags.mine();
  43. });
  44. self.sharedTags = ko.computed(function () {
  45. return self.tags.notmine();
  46. });
  47. self.allTags = ko.computed(function () {
  48. var _all = [];
  49. _all.push(self.tags.history);
  50. _all.push(self.tags.trash);
  51. _all = _all.concat(self.tags.mine());
  52. _all = _all.concat(self.tags.notmine());
  53. return _all;
  54. });
  55. self.renderableDocuments = ko.computed(function () {
  56. return self.documents().slice((self.page() * 1 - 1) * self.documentsPerPage(), (self.page() * self.documentsPerPage()) - 1);
  57. });
  58. self.totalPages = ko.computed(function () {
  59. return Math.ceil(self.documents().length / self.documentsPerPage());
  60. });
  61. self.hasPrevious = ko.computed(function () {
  62. return self.page() > 1;
  63. });
  64. self.hasNext = ko.computed(function () {
  65. return self.page() < self.totalPages();
  66. });
  67. self.page.subscribe(function (value) {
  68. if (isNaN(value * 1)) {
  69. self.page(1);
  70. }
  71. if (value > self.totalPages()) {
  72. self.page(self.totalPages());
  73. }
  74. if (value < 1) {
  75. self.page(1);
  76. }
  77. });
  78. self.nextPage = function () {
  79. if (self.hasNext()) {
  80. self.page(self.page() + 1);
  81. }
  82. }
  83. self.previousPage = function () {
  84. if (self.hasPrevious()) {
  85. self.page(self.page() - 1);
  86. }
  87. }
  88. self.documents.subscribe(function () {
  89. self.page(1);
  90. });
  91. self.getTagById = function (tagId) {
  92. var _tag = null;
  93. $.each(self.allTags(), function (id, tag) {
  94. if (tag.hasOwnProperty("id") && tag.id() == tagId) {
  95. _tag = tag;
  96. }
  97. if (tag.hasOwnProperty("projects")) {
  98. $.each(tag.projects(), function (iid, itag) {
  99. if (itag.hasOwnProperty("id") && itag.id() == tagId) {
  100. _tag = itag;
  101. }
  102. });
  103. }
  104. });
  105. return _tag;
  106. }
  107. self.getDocById = function (docId) {
  108. var _doc = null;
  109. $.each(ALL_DOCUMENTS, function (id, doc) {
  110. if (doc.id == docId) {
  111. _doc = doc;
  112. }
  113. });
  114. return _doc;
  115. }
  116. self.updateDoc = function (doc) {
  117. $.each(ALL_DOCUMENTS, function (id, iDoc) {
  118. if (iDoc.id == doc.id) {
  119. ALL_DOCUMENTS[id] = doc;
  120. }
  121. });
  122. self.filterDocs(self.selectedTag());
  123. }
  124. self.filterDocs = function (tag) {
  125. self.documents.removeAll();
  126. self.selectedTag(tag);
  127. var _docs = [];
  128. $.each(ALL_DOCUMENTS, function (id, doc) {
  129. if (tag.docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  130. _docs.push(doc);
  131. }
  132. });
  133. self.documents(_docs);
  134. }
  135. self.searchDocs = function (query) {
  136. self.documents.removeAll();
  137. var _docs = [];
  138. $.each(ALL_DOCUMENTS, function (id, doc) {
  139. if (self.selectedTag().docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  140. var _bigString = doc.name + doc.description + doc.owner + doc.lastModified;
  141. $.each(doc.tags, function (cnt, tag) {
  142. _bigString += tag.name;
  143. })
  144. if (_bigString.toLowerCase().indexOf($.trim(query.toLowerCase())) > -1) {
  145. _docs.push(doc);
  146. }
  147. }
  148. });
  149. self.documents(_docs);
  150. }
  151. self.createTag = function (tag_json) {
  152. var mapped_tag = ko.mapping.fromJS(tag_json);
  153. self.tags.mine.push(mapped_tag);
  154. }
  155. self.deleteTag = function (tag) {
  156. self.tags.mine.remove(tag);
  157. }
  158. }