// Licensed to Cloudera, Inc. under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. Cloudera, Inc. licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
function addTemplateFunctions(item) {
if (Mustache == "undefined") {
return;
}
function genericDate(val, item) {
var d = moment(Mustache.render(val, item));
if (d.isValid()) {
return d;
}
else {
var number = parseInt(Mustache.render(val, item)) || '';
if (number) {
var d = moment(number * 1000); // timestamp * 1000
if (d.isValid()) {
return d;
}
}
}
}
function genericFormatDate(val, item, format) {
var d = genericDate(val, item);
if (d) {
return d.format(format);
}
else {
return Mustache.render(val, item);
}
}
// Functions
item.preview = function () {
return function (val) {
return '' + $.trim(Mustache.render(val, item)) + '';
}
};
item.embeddeddownload = function () {
return function (val) {
return '' + $.trim(Mustache.render(val, item)) + '';
}
};
item.download = function () {
return function (val) {
return ' length) {
return _val.substr(0, length) + "…";
}
return _val;
}
}
// fix the fields that contain dots in the name
for (var prop in item) {
if (item.hasOwnProperty(prop) && prop.indexOf(".") > -1) {
item[prop.replace(/\./gi, "_")] = item[prop];
}
}
}
function fixTemplateDots(template) {
var _mustacheTmpl = template;
var _mustacheTags = _mustacheTmpl.match(/{{(.*?)}}/g);
$.each(_mustacheTags, function (cnt, tag) {
if (tag.indexOf(".") > -1) {
_mustacheTmpl = _mustacheTmpl.replace(tag, tag.replace(/\./gi, "_"))
}
});
return _mustacheTmpl;
}