search.utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 genericFormatDate(val, item, format) {
  21. var d = moment(Mustache.render(val, item));
  22. if (d.isValid()) {
  23. return d.format(format);
  24. }
  25. else {
  26. return Mustache.render(val, item);
  27. }
  28. }
  29. item.preview = function () {
  30. return function (val) {
  31. return '<a href="/filebrowser/view/' + $.trim(Mustache.render(val, item)) + '">' + $.trim(Mustache.render(val, item)) + '</a>';
  32. }
  33. };
  34. item.embeddeddownload = function () {
  35. return function (val) {
  36. return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '?disposition=inline">' + $.trim(Mustache.render(val, item)) + '</a>';
  37. }
  38. };
  39. item.download = function () {
  40. return function (val) {
  41. return '<a href="/filebrowser/download/' + $.trim(Mustache.render(val, item)) + '>' + $.trim(Mustache.render(val, item)) + '</a>';
  42. }
  43. };
  44. item.date = function () {
  45. return function (val) {
  46. return genericFormatDate(val, item, "DD-MM-YYYY");
  47. }
  48. };
  49. item.time = function () {
  50. return function (val) {
  51. return genericFormatDate(val, item, "HH:mm:ss");
  52. }
  53. };
  54. item.datetime = function () {
  55. return function (val) {
  56. return genericFormatDate(val, item, "DD-MM-YYYY HH:mm:ss");
  57. }
  58. };
  59. item.fulldate = function () {
  60. return function (val) {
  61. return genericFormatDate(val, item, null);
  62. }
  63. };
  64. item.timestamp = function () {
  65. return function (val) {
  66. var d = moment(Mustache.render(val, item));
  67. if (d.isValid()) {
  68. return d.valueOf();
  69. }
  70. else {
  71. return Mustache.render(val, item);
  72. }
  73. }
  74. };
  75. item.fromnow = function () {
  76. return function (val) {
  77. var d = moment(Mustache.render(val, item));
  78. if (d.isValid()) {
  79. return d.fromNow();
  80. }
  81. else {
  82. return Mustache.render(val, item);
  83. }
  84. }
  85. };
  86. item.truncate50 = function () {
  87. return _truncate(50);
  88. };
  89. item.truncate100 = function () {
  90. return _truncate(100);
  91. };
  92. item.truncate200 = function () {
  93. return _truncate(200);
  94. };
  95. item.truncate250 = function () {
  96. return _truncate(250);
  97. };
  98. item.truncate500 = function () {
  99. return _truncate(500);
  100. };
  101. item.truncate1000 = function () {
  102. return _truncate(1000);
  103. };
  104. function _truncate(length) {
  105. return function (val) {
  106. var _val = $.trim(Mustache.render(val, item));
  107. if (_val.length > length) {
  108. return _val.substr(0, length) + "&hellip;";
  109. }
  110. return _val;
  111. }
  112. }
  113. // fix the fields that contain dots in the name
  114. for (var prop in item) {
  115. if (item.hasOwnProperty(prop) && prop.indexOf(".") > -1) {
  116. item[prop.replace(/\./gi, "_")] = item[prop];
  117. }
  118. }
  119. }
  120. function fixTemplateDots(template) {
  121. var _mustacheTmpl = template;
  122. var _mustacheTags = _mustacheTmpl.match(/{{(.*?)}}/g);
  123. $.each(_mustacheTags, function (cnt, tag) {
  124. if (tag.indexOf(".") > -1) {
  125. _mustacheTmpl = _mustacheTmpl.replace(tag, tag.replace(/\./gi, "_"))
  126. }
  127. });
  128. return _mustacheTmpl;
  129. }