resourceEditor.directive.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. angular.module('kityminderEditor')
  2. .directive('resourceEditor', function () {
  3. return {
  4. restrict: 'E',
  5. templateUrl: 'ui/directive/resourceEditor/resourceEditor.html',
  6. scope: {
  7. minder: '='
  8. },
  9. replace: true,
  10. controller: function ($scope) {
  11. var minder = $scope.minder;
  12. var isInteracting = false;
  13. minder.on('interactchange', function () {
  14. var enabled = $scope.enabled = minder.queryCommandState('resource') != -1;
  15. var selected = enabled ? minder.queryCommandValue('resource') : [];
  16. var used = minder.getUsedResource().map(function (resourceName) {
  17. return {
  18. name: resourceName,
  19. selected: selected.indexOf(resourceName) > -1
  20. }
  21. });
  22. $scope.used = used;
  23. isInteracting = true;
  24. $scope.$apply();
  25. isInteracting = false;
  26. });
  27. $scope.$watch('used', function (used) {
  28. if (minder.queryCommandState('resource') != -1 && used) {
  29. var resource = used.filter(function (resource) {
  30. return resource.selected;
  31. }).map(function (resource) {
  32. return resource.name;
  33. });
  34. // 由于 interactchange 带来的改变则不用执行 resource 命令
  35. if (isInteracting) {
  36. return;
  37. }
  38. minder.execCommand('resource', resource);
  39. }
  40. }, true);
  41. $scope.resourceColor = function (resource) {
  42. return minder.getResourceColor(resource).toHEX();
  43. };
  44. $scope.addResource = function (resourceName) {
  45. var origin = minder.queryCommandValue('resource');
  46. if (!resourceName || !/\S/.test(resourceName)) return;
  47. if (origin.indexOf(resourceName) == -1) {
  48. $scope.used.push({
  49. name: resourceName,
  50. selected: true
  51. });
  52. }
  53. $scope.newResourceName = null;
  54. };
  55. }
  56. };
  57. })
  58. .directive('clickAnywhereButHere', ['$document', function ($document) {
  59. return {
  60. link: function(scope, element, attrs) {
  61. var onClick = function (event) {
  62. var isChild = $('#resource-dropdown').has(event.target).length > 0;
  63. var isSelf = $('#resource-dropdown') == event.target;
  64. var isInside = isChild || isSelf;
  65. if (!isInside) {
  66. scope.$apply(attrs.clickAnywhereButHere)
  67. }
  68. };
  69. scope.$watch(attrs.isActive, function(newValue, oldValue) {
  70. if (newValue !== oldValue && newValue == true) {
  71. $document.bind('click', onClick);
  72. }
  73. else if (newValue !== oldValue && newValue == false) {
  74. $document.unbind('click', onClick);
  75. }
  76. });
  77. }
  78. };
  79. }]);