home.vm.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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': [{'name': 'default', 'id': 2, 'docs': [3]}, {'name': 'web', 'id': 3, 'docs': [3]}],
  22. 'notmine': [
  23. {'name': 'romain', 'projects': [{'name': 'example', 'id': 20, 'docs': [10]}, {'name': 'ex2', 'id': 30, 'docs': [10, 11]}]},
  24. {'name': 'pai', 'projects': [{'name': 'example2', 'id': 20, 'docs': [10]}]}
  25. ]
  26. };
  27. var MOCK_DOCUMENTS = {
  28. '1': {
  29. 'id': 1,
  30. 'name': 'my query history', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  31. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  32. },
  33. '2': {
  34. 'id': 2,
  35. 'name': 'my query 2 trashed', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  36. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  37. },
  38. '3': {
  39. 'id': 3,
  40. 'name': 'my query 3 tagged twice', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  41. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  42. },
  43. '10': {
  44. 'id': 10,
  45. 'name': 'my query 3 shared', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  46. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  47. },
  48. '11': {
  49. 'id': 11,
  50. 'name': 'my query 4 shared', 'description': '', 'url': '/beeswax/execute/design/83', 'icon': '/beeswax/static/art/icon_beeswax_24.png',
  51. 'lastModified': '03/11/14 16:06:49', 'owner': 'admin', 'lastModifiedInMillis': 1394579209.0, 'isMine': true
  52. }
  53. };
  54. var ALL_DOCUMENTS = json_docs;
  55. self.tags = ko.mapping.fromJS(json_tags);
  56. self.documents = ko.observableArray([]);
  57. self.editTagsToCreate = ko.observableArray([]);
  58. self.editTagsToDelete = ko.observableArray([]);
  59. self.trash = ko.computed(function() {
  60. return self.tags.trash;
  61. });
  62. self.history = ko.computed(function() {
  63. return self.tags.history;
  64. });
  65. self.myTags = ko.computed(function() {
  66. return self.tags.mine();
  67. });
  68. self.sharedTags = ko.computed(function() {
  69. return self.tags.notmine();
  70. });
  71. self.filterDocs = function(tag) {
  72. self.documents.removeAll();
  73. $.each(ALL_DOCUMENTS, function(id, doc) {
  74. if (tag.docs().indexOf(parseInt(id)) != -1) { // Beware, keys are strings in js
  75. self.documents.push(doc); // pushall?
  76. }
  77. })
  78. }
  79. self.createTag = function(tag_json) {
  80. var mapped_tag = ko.mapping.fromJS({'name': 'default2', 'id': 50, 'docs': [3]}); // todo
  81. self.tags.mine.push(mapped_tag);
  82. }
  83. }