home.vm.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.editTagsToCreate = ko.observableArray([]);
  66. self.editTagsToDelete = ko.observableArray([]);
  67. self.selectedTag = ko.observable("");
  68. self.selectedForDelete = ko.observable({
  69. name: ''
  70. });
  71. self.trash = ko.computed(function () {
  72. return self.tags.trash;
  73. });
  74. self.history = ko.computed(function () {
  75. return self.tags.history;
  76. });
  77. self.myTags = ko.computed(function () {
  78. return self.tags.mine();
  79. });
  80. self.sharedTags = ko.computed(function () {
  81. return self.tags.notmine();
  82. });
  83. self.allTags = ko.computed(function () {
  84. var _all = [];
  85. _all.push(self.tags.history);
  86. _all.push(self.tags.trash);
  87. _all = _all.concat(self.tags.mine());
  88. _all = _all.concat(self.tags.notmine());
  89. return _all;
  90. });
  91. self.getTagById = function (tag_id) {
  92. var _tag = null;
  93. $.each(self.allTags(), function (id, tag) {
  94. if (tag.hasOwnProperty("id") && tag.id() == tag_id) {
  95. _tag = tag;
  96. }
  97. if (tag.hasOwnProperty("projects")) {
  98. $.each(tag.projects(), function (iid, itag) {
  99. if (itag.hasOwnProperty("id") && itag.id() == tag_id) {
  100. _tag = itag;
  101. }
  102. });
  103. }
  104. });
  105. return _tag;
  106. };
  107. self.filterDocs = function (tag) {
  108. self.documents.removeAll();
  109. self.selectedTag(tag);
  110. var _docs = [];
  111. $.each(ALL_DOCUMENTS, function (id, doc) {
  112. if (tag.docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  113. _docs.push(doc);
  114. }
  115. });
  116. self.documents(_docs);
  117. }
  118. self.searchDocs = function (query) {
  119. self.documents.removeAll();
  120. var _docs = [];
  121. $.each(ALL_DOCUMENTS, function (id, doc) {
  122. if (self.selectedTag().docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  123. var _bigString = doc.name + doc.description + doc.owner + doc.lastModified;
  124. $.each(doc.tags, function (cnt, tag) {
  125. _bigString += tag.name;
  126. })
  127. if (_bigString.toLowerCase().indexOf($.trim(query.toLowerCase())) > -1) {
  128. _docs.push(doc);
  129. }
  130. }
  131. });
  132. self.documents(_docs);
  133. }
  134. self.createTag = function (tag_json) {
  135. var mapped_tag = ko.mapping.fromJS(tag_json);
  136. self.tags.mine.push(mapped_tag);
  137. }
  138. self.removeTag = function (tag) {
  139. self.tags.mine.remove(tag);
  140. }
  141. }