angular-ladda.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*! angular-ladda 0.2.2 */
  2. /**!
  3. * AngularJS Ladda directive
  4. * @author Chungsub Kim <subicura@subicura.com>
  5. */
  6. /* global Ladda */
  7. /* exported Ladda */
  8. (function (root, factory)
  9. {
  10. 'use strict';
  11. var Ladda;
  12. if (typeof exports === 'object') {
  13. // CommonJS module
  14. // Load ladda
  15. try { Ladda = require('ladda'); } catch (e) {}
  16. module.exports = factory(Ladda);
  17. } else if (typeof define === 'function' && define.amd) {
  18. // AMD. Register as an anonymous module.
  19. define(function (req)
  20. {
  21. // Load ladda as an optional dependency
  22. var id = 'ladda';
  23. try { Ladda = req(id); } catch (e) {}
  24. return factory(Ladda);
  25. });
  26. } else {
  27. root.Ladda = factory(root.Ladda);
  28. }
  29. }(this, function (Ladda){
  30. 'use strict';
  31. angular.module('angular-ladda', [])
  32. .provider('ladda', function () {
  33. var opts = {
  34. 'style': 'zoom-in'
  35. };
  36. return {
  37. setOption: function (newOpts) {
  38. angular.extend(opts, newOpts);
  39. },
  40. $get: function () {
  41. return opts;
  42. }
  43. };
  44. })
  45. .directive('ladda', ['ladda', function (laddaOption) {
  46. return {
  47. restrict: 'A',
  48. priority: -1,
  49. link: function (scope, element, attrs) {
  50. element.addClass('ladda-button');
  51. if(angular.isUndefined(element.attr('data-style'))) {
  52. element.attr('data-style', laddaOption.style || 'zoom-in');
  53. }
  54. // ladda breaks childNode's event property.
  55. // because ladda use innerHTML instead of append node
  56. if(!element[0].querySelector('.ladda-label')) {
  57. var labelWrapper = document.createElement('span');
  58. labelWrapper.className = 'ladda-label';
  59. angular.element(labelWrapper).append(element.contents());
  60. element.append(labelWrapper);
  61. }
  62. // create ladda button
  63. var ladda = Ladda.create( element[0] );
  64. // add watch!
  65. scope.$watch(attrs.ladda, function(loading) {
  66. if(loading || angular.isNumber(loading)) {
  67. if(!ladda.isLoading()) {
  68. ladda.start();
  69. }
  70. if(angular.isNumber(loading)) {
  71. ladda.setProgress(loading);
  72. }
  73. } else {
  74. ladda.stop();
  75. // When the button also have the ng-disabled directive it needs to be
  76. // re-evaluated since the disabled attribute is removed by the 'stop' method.
  77. if (attrs.ngDisabled) {
  78. element.attr('disabled', scope.$eval(attrs.ngDisabled));
  79. }
  80. }
  81. });
  82. }
  83. };
  84. }]);
  85. }));