button.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * angular-strap
  3. * @version v2.3.9 - 2016-06-10
  4. * @link http://mgcrea.github.io/angular-strap
  5. * @author Olivier Louvignes <olivier@mg-crea.com> (https://github.com/mgcrea)
  6. * @license MIT License, http://www.opensource.org/licenses/MIT
  7. */
  8. 'use strict';
  9. angular.module('mgcrea.ngStrap.button', []).provider('$button', function() {
  10. var defaults = this.defaults = {
  11. activeClass: 'active',
  12. toggleEvent: 'click'
  13. };
  14. this.$get = function() {
  15. return {
  16. defaults: defaults
  17. };
  18. };
  19. }).directive('bsCheckboxGroup', function() {
  20. return {
  21. restrict: 'A',
  22. require: 'ngModel',
  23. compile: function postLink(element, attr) {
  24. element.attr('data-toggle', 'buttons');
  25. element.removeAttr('ng-model');
  26. var children = element[0].querySelectorAll('input[type="checkbox"]');
  27. angular.forEach(children, function(child) {
  28. var childEl = angular.element(child);
  29. childEl.attr('bs-checkbox', '');
  30. childEl.attr('ng-model', attr.ngModel + '.' + childEl.attr('value'));
  31. });
  32. }
  33. };
  34. }).directive('bsCheckbox', [ '$button', '$$rAF', function($button, $$rAF) {
  35. var defaults = $button.defaults;
  36. var constantValueRegExp = /^(true|false|\d+)$/;
  37. return {
  38. restrict: 'A',
  39. require: 'ngModel',
  40. link: function postLink(scope, element, attr, controller) {
  41. var options = defaults;
  42. var isInput = element[0].nodeName === 'INPUT';
  43. var activeElement = isInput ? element.parent() : element;
  44. var trueValue = angular.isDefined(attr.trueValue) ? attr.trueValue : true;
  45. if (constantValueRegExp.test(attr.trueValue)) {
  46. trueValue = scope.$eval(attr.trueValue);
  47. }
  48. var falseValue = angular.isDefined(attr.falseValue) ? attr.falseValue : false;
  49. if (constantValueRegExp.test(attr.falseValue)) {
  50. falseValue = scope.$eval(attr.falseValue);
  51. }
  52. var hasExoticValues = typeof trueValue !== 'boolean' || typeof falseValue !== 'boolean';
  53. if (hasExoticValues) {
  54. controller.$parsers.push(function(viewValue) {
  55. return viewValue ? trueValue : falseValue;
  56. });
  57. controller.$formatters.push(function(modelValue) {
  58. return angular.equals(modelValue, trueValue);
  59. });
  60. scope.$watch(attr.ngModel, function(newValue, oldValue) {
  61. controller.$render();
  62. });
  63. }
  64. controller.$render = function() {
  65. var isActive = angular.equals(controller.$modelValue, trueValue);
  66. $$rAF(function() {
  67. if (isInput) element[0].checked = isActive;
  68. activeElement.toggleClass(options.activeClass, isActive);
  69. });
  70. };
  71. element.bind(options.toggleEvent, function() {
  72. scope.$apply(function() {
  73. if (!isInput) {
  74. controller.$setViewValue(!activeElement.hasClass('active'));
  75. }
  76. if (!hasExoticValues) {
  77. controller.$render();
  78. }
  79. });
  80. });
  81. }
  82. };
  83. } ]).directive('bsRadioGroup', function() {
  84. return {
  85. restrict: 'A',
  86. require: 'ngModel',
  87. compile: function postLink(element, attr) {
  88. element.attr('data-toggle', 'buttons');
  89. element.removeAttr('ng-model');
  90. var children = element[0].querySelectorAll('input[type="radio"]');
  91. angular.forEach(children, function(child) {
  92. angular.element(child).attr('bs-radio', '');
  93. angular.element(child).attr('ng-model', attr.ngModel);
  94. });
  95. }
  96. };
  97. }).directive('bsRadio', [ '$button', '$$rAF', function($button, $$rAF) {
  98. var defaults = $button.defaults;
  99. var constantValueRegExp = /^(true|false|\d+)$/;
  100. return {
  101. restrict: 'A',
  102. require: 'ngModel',
  103. link: function postLink(scope, element, attr, controller) {
  104. var options = defaults;
  105. var isInput = element[0].nodeName === 'INPUT';
  106. var activeElement = isInput ? element.parent() : element;
  107. var value;
  108. attr.$observe('value', function(v) {
  109. if (typeof v !== 'boolean' && constantValueRegExp.test(v)) {
  110. value = scope.$eval(v);
  111. } else {
  112. value = v;
  113. }
  114. controller.$render();
  115. });
  116. controller.$render = function() {
  117. var isActive = angular.equals(controller.$modelValue, value);
  118. $$rAF(function() {
  119. if (isInput) element[0].checked = isActive;
  120. activeElement.toggleClass(options.activeClass, isActive);
  121. });
  122. };
  123. element.bind(options.toggleEvent, function() {
  124. scope.$apply(function() {
  125. controller.$setViewValue(value);
  126. controller.$render();
  127. });
  128. });
  129. }
  130. };
  131. } ]);