(function ($) {
$(function () {
var dateFields = $(".snr_date");
dateFields.each(function (i) {
var ele = $(this);
ele.datepicker({
changeMonth: true,
numberOfMonths: 1,
showOn : "both",
buttonImage: ele.attr("data-buttonImage"),
buttonImageOnly: true,
//showCurrentAtPos: 1,
dateFormat: ele.attr("data-dateformat")
});
});
//Set up date ranges.
var dateRanges = $(".snr_date_range");
dateRanges.each(function (i) {
var fromBox = $(this).find("div.dr_from input.snr_date:first");
var toBox = $(this).find("div.dr_to input.snr_date:first");
fromBox.datepicker("option", "onSelect", function (selectedDate) {
toBox.datepicker("option", "minDate", selectedDate);
toBox.trigger("datepicker-select", selectedDate);
});
toBox.datepicker("option", "onSelect", function (selectedDate) {
fromBox.datepicker("option", "maxDate", selectedDate);
fromBox.trigger("datepicker-select", selectedDate);
});
});
var timeFields = $(".snr_time input");
timeFields.each(function (i) {
var ele = $(this);
var step = ele.attr("data-step") ? ele.attr("data-step") : 30;
ele.timepicker().keypress(function (e) {
//alert(e.which);
if ($.inArray(e.which, [8, 13, 0]) == -1) {
// var sel = ele.getSelectedText();
// var c = String.fromCharCode(e.which);
// var val = ele.val();
// var carat = ele.getCursorPosition();
// //Text highlighted?
// var newVal = val.slice(0, carat);
// newVal += c + val.slice(carat + sel.length);
}
}).click(function (e) {
var carat = ele.getCursorPosition();
//var sel = ele.getSelectedText();
var offset = Math.max($(this).val().indexOf(":"), 1);
if (carat < 5 && carat != offset) {
//
//alert(offset);
ele.selectText(carat, carat + 1);
}
else if (carat > 4) {
ele.selectText(offset + 3, 6);
}
});
ele.timepicker('option', 'step', step);
});
//Custom knockout template for date field
if (typeof ko !== "undefined") {
ko.bindingHandlers.date = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var dateField = $(".snr_date", element);
var value = valueAccessor();
dateField.change(function () {
var ele = $(this);
value(ele.datepicker("setDate", ele.val()).datepicker("getDate"));
});
dateField.datepicker("option", "onSelect", function (selectedDate) {
var ele = $(this);
//value(selectedDate);
//Seems to be a weird bug in some situations where this returns a date with the wrong timezone info.
//The following gets around this problem
value(ele.datepicker("getDate"));
});
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var dateField = $(".snr_date", element);
var valueUnwrapped = ko.unwrap(valueAccessor());
if (valueUnwrapped) {
dateField.val($.senior.formatDate(dateField.datepicker("option", "dateFormat"), valueUnwrapped));
}
}
};
ko.bindingHandlers.time = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
$(element).change(function () {
var value = valueAccessor();
value($(this).val());
});
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var valueUnwrapped = ko.unwrap(valueAccessor());
var timeField = $(".snr_time", element).children("input");
timeField.val(valueUnwrapped);
}
};
}
if (jQuery().chosen) {
var chosenDDLs = $(".ddl_chosen");
chosenDDLs.each(function () {
var stayOpen = $(this).attr("data-stayOpen") == "true";
$(this).chosen({
stay_open : stayOpen,
disable_search_threshold: 10,
allow_single_deselect: $(this).hasClass('ddl_chosen_single_deselect')
});
});
}
var $linkFields = $('.snr_link');
var targets = {
'_blank': 'Open in a new window',
'_self': 'Open in this window'
};
var emailLinkControls = [{
labelText: 'Email Address',
inputType: 'text',
field: 'Link'
},
{
labelText: 'Email Link Text',
inputType: 'text',
field: 'LinkText'
}];
var webLinkControls = [{
labelText: 'Link',
inputType: 'text',
field: 'Link'
},
{
labelText: 'Link Text',
inputType: 'text',
field: 'LinkText'
}];
$linkFields.each(function (index, element) {
var that = this;
var $inputField = $('input[type=text]', element);
var $linkDetails = $('')
var $editLink = $('Edit Link');
var linkType = $inputField.data('link-type') || '';
var controls = (linkType.toLowerCase() === 'web') ? webLinkControls.slice(0) :
emailLinkControls.slice(0);
if (linkType.toLowerCase() === 'web') {
controls.push({
labelText: 'Target',
inputType: 'select',
field: 'Target',
options: targets
});
}
// need to set the validation property on the link field
controls[0].validate = (linkType.toLowerCase() === 'web') ? 'url' : 'email';
$(that).settingsDialog({
dialogTitle: 'Link Settings',
dialogControls: controls
});
if ($inputField.val().length > 1) {
var lnkTxt = '';
var lnkLnk = '';
try {
var link = JSON.parse(($inputField.val().length < 1 ? "{}" : $inputField.val()))
lnkTxt = link.LinkText;
lnkLnk = link.Link;
} catch (err) {
lnkTxt = $inputField.val();
lnkLnk = $inputField.val();
}
$linkDetails.html('Title: ' + lnkTxt + '
URL: ' + lnkLnk);
$inputField.after($editLink).after("