home.vm.js 5.9 KB

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