angular-skycons.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var angularSkycons = angular.module( 'angular-skycons', [] );
  2. angularSkycons.directive( 'skycon', function () {
  3. return {
  4. restrict: 'E',
  5. replace: true,
  6. scope: {
  7. icon: "=",
  8. size: "="
  9. },
  10. link: function ( scope, element, attrs ) {
  11. // make a canvas for our icon
  12. var canvas = document.createElement( 'canvas' );
  13. // set the CSS class from attribute
  14. if ( !attrs.class ) {
  15. canvas.className = "";
  16. } else {
  17. canvas.className = attrs.class;
  18. }
  19. // set default color if "color" attribute not present
  20. var config = {};
  21. if ( !attrs.color ) {
  22. config.color = "black";
  23. } else {
  24. config.color = attrs.color;
  25. }
  26. var skycons = new Skycons( config );
  27. // watch the size property from the controller
  28. scope.$watch( "size", function ( newVal, oldVal ) {
  29. if ( newVal ) {
  30. canvas.height = newVal;
  31. canvas.width = newVal;
  32. } else {
  33. canvas.height = scope.size || 64;
  34. canvas.width = scope.size || 64;
  35. }
  36. }, true );
  37. // watch the icon property from the controller
  38. scope.$watch( "icon", function () {
  39. skycons.set( canvas, scope.icon );
  40. }, true );
  41. skycons.play();
  42. if ( element[0].nodeType === 8 ) {
  43. element.replaceWith( canvas );
  44. } else {
  45. element[0].appendChild( canvas );
  46. }
  47. scope.$on('$destroy', function () {
  48. skycons.remove(canvas);
  49. if (skycons.list.length === 0) {
  50. skycons.pause(canvas);
  51. }
  52. });
  53. }
  54. };
  55. } );