home.vm.js 5.2 KB

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