Skip to content

Instantly share code, notes, and snippets.

@trueqbit
Created April 1, 2017 08:50
Show Gist options
  • Save trueqbit/70e0536cc02b8d59497fc3cb4b1f4755 to your computer and use it in GitHub Desktop.
Save trueqbit/70e0536cc02b8d59497fc3cb4b1f4755 to your computer and use it in GitHub Desktop.
type/value agnostic jsviews datepicker tag control (issue 360)
(function($) {
'use strict';
$.views.tags({
// workaround https://github.com/BorisMoore/jsviews/issues/374
// concerning https://github.com/BorisMoore/jsviews/issues/360#issuecomment-272936627
// delegate dealing with type of values to converters
datepicker_360_374: {
baseTag: "widget",
widgetName: "datepicker",
linkedElement: "*",
elem: "input",
setSize: true,
options: function() {
var tag = this;
return {
onSelect: function(dateText, dp) {
// if convertBack is present then we presume it can convert
// a date object to the internal representation.
// otherwise we update the bound variable with the date text
var updateValue = tag.tagCtx.props.convertBack ?
$.datepicker._getDate(dp) :
dateText;
tag.update(updateValue);
}
};
},
onBind: function(tagCtx) {
var tag = this;
tag.baseApply(arguments);
if (tag.mainElem[0].tagName !== "INPUT") {
// This datepicker is not using an input (e.g. using a div) - so set to inline-
tag.mainElem.css("display", "inline-block");
} else {
tag.tagCtx.props.trigger = false;
}
},
//https://github.com/BorisMoore/jsviews/issues/360#issuecomment-272936627
// onAfterLink: function(tagCtx) {
// var tag = this,
// value = tag.bndArgs()[0];
// tag.baseApply(arguments);
// tag._datefrmt = $('input', tag.mainElem).datepicker("option", "dateFormat");
// var formatted = $.datepicker.formatDate(tag._datefrmt, new Date(tag.bndArgs()[0]));
// tag.setValue(formatted);
// },
setValue: function(value) {
var tag = this;
if (value !== undefined && value !== tag.value) {
tag.value = value;
tag.linkedElem.datepicker("setDate", value);
// workaround https://github.com/BorisMoore/jsviews/issues/374
tag.linkCtx._val = value;
}
},
getValue: function() {
return this.value;
}
},
});
})(this.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment