home.vm.js 5.1 KB

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