'use strict';
/**
 * Implements directive for spinner jqueryUI component. 
 * Spinner component: http://jqueryui.com/spinner/
 */
angular.module('qusion.ui.spinner', [])
    .directive('qnSpinner', [
        function() {
            return {
                require: 'ngModel',
                link: function (scope, element, attr, ngModel) {
                    var dp = attr.qnSpinner;
                    var options = scope.$eval(dp);
                    var model = attr.ngModel;

                    options = options || { };

                    element.spinner(angular.extend(options, {
                        change: function(event, ui) {
                            ngModel.$setViewValue(element.val());
                            scope.$apply();
                        },
                        spin: function (event, ui) {
                            ngModel.$setViewValue(element.val());
                            scope.$apply();
                        }
                    }));

                    scope.$watch(model, function(value) {
                        element.spinner("value", value);
                    });
                }
            };
        }
    ]);