template.ko.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. var Query = function (importable) {
  17. var self = this;
  18. self.type = ko.observable(importable.type);
  19. self.name = ko.observable(importable.name);
  20. self.selected = ko.observable(false);
  21. self.handleSelect = function (row, e) {
  22. this.selected(!this.selected());
  23. };
  24. };
  25. var Collection = function (coll) {
  26. var self = this;
  27. self.id = ko.observable(coll.id);
  28. self.name = ko.observable(coll.name);
  29. self.label = ko.observable(coll.label);
  30. self.isCoreOnly = ko.observable(coll.isCoreOnly);
  31. self.absoluteUrl = ko.observable(coll.absoluteUrl);
  32. self.selected = ko.observable(false);
  33. self.hovered = ko.observable(false);
  34. self.handleSelect = function (row, e) {
  35. this.selected(!this.selected());
  36. };
  37. self.toggleHover = function (row, e) {
  38. this.hovered(!this.hovered());
  39. };
  40. };
  41. // sorting
  42. // highlithing
  43. //
  44. // Facets (text or chart)
  45. // field
  46. // range
  47. // spacial search
  48. // query facet
  49. // pivot facet
  50. var SearchViewModel = function (result, facets) {
  51. var self = this;
  52. // Mock testing
  53. //var TEMPLATE = {"extracode": " \n<style type=\"text/css\">\nem {\n font-weight: bold;\n background-color: yellow;\n}\n.avatar {\n margin: 10px;\n}\n.created {\n margin-top: 10px;\n color: #CCC;\n}\n.openTweet {\n float: right;\n margin: 10px;\n}\n</style>\n \n ", "highlighting": ["text"], "properties": {"highlighting_enabled": true}, "template": "\n\n<div class=\"row-fluid\">\n <div class=\"row-fluid\">\n <div class=\"row-fluid\">\n <div class=\"span1\">\n <img src=\"http://twitter.com/api/users/profile_image/{{user_screen_name}}\" class=\"avatar\">\n </div>\n <div class=\"span11\">\n <b>{{user_name}}</b>\n <br>\n <a href=\"https://twitter.com/{{user_screen_name}}/status/{{id}}\" target=\"_blank\">\n {{text}}\n </a>\n <br>\n <div class=\"created\">{{#fromnow}}{{created_at}}{{/fromnow}}</div>\n </div>\n </div>\n <br>\n </div>\n </div>\n \n "}
  54. //var FACETS = {"dates": [], "fields": [{"uuid": "f6618a5c-bbba-2886-1886-bbcaf01409ca", "verbatim": "", "isVerbatim": false, "label": "Location", "field": "user_location", "type": "field"}], "charts": [{"end": "2014-02-28T12:00:00Z", "uuid": "4883871c-0cea-8547-de60-7166d498098a", "verbatim": "", "start": "2014-02-28T12:00:00Z-10DAYS", "isVerbatim": false, "label": "Posted", "field": "created_at", "gap": "+5MINUTES", "type": "chart"}], "properties": {"sort": "count", "mincount": 1, "isEnabled": true, "limit": 10}, "ranges": [{"end": "1000", "uuid": "5533165a-0b1c-21b6-4ede-9d2fc301ed6b", "verbatim": "", "start": 0, "isVerbatim": false, "label": "Followers count", "field": "user_followers_count", "gap": "100", "type": "range"}, {"end": "10000", "uuid": "d5e66f3d-ca7d-67d7-05c7-33ec499cc106", "verbatim": "", "start": 0, "isVerbatim": false, "label": "Tweet count", "field": "user_statuses_count", "gap": "1000", "type": "range"}], "order": ["f6618a5c-bbba-2886-1886-bbcaf01409ca", "5533165a-0b1c-21b6-4ede-9d2fc301ed6b", "d5e66f3d-ca7d-67d7-05c7-33ec499cc106"]}
  55. var TEMPLATE = {"extracode": "", "highlighting": ["text"], "properties": {"highlighting_enabled": true},
  56. "template": "{{user_screen_name}} {{user_name}} {{text}}", "isGridLayout": true, "fields": ["user_screen_name", "user_name", "text"]
  57. };
  58. var FACETS = {"dates": [], "fields": [
  59. {
  60. "uuid": "f6618a5c-bbba-2886-1886-bbcaf01409ca", "verbatim": "", "isVerbatim": false, "label": "Location",
  61. "field": "user_location", "type": "field"
  62. }
  63. ],
  64. "charts": [], "properties": {"sort": "count", "mincount": 1, "isEnabled": true, "limit": 10}, "ranges": [], "order": []
  65. };
  66. // Collection customization
  67. var collection = 10000004;
  68. self.template = ko.mapping.fromJS(TEMPLATE); //result.template;
  69. self.template.fields.subscribe(function() {
  70. self.search();
  71. });
  72. self.template.template.subscribe(function() {
  73. self.search();
  74. });
  75. self.facets = ko.mapping.fromJS(FACETS.fields); //facets.fields
  76. self.fields = ko.observableArray(["user_screen_name", "user_name", "text", "created_at", "user_statuses_count", "id"]); // ad dynamic ajaxifoed
  77. // Query URL
  78. self.q = ko.observable('');
  79. self.qFacets = {}
  80. // Query results
  81. self.response = ko.observable({});
  82. self.results = ko.observableArray([]);
  83. self.norm_facets = ko.computed(function () {
  84. return self.response().normalized_facets;
  85. });
  86. // Forms
  87. self.selectedFacet = ko.observable();
  88. self.search = function () {
  89. $.post("/search/query2?format=json", {
  90. collection: collection,
  91. q: ko.toJSON(self.q),
  92. facets: ko.toJSON(self.facets),
  93. fq: ko.utils.stringifyJson(self.qFacets),
  94. template: ko.mapping.toJSON(self.template),
  95. }, function (data) {
  96. self.response(data);
  97. self.results.removeAll();
  98. if (data.error) {
  99. $(document).trigger("error", data.error);
  100. } else {
  101. if (self.template.isGridLayout()) {
  102. // Table view
  103. $.each(data.response.docs, function (index, item) {
  104. var row = [];
  105. $.each(self.template.fields(), function (index, column) {
  106. row.push(item[column]); // todo if is null
  107. });
  108. self.results.push(row);
  109. });
  110. } else {
  111. // Template view
  112. var _mustacheTmpl = fixTemplateDotsAndFunctionNames(self.template.template());
  113. $.each(data.response.docs, function (index, item) {
  114. addTemplateFunctions(item);
  115. self.results.push(Mustache.render(_mustacheTmpl, item));
  116. });
  117. }
  118. }
  119. }).fail(function(xhr, textStatus, errorThrown) {
  120. $(document).trigger("error", xhr.responseText); // cleanup all "alert jHueNotify alert-error" before
  121. });
  122. };
  123. self.addFacet = function(facet_json) {
  124. self.facets.push(ko.mapping.fromJS({
  125. "uuid": "f6618a5c-bbba-2886-1886-bbcaf01409ca", "verbatim": "", "isVerbatim": false, "label": "Location",
  126. "field": facet_json.name, "type": "field"
  127. }));
  128. }
  129. self.selectSingleFacet = function(normalized_facet_json) {
  130. $.each(self.facets(), function(index, facet) {
  131. if (facet.field() == normalized_facet_json.field) {
  132. self.selectedFacet(facet);
  133. }
  134. });
  135. }
  136. self.removeFacet = function(facet_json) {
  137. $.each(self.facets(), function(index, item) {
  138. if (item.field() == facet_json.field) {
  139. self.facets.remove(item);
  140. }
  141. });
  142. self.search();
  143. }
  144. self.selectFacet = function(facet_json) {
  145. self.qFacets[facet_json.cat] = facet_json.value;
  146. self.search();
  147. }
  148. self.unselectFacet = function(facet_json) {
  149. delete self.qFacets[facet_json.cat];
  150. self.search();
  151. }
  152. };