search.utils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 addTemplateFunctions(item) {
  17. if (Mustache == "undefined") {
  18. return;
  19. }
  20. function genericDate(val, item) {
  21. var d = moment(Mustache.render(val, item));
  22. if (d.isValid()) {
  23. return d;
  24. }
  25. else {
  26. var number = parseInt(Mustache.render(val, item)) || '';
  27. if (number) {
  28. var d = moment(number * 1000); // timestamp * 1000
  29. if (d.isValid()) {
  30. return d;
  31. }
  32. }
  33. }
  34. }
  35. function genericFormatDate(val, item, format) {
  36. var d = genericDate(val, item);
  37. if (d) {
  38. return d.format(format);
  39. }
  40. else {
  41. return Mustache.render(val, item);
  42. }
  43. }
  44. // Functions
  45. item.hue_fn_preview = function () {
  46. return function (val) {
  47. return '<a href="/filebrowser/view/' + $.trim(Mustache.render(val, item)) + '">' + $.trim(Mustache.render(val, item)) + '</a>';
  48. }
  49. };
  50. item.hue_fn_embeddeddownload = function () {
  51. return function (val) {
  52. return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '?disposition=inline">' + $.trim(Mustache.render(val, item)) + '</a>';
  53. }
  54. };
  55. item.hue_fn_download = function () {
  56. return function (val) {
  57. return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '>' + $.trim(Mustache.render(val, item)) + '</a>';
  58. }
  59. };
  60. item.hue_fn_date = function () {
  61. return function (val) {
  62. return genericFormatDate(val, item, "DD-MM-YYYY");
  63. }
  64. };
  65. item.hue_fn_time = function () {
  66. return function (val) {
  67. return genericFormatDate(val, item, "HH:mm:ss");
  68. }
  69. };
  70. item.hue_fn_datetime = function () {
  71. return function (val) {
  72. return genericFormatDate(val, item, "DD-MM-YYYY HH:mm:ss");
  73. }
  74. };
  75. item.hue_fn_fulldate = function () {
  76. return function (val) {
  77. return genericFormatDate(val, item, null);
  78. }
  79. };
  80. item.hue_fn_timestamp = function () {
  81. return function (val) {
  82. var d = moment(Mustache.render(val, item));
  83. if (d.isValid()) {
  84. return d.valueOf();
  85. }
  86. else {
  87. return Mustache.render(val, item);
  88. }
  89. }
  90. };
  91. item.hue_fn_fromnow = function () {
  92. return function (val) {
  93. var d = genericDate(val, item);
  94. if (d && d.isValid()) {
  95. return d.fromNow();
  96. }
  97. else {
  98. return Mustache.render(val, item);
  99. }
  100. }
  101. };
  102. item.hue_fn_truncate50 = function () {
  103. return _truncate(50);
  104. };
  105. item.hue_fn_truncate100 = function () {
  106. return _truncate(100);
  107. };
  108. item.hue_fn_truncate200 = function () {
  109. return _truncate(200);
  110. };
  111. item.hue_fn_truncate250 = function () {
  112. return _truncate(250);
  113. };
  114. item.hue_fn_truncate500 = function () {
  115. return _truncate(500);
  116. };
  117. item.hue_fn_truncate1000 = function () {
  118. return _truncate(1000);
  119. };
  120. function _truncate(length) {
  121. return function (val) {
  122. var _val = $.trim(Mustache.render(val, item));
  123. if (_val.length > length) {
  124. return _val.substr(0, length) + "&hellip;";
  125. }
  126. return _val;
  127. }
  128. }
  129. // fix the fields that contain dots in the name
  130. for (var prop in item) {
  131. if (item.hasOwnProperty(prop) && prop.indexOf(".") > -1) {
  132. item[prop.replace(/\./gi, "_")] = item[prop];
  133. }
  134. }
  135. }
  136. function fixTemplateDotsAndFunctionNames(template) {
  137. var _mustacheTmpl = stripHtmlFromFunctions(template);
  138. var _mustacheTags = _mustacheTmpl.match(/{{(.*?)}}/g);
  139. if (_mustacheTags){
  140. $.each(_mustacheTags, function (cnt, tag) {
  141. if (tag.indexOf("{#") > -1) {
  142. _mustacheTmpl = _mustacheTmpl.replace(tag, tag.replace(/\#/gi, "#hue_fn_"))
  143. }
  144. if (tag.indexOf("{/") > -1) {
  145. _mustacheTmpl = _mustacheTmpl.replace(tag, tag.replace(/\//gi, "/hue_fn_"))
  146. }
  147. if (tag.indexOf(".") > -1) {
  148. _mustacheTmpl = _mustacheTmpl.replace(tag, tag.replace(/\./gi, "_"))
  149. }
  150. });
  151. _mustacheTmpl = _mustacheTmpl.replace(/\{\{(.+?)\}\}/g, "{{{$1}}}");
  152. _mustacheTmpl = _mustacheTmpl.replace(/\{\{\{\#hue_fn(.+?)\}\}\}/g, "{{#hue_fn$1}}")
  153. _mustacheTmpl = _mustacheTmpl.replace(/\{\{\{\/hue_fn(.+?)\}\}\}/g, "{{/hue_fn$1}}")
  154. }
  155. return _mustacheTmpl;
  156. }
  157. function stripHtmlFromFunctions(template) {
  158. // strips HTML from inside the functions
  159. var _tmpl = template;
  160. var _mustacheFunctions = _tmpl.match(/{{#(.[\s\S]*?){{\//g);
  161. if (_mustacheFunctions){
  162. $.each(_mustacheFunctions, function (cnt, fn) {
  163. _tmpl = _tmpl.replace(fn, fn.substr(0, fn.indexOf("}}") + 2) + $.trim(stripHtml(fn.substr(fn.indexOf("}}") + 2).slice(0, -3))) + "{{/");
  164. });
  165. }
  166. return _tmpl;
  167. }
  168. function stripHtml(html) {
  169. var tmp = document.createElement("DIV");
  170. tmp.innerHTML = html;
  171. return tmp.textContent || tmp.innerText;
  172. }