// 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.
ko.bindingHandlers.slideVisible = {
init: function (element, valueAccessor) {
var value = valueAccessor();
$(element).toggle(ko.unwrap(value));
},
update: function (element, valueAccessor) {
var value = valueAccessor();
ko.unwrap(value) ? $(element).slideDown(100) : $(element).slideUp(100);
}
};
ko.bindingHandlers.fadeVisible = {
init: function (element, valueAccessor) {
var value = valueAccessor();
$(element).toggle(ko.unwrap(value));
},
update: function (element, valueAccessor) {
var value = valueAccessor();
$(element).stop();
ko.unwrap(value) ? $(element).fadeIn() : $(element).hide();
}
};
ko.extenders.numeric = function (target, precision) {
var result = ko.computed({
read: target,
write: function (newValue) {
var current = target(),
roundingMultiplier = Math.pow(10, precision),
newValueAsNum = isNaN(newValue) ? 0 : parseFloat(+newValue),
valueToWrite = Math.round(newValueAsNum * roundingMultiplier) / roundingMultiplier;
if (valueToWrite !== current) {
target(valueToWrite);
} else {
if (newValue !== current) {
target.notifySubscribers(valueToWrite);
}
}
}
}).extend({ notify: 'always' });
result(target());
return result;
};
ko.bindingHandlers.freshereditor = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var _el = $(element);
var options = $.extend(valueAccessor(), {});
_el.html(options.data());
_el.freshereditor({
excludes: ['strikethrough', 'removeFormat', 'insertorderedlist', 'justifyfull', 'insertheading1', 'insertheading2', 'superscript', 'subscript']
});
_el.freshereditor("edit", true);
_el.on("mouseup", function () {
storeSelection();
updateValues();
});
var sourceDelay = -1;
_el.on("keyup", function () {
clearTimeout(sourceDelay);
storeSelection();
sourceDelay = setTimeout(function () {
updateValues();
}, 100);
});
$(".chosen-select").chosen({
disable_search_threshold: 10,
width: "75%"
});
$(document).on("addFieldToVisual", function (e, field) {
_el.focus();
pasteHtmlAtCaret("{{" + field.name() + "}}");
});
$(document).on("addFunctionToVisual", function (e, fn) {
_el.focus();
pasteHtmlAtCaret(fn);
});
function updateValues() {
$("[data-template]")[0].editor.setValue(stripHtmlFromFunctions(_el.html()));
valueAccessor().data(_el.html());
}
function storeSelection() {
if (window.getSelection) {
// IE9 and non-IE
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
_el.data("range", range);
}
}
else if (document.selection && document.selection.type != "Control") {
// IE < 9
_el.data("selection", document.selection);
}
}
function pasteHtmlAtCaret(html) {
var sel, range;
if (window.getSelection) {
// IE9 and non-IE
sel = window.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
if (_el.data("range")) {
range = _el.data("range");
}
else {
range = sel.getRangeAt(0);
}
range.deleteContents();
// Range.createContextualFragment() would be useful here but is
// non-standard and not supported in all browsers (IE9, for one)
var el = document.createElement("div");
el.innerHTML = html;
var frag = document.createDocumentFragment(), node, lastNode;
while ((node = el.firstChild)) {
lastNode = frag.appendChild(node);
}
range.insertNode(frag);
// Preserve the selection
if (lastNode) {
range = range.cloneRange();
range.setStartAfter(lastNode);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
}
} else if (document.selection && document.selection.type != "Control") {
// IE < 9
if (_el.data("selection")) {
_el.data("selection").createRange().pasteHTML(html);
}
else {
document.selection.createRange().pasteHTML(html);
}
}
}
}
};
ko.bindingHandlers.slider = {
init: function (element, valueAccessor) {
var _el = $(element);
var _options = $.extend(valueAccessor(), {});
_el.slider({
min: !isNaN(parseFloat(_options.start())) ? parseFloat(_options.start()) : 0,
max: !isNaN(parseFloat(_options.end())) ? parseFloat(_options.end()) : 10,
step: !isNaN(parseFloat(_options.gap())) ? parseFloat(_options.gap()) : 1,
handle: _options.handle ? _options.handle : 'triangle',
start: parseFloat(_options.min()),
end: parseFloat(_options.max()),
tooltip_split: true,
tooltip: 'always'
});
_el.on("slide", function (e) {
_options.start(e.min);
_options.end(e.max);
_options.min(e.start);
_options.max(e.end);
_options.gap(e.step);
});
_el.on("slideStop", function (e) {
viewModel.search();
});
},
update: function (element, valueAccessor) {
var _options = $.extend(valueAccessor(), {});
}
}
ko.bindingHandlers.daterangepicker = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var DATE_FORMAT = "YYYY-MM-DD";
var TIME_FORMAT = "HH:mm:ss";
var DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
var _el = $(element);
var _options = $.extend(valueAccessor(), {});
var _intervalOptions = [
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
'',
''
];
function enableOptions() {
var _opts = [];
var _tmp = $("
").html(_intervalOptions.join(""))
$.each(arguments, function (cnt, item) {
if (_tmp.find("option[value='+" + item + "']").length > 0) {
_opts.push('');
}
});
return _opts;
}
function renderOptions(opts) {
var _html = "";
for (var i = 0; i < opts.length; i++) {
_html += opts[i];
}
_html += '';
return _html;
}
var _tmpl = $('