home.vm.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.editTagsToCreate = ko.observableArray([]);
  68. self.editTagsToDelete = ko.observableArray([]);
  69. self.selectedTag = ko.observable({});
  70. self.selectedForDelete = ko.observable({
  71. name: ''
  72. });
  73. self.trash = ko.computed(function () {
  74. return self.tags.trash;
  75. });
  76. self.history = ko.computed(function () {
  77. return self.tags.history;
  78. });
  79. self.myTags = ko.computed(function () {
  80. return self.tags.mine();
  81. });
  82. self.sharedTags = ko.computed(function () {
  83. return self.tags.notmine();
  84. });
  85. self.allTags = ko.computed(function () {
  86. var _all = [];
  87. _all.push(self.tags.history);
  88. _all.push(self.tags.trash);
  89. _all = _all.concat(self.tags.mine());
  90. _all = _all.concat(self.tags.notmine());
  91. return _all;
  92. });
  93. self.renderableDocuments = ko.computed(function () {
  94. return self.documents().slice((self.page() * 1 - 1) * self.documentsPerPage(), (self.page() * self.documentsPerPage()) - 1);
  95. });
  96. self.totalPages = ko.computed(function () {
  97. return Math.ceil(self.documents().length / self.documentsPerPage());
  98. });
  99. self.hasPrevious = ko.computed(function () {
  100. return self.page() > 1;
  101. });
  102. self.hasNext = ko.computed(function () {
  103. return self.page() < self.totalPages();
  104. });
  105. self.page.subscribe(function (value) {
  106. if (isNaN(value * 1)) {
  107. self.page(1);
  108. }
  109. if (value > self.totalPages()) {
  110. self.page(self.totalPages());
  111. }
  112. if (value < 1) {
  113. self.page(1);
  114. }
  115. });
  116. self.nextPage = function () {
  117. if (self.hasNext()) {
  118. self.page(self.page() + 1);
  119. }
  120. }
  121. self.previousPage = function () {
  122. if (self.hasPrevious()) {
  123. self.page(self.page() - 1);
  124. }
  125. }
  126. self.documents.subscribe(function () {
  127. self.page(1);
  128. });
  129. self.getTagById = function (tag_id) {
  130. var _tag = null;
  131. $.each(self.allTags(), function (id, tag) {
  132. if (tag.hasOwnProperty("id") && tag.id() == tag_id) {
  133. _tag = tag;
  134. }
  135. if (tag.hasOwnProperty("projects")) {
  136. $.each(tag.projects(), function (iid, itag) {
  137. if (itag.hasOwnProperty("id") && itag.id() == tag_id) {
  138. _tag = itag;
  139. }
  140. });
  141. }
  142. });
  143. return _tag;
  144. };
  145. self.filterDocs = function (tag) {
  146. self.documents.removeAll();
  147. self.selectedTag(tag);
  148. var _docs = [];
  149. $.each(ALL_DOCUMENTS, function (id, doc) {
  150. if (tag.docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  151. _docs.push(doc);
  152. }
  153. });
  154. self.documents(_docs);
  155. }
  156. self.searchDocs = function (query) {
  157. self.documents.removeAll();
  158. var _docs = [];
  159. $.each(ALL_DOCUMENTS, function (id, doc) {
  160. if (self.selectedTag().docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  161. var _bigString = doc.name + doc.description + doc.owner + doc.lastModified;
  162. $.each(doc.tags, function (cnt, tag) {
  163. _bigString += tag.name;
  164. })
  165. if (_bigString.toLowerCase().indexOf($.trim(query.toLowerCase())) > -1) {
  166. _docs.push(doc);
  167. }
  168. }
  169. });
  170. self.documents(_docs);
  171. }
  172. self.createTag = function (tag_json) {
  173. var mapped_tag = ko.mapping.fromJS(tag_json);
  174. self.tags.mine.push(mapped_tag);
  175. }
  176. self.deleteTag = function (tag) {
  177. self.tags.mine.remove(tag);
  178. }
  179. }