-
-
Save sajal/3342925 to your computer and use it in GitHub Desktop.
Coffeescript generated javascript for angular-ui ui-date directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
jQuery UI Datepicker plugin wrapper | |
@param [ui-date] {object} Options to pass to $.fn.datepicker() merged onto ui.config | |
*/ | |
angular.module('ui.directives').directive('uiDate', [ | |
'ui.config', function(uiConfig) { | |
var options; | |
options = {}; | |
if (uiConfig.date != null) { | |
angular.extend(options, uiConfig.date); | |
} | |
return { | |
require: '?ngModel', | |
link: function(scope, element, attrs, controller) { | |
var opts, updateModel, usersOnSelectHandler; | |
opts = angular.extend({}, options, scope.$eval(attrs.uiDate)); | |
/* If we have a controller (i.e. ngModelController) then wire it up | |
*/ | |
if (controller != null) { | |
updateModel = function(value, picker) { | |
return scope.$apply(function() { | |
return controller.$setViewValue(element.datepicker("getDate")); | |
}); | |
}; | |
if (opts.onSelect != null) { | |
/* Caller has specified onSelect to call this as well as updating the model | |
*/ | |
usersOnSelectHandler = opts.onSelect; | |
opts.onSelect = function(value, picker) { | |
updateModel(value); | |
return usersOnSelectHandler(value, picker); | |
}; | |
} else { | |
/* No onSelect already specified so just update the model | |
*/ | |
opts.onSelect = updateModel; | |
} | |
/* Update the date picker when the model changes | |
*/ | |
controller.$render = function() { | |
var date; | |
date = controller.$viewValue; | |
if (!(date instanceof Date)) { | |
date = new Date(date); | |
} | |
return element.datepicker("setDate", date); | |
}; | |
} | |
/* Watch uiDate attr and set datepicker options | |
*/ | |
scope.$watch(attrs.uiDate, function(options){ | |
Object.keys(options).forEach(function(o){ | |
element.datepicker( "option", o, options[o] ) | |
}) | |
}, true); | |
/* Create the datepicker widget | |
*/ | |
return element.datepicker(opts); | |
} | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment