search.utils.js 3.6 KB

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