aside.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.aside', [ 'mgcrea.ngStrap.modal' ]).provider('$aside', function() {
  10. var defaults = this.defaults = {
  11. animation: 'am-fade-and-slide-right',
  12. prefixClass: 'aside',
  13. prefixEvent: 'aside',
  14. placement: 'right',
  15. templateUrl: 'aside/aside.tpl.html',
  16. contentTemplate: false,
  17. container: false,
  18. element: null,
  19. backdrop: true,
  20. keyboard: true,
  21. html: false,
  22. show: true
  23. };
  24. this.$get = [ '$modal', function($modal) {
  25. function AsideFactory(config) {
  26. var $aside = {};
  27. var options = angular.extend({}, defaults, config);
  28. $aside = $modal(options);
  29. return $aside;
  30. }
  31. return AsideFactory;
  32. } ];
  33. }).directive('bsAside', [ '$window', '$sce', '$aside', function($window, $sce, $aside) {
  34. return {
  35. restrict: 'EAC',
  36. scope: true,
  37. link: function postLink(scope, element, attr, transclusion) {
  38. var options = {
  39. scope: scope,
  40. element: element,
  41. show: false
  42. };
  43. angular.forEach([ 'template', 'templateUrl', 'controller', 'controllerAs', 'contentTemplate', 'placement', 'backdrop', 'keyboard', 'html', 'container', 'animation' ], function(key) {
  44. if (angular.isDefined(attr[key])) options[key] = attr[key];
  45. });
  46. var falseValueRegExp = /^(false|0|)$/i;
  47. angular.forEach([ 'backdrop', 'keyboard', 'html', 'container' ], function(key) {
  48. if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) options[key] = false;
  49. });
  50. angular.forEach([ 'onBeforeShow', 'onShow', 'onBeforeHide', 'onHide' ], function(key) {
  51. var bsKey = 'bs' + key.charAt(0).toUpperCase() + key.slice(1);
  52. if (angular.isDefined(attr[bsKey])) {
  53. options[key] = scope.$eval(attr[bsKey]);
  54. }
  55. });
  56. angular.forEach([ 'title', 'content' ], function(key) {
  57. if (attr[key]) {
  58. attr.$observe(key, function(newValue, oldValue) {
  59. scope[key] = $sce.trustAsHtml(newValue);
  60. });
  61. }
  62. });
  63. if (attr.bsAside) {
  64. scope.$watch(attr.bsAside, function(newValue, oldValue) {
  65. if (angular.isObject(newValue)) {
  66. angular.extend(scope, newValue);
  67. } else {
  68. scope.content = newValue;
  69. }
  70. }, true);
  71. }
  72. var aside = $aside(options);
  73. element.on(attr.trigger || 'click', aside.toggle);
  74. scope.$on('$destroy', function() {
  75. if (aside) aside.destroy();
  76. options = null;
  77. aside = null;
  78. });
  79. }
  80. };
  81. } ]);