Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
chore(buttons): refactor defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcrea committed Jan 7, 2014
1 parent 3db8f83 commit 3460b6f
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ var jqLite = angular.element;

angular.module('mgcrea.ngStrap.button', [])

.constant('buttonsConfig', {
activeClass:'active',
toggleEvent:'click'
.provider('$button', function() {

var defaults = this.defaults = {
activeClass:'active',
toggleEvent:'click'
};

this.$get = function() {
return {defaults: defaults};
};

})

.directive('bsCheckboxGroup', function(buttonsConfig) {
.directive('bsCheckboxGroup', function() {

return {
restrict: 'A',
Expand All @@ -30,18 +38,19 @@ angular.module('mgcrea.ngStrap.button', [])

})

.directive('bsCheckbox', function(buttonsConfig) {
.directive('bsCheckbox', function($button) {

var defaults = $button.defaults;
var isDefined = angular.isDefined;
var activeClass = buttonsConfig.activeClass || 'active';
var toggleEvent = buttonsConfig.toggleEvent || 'click';
var constantValueRegExp = /^(true|false|\d+)$/;

return {
restrict: 'A',
require: 'ngModel',
link: function postLink(scope, element, attr, controller) {

var options = defaults;

// Support label > input[type="checkbox"]
var isInput = element[0].nodeName === 'INPUT';
var activeElement = isInput ? element.parent() : element;
Expand Down Expand Up @@ -75,11 +84,11 @@ angular.module('mgcrea.ngStrap.button', [])
if(isInput) {
element[0].checked = isActive;
}
activeElement.toggleClass(activeClass, isActive);
activeElement.toggleClass(options.activeClass, isActive);
};

// view -> model
element.bind(toggleEvent, function() {
element.bind(options.toggleEvent, function() {
scope.$apply(function () {
// console.warn('!click', element.attr('ng-model'), 'controller.$viewValue', typeof controller.$viewValue, controller.$viewValue, 'controller.$modelValue', typeof controller.$modelValue, controller.$modelValue);
if(!isInput) {
Expand All @@ -97,7 +106,7 @@ angular.module('mgcrea.ngStrap.button', [])

})

.directive('bsRadioGroup', function(buttonsConfig) {
.directive('bsRadioGroup', function() {

return {
restrict: 'A',
Expand All @@ -116,18 +125,19 @@ angular.module('mgcrea.ngStrap.button', [])

})

.directive('bsRadio', function(buttonsConfig) {
.directive('bsRadio', function($button) {

var defaults = $button.defaults;
var isDefined = angular.isDefined;
var activeClass = buttonsConfig.activeClass || 'active';
var toggleEvent = buttonsConfig.toggleEvent || 'click';
var constantValueRegExp = /^(true|false|\d+)$/;

return {
restrict: 'A',
require: 'ngModel',
link: function postLink(scope, element, attr, controller) {

var options = defaults;

// Support `label > input[type="radio"]` markup
var isInput = element[0].nodeName === 'INPUT';
var activeElement = isInput ? element.parent() : element;
Expand All @@ -141,11 +151,11 @@ angular.module('mgcrea.ngStrap.button', [])
if(isInput) {
element[0].checked = isActive;
}
activeElement.toggleClass(activeClass, isActive);
activeElement.toggleClass(options.activeClass, isActive);
};

// view -> model
element.bind(toggleEvent, function() {
element.bind(options.toggleEvent, function() {
scope.$apply(function () {
// console.warn('!click', element.attr('value'), 'controller.$viewValue', typeof controller.$viewValue, controller.$viewValue, 'controller.$modelValue', typeof controller.$modelValue, controller.$modelValue);
controller.$setViewValue(value);
Expand Down

0 comments on commit 3460b6f

Please sign in to comment.