-
-
Save leovca/7162144 to your computer and use it in GitHub Desktop.
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
// Directive for autoNumeric.js | |
// Require AngularJS, jQuery and autoNumeric.js | |
angular.module('crunch.ui.numeric', []).directive('crNumeric', [function () { | |
'use strict'; | |
// Declare a empty options object | |
var options = {}; | |
return { | |
// Require ng-model in the element attribute for watching changes. | |
require: '?ngModel', | |
// This directive only works when used in element's attribute (e.g: cr-numeric) | |
restrict: 'A', | |
compile: function (tElm, tAttrs) { | |
var isTextInput = tElm.is('input:text'); | |
return function (scope, elm, attrs, controller) { | |
// Get instance-specific options. | |
var opts = angular.extend({}, options, scope.$eval(attrs.crNumeric)); | |
// Helper method to update autoNumeric with new value. | |
var updateElement = function (element, newVal) { | |
// Only set value if value is numeric | |
if ($.isNumeric(newVal)) | |
element.autoNumeric('set', newVal); | |
}; | |
// Initialize element as autoNumeric with options. | |
elm.autoNumeric(opts); | |
// if element has controller, wire it (only for <input type="text" />) | |
if (controller && isTextInput) { | |
// watch for external changes to model and re-render element | |
scope.$watch(tAttrs.ngModel, function (current, old) { | |
controller.$render(); | |
}); | |
// render element as autoNumeric | |
controller.$render = function () { | |
updateElement(elm, controller.$viewValue); | |
} | |
// Detect changes on element and update model. | |
elm.on('change', function (e) { | |
scope.$apply(function () { | |
controller.$setViewValue(elm.autoNumeric('get')); | |
}); | |
}); | |
elm.on('keypress', function (e) { | |
scope.$apply(function () { | |
controller.$setViewValue(elm.autoNumeric('get')); | |
}); | |
}); | |
} | |
else { | |
// Listen for changes to value changes and re-render element. | |
// Useful when binding to a readonly input field or html elements. | |
attrs.$observe('value', function (val) { | |
updateElement(elm, val); | |
}); | |
} | |
} | |
} // compile | |
} // return | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment