home.vm.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 MOCK_TAGS = {
  19. 'history': {'name': 'History', 'id': 1, 'docs': [1], 'type': 'history'},
  20. 'trash': {'name': 'Trash', 'id': 3, 'docs': [2]},
  21. 'mine': [
  22. {'name': 'default', 'id': 2, 'docs': [3]},
  23. {'name': 'web', 'id': 3, 'docs': [3]}
  24. ],
  25. 'notmine': [
  26. {'name': 'romain', 'projects': [
  27. {'name': 'example', 'id': 20, 'docs': [10]},
  28. {'name': 'ex2', 'id': 30, 'docs': [10, 11]}
  29. ]},
  30. {'name': 'pai', 'projects': [
  31. {'name': 'example2', 'id': 20, 'docs': [10]}
  32. ]}
  33. ]
  34. };
  35. var MOCK_DOCUMENTS = {
  36. '1': {
  37. 'id': 1,
  38. 'name': 'my query history', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  39. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  40. },
  41. '2': {
  42. 'id': 2,
  43. 'name': 'my query 2 trashed', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  44. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  45. },
  46. '3': {
  47. 'id': 3,
  48. 'name': 'my query 3 tagged twice', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  49. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  50. },
  51. '10': {
  52. 'id': 10,
  53. 'name': 'my query 3 shared', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  54. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  55. },
  56. '11': {
  57. 'id': 11,
  58. 'name': 'my query 4 shared', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  59. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  60. }
  61. };
  62. var ALL_DOCUMENTS = json_docs;
  63. self.tags = ko.mapping.fromJS(json_tags);
  64. self.documents = ko.observableArray([]);
  65. self.page = ko.observable(1);
  66. self.documentsPerPage = ko.observable(50);
  67. self.selectedDoc = ko.observable(ko.mapping.fromJS({
  68. perms: {
  69. read: {
  70. users: [],
  71. groups: []
  72. }
  73. }
  74. }));
  75. self.selectedTag = ko.observable({});
  76. self.selectedForDelete = ko.observable({
  77. name: ''
  78. });
  79. self.trash = ko.computed(function () {
  80. return self.tags.trash;
  81. });
  82. self.history = ko.computed(function () {
  83. return self.tags.history;
  84. });
  85. self.myTags = ko.computed(function () {
  86. return self.tags.mine();
  87. });
  88. self.sharedTags = ko.computed(function () {
  89. return self.tags.notmine();
  90. });
  91. self.allTags = ko.computed(function () {
  92. var _all = [];
  93. _all.push(self.tags.history);
  94. _all.push(self.tags.trash);
  95. _all = _all.concat(self.tags.mine());
  96. _all = _all.concat(self.tags.notmine());
  97. return _all;
  98. });
  99. self.renderableDocuments = ko.computed(function () {
  100. return self.documents().slice((self.page() * 1 - 1) * self.documentsPerPage(), (self.page() * self.documentsPerPage()) - 1);
  101. });
  102. self.totalPages = ko.computed(function () {
  103. return Math.ceil(self.documents().length / self.documentsPerPage());
  104. });
  105. self.hasPrevious = ko.computed(function () {
  106. return self.page() > 1;
  107. });
  108. self.hasNext = ko.computed(function () {
  109. return self.page() < self.totalPages();
  110. });
  111. self.page.subscribe(function (value) {
  112. if (isNaN(value * 1)) {
  113. self.page(1);
  114. }
  115. if (value > self.totalPages()) {
  116. self.page(self.totalPages());
  117. }
  118. if (value < 1) {
  119. self.page(1);
  120. }
  121. });
  122. self.nextPage = function () {
  123. if (self.hasNext()) {
  124. self.page(self.page() + 1);
  125. }
  126. }
  127. self.previousPage = function () {
  128. if (self.hasPrevious()) {
  129. self.page(self.page() - 1);
  130. }
  131. }
  132. self.documents.subscribe(function () {
  133. self.page(1);
  134. });
  135. self.getTagById = function (tagId) {
  136. var _tag = null;
  137. $.each(self.allTags(), function (id, tag) {
  138. if (tag.hasOwnProperty("id") && tag.id() == tagId) {
  139. _tag = tag;
  140. }
  141. if (tag.hasOwnProperty("projects")) {
  142. $.each(tag.projects(), function (iid, itag) {
  143. if (itag.hasOwnProperty("id") && itag.id() == tagId) {
  144. _tag = itag;
  145. }
  146. });
  147. }
  148. });
  149. return _tag;
  150. };
  151. self.filterDocs = function (tag) {
  152. self.documents.removeAll();
  153. self.selectedTag(tag);
  154. var _docs = [];
  155. $.each(ALL_DOCUMENTS, function (id, doc) {
  156. if (tag.docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  157. _docs.push(doc);
  158. }
  159. });
  160. self.documents(_docs);
  161. }
  162. self.searchDocs = function (query) {
  163. self.documents.removeAll();
  164. var _docs = [];
  165. $.each(ALL_DOCUMENTS, function (id, doc) {
  166. if (self.selectedTag().docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  167. var _bigString = doc.name + doc.description + doc.owner + doc.lastModified;
  168. $.each(doc.tags, function (cnt, tag) {
  169. _bigString += tag.name;
  170. })
  171. if (_bigString.toLowerCase().indexOf($.trim(query.toLowerCase())) > -1) {
  172. _docs.push(doc);
  173. }
  174. }
  175. });
  176. self.documents(_docs);
  177. }
  178. self.createTag = function (tag_json) {
  179. var mapped_tag = ko.mapping.fromJS(tag_json);
  180. self.tags.mine.push(mapped_tag);
  181. }
  182. self.deleteTag = function (tag) {
  183. self.tags.mine.remove(tag);
  184. }
  185. }