home.vm.js 5.6 KB

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