omega_decoration.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. (function() {
  2. var orderForType;
  3. orderForType = {
  4. 'FixedProfile': -2000,
  5. 'PacProfile': -1000,
  6. 'VirtualProfile': 1000,
  7. 'SwitchProfile': 2000,
  8. 'RuleListProfile': 3000
  9. };
  10. angular.module('omegaDecoration', []).value('profileIcons', {
  11. 'DirectProfile': 'glyphicon-transfer',
  12. 'SystemProfile': 'glyphicon-off',
  13. 'AutoDetectProfile': 'glyphicon-file',
  14. 'FixedProfile': 'glyphicon-globe',
  15. 'PacProfile': 'glyphicon-file',
  16. 'VirtualProfile': 'glyphicon-question-sign',
  17. 'RuleListProfile': 'glyphicon-list',
  18. 'SwitchProfile': 'glyphicon-retweet'
  19. }).constant('profileOrder', function(a, b) {
  20. var diff;
  21. diff = (orderForType[a.profileType] | 0) - (orderForType[b.profileType] | 0);
  22. if (diff !== 0) {
  23. return diff;
  24. }
  25. if (a.name === b.name) {
  26. return 0;
  27. } else if (a.name < b.name) {
  28. return -1;
  29. } else {
  30. return 1;
  31. }
  32. }).constant('getVirtualTarget', function(profile, options) {
  33. if ((profile != null ? profile.profileType : void 0) === 'VirtualProfile') {
  34. return options != null ? options['+' + profile.defaultProfileName] : void 0;
  35. }
  36. }).directive('omegaProfileIcon', function(profileIcons, getVirtualTarget) {
  37. return {
  38. restrict: 'A',
  39. template: '<span ng-style="{color: color || getColor(profile)}"\n ng-class="{\'virtual-profile-icon\': isVirtual(profile)}"\n class="glyphicon {{icon || getIcon(profile)}}">\n</span>',
  40. scope: {
  41. 'profile': '=?omegaProfileIcon',
  42. 'icon': '=?icon',
  43. 'color': '=?color',
  44. 'options': '=options'
  45. },
  46. link: function(scope, element, attrs, ngModel) {
  47. scope.profileIcons = profileIcons;
  48. scope.isVirtual = function(profile) {
  49. return (profile != null ? profile.profileType : void 0) === 'VirtualProfile';
  50. };
  51. scope.getIcon = function(profile) {
  52. var type, _ref, _ref1;
  53. type = profile != null ? profile.profileType : void 0;
  54. type = (_ref = (_ref1 = getVirtualTarget(profile, scope.options)) != null ? _ref1.profileType : void 0) != null ? _ref : type;
  55. return profileIcons[type];
  56. };
  57. return scope.getColor = function(profile) {
  58. var color;
  59. color = void 0;
  60. while (profile) {
  61. color = profile.color;
  62. profile = getVirtualTarget(profile, scope.options);
  63. }
  64. return color;
  65. };
  66. }
  67. };
  68. }).directive('omegaProfileInline', function() {
  69. return {
  70. restrict: 'A',
  71. template: '<span omega-profile-icon="profile" options="options"></span>\n{{dispName ? dispName(profile) : profile.name}}',
  72. scope: {
  73. 'profile': '=omegaProfileInline',
  74. 'dispName': '=?dispName',
  75. 'options': '=options'
  76. }
  77. };
  78. }).directive('omegaHtml', function($compile) {
  79. return {
  80. restrict: 'A',
  81. link: function(scope, element, attrs, ngModel) {
  82. var getHtml, locals;
  83. locals = {
  84. $profile: function(profile, dispName, options) {
  85. if (profile == null) {
  86. profile = 'profile';
  87. }
  88. if (dispName == null) {
  89. dispName = 'dispNameFilter';
  90. }
  91. if (options == null) {
  92. options = 'options';
  93. }
  94. return "<span class=\"profile-inline\" omega-profile-inline=\"" + profile + "\"\n disp-name=\"" + dispName + "\" options=\"" + options + "\"></span>";
  95. }
  96. };
  97. getHtml = function() {
  98. return scope.$eval(attrs.omegaHtml, locals);
  99. };
  100. return scope.$watch(getHtml, function(html) {
  101. element.html(html);
  102. return $compile(element.contents())(scope);
  103. });
  104. }
  105. };
  106. }).directive('omegaProfileSelect', function($timeout, profileIcons) {
  107. return {
  108. restrict: 'A',
  109. templateUrl: 'partials/omega_profile_select.html',
  110. require: '?ngModel',
  111. scope: {
  112. 'profiles': '&omegaProfileSelect',
  113. 'defaultText': '@?defaultText',
  114. 'dispName': '=?dispName',
  115. 'options': '=options'
  116. },
  117. link: function(scope, element, attrs, ngModel) {
  118. var updateView;
  119. scope.profileIcons = profileIcons;
  120. scope.currentProfiles = [];
  121. scope.dispProfiles = void 0;
  122. updateView = function() {
  123. var profile, _i, _len, _ref, _results;
  124. scope.profileIcon = '';
  125. _ref = scope.currentProfiles;
  126. _results = [];
  127. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  128. profile = _ref[_i];
  129. if (profile.name === scope.profileName) {
  130. scope.selectedProfile = profile;
  131. scope.profileIcon = profileIcons[profile.profileType];
  132. break;
  133. } else {
  134. _results.push(void 0);
  135. }
  136. }
  137. return _results;
  138. };
  139. scope.$watch(scope.profiles, (function(profiles) {
  140. scope.currentProfiles = profiles || [];
  141. if (scope.dispProfiles != null) {
  142. scope.dispProfiles = currentProfiles;
  143. }
  144. return updateView();
  145. }), true);
  146. scope.toggled = function(open) {
  147. if (open && (scope.dispProfiles == null)) {
  148. scope.dispProfiles = scope.currentProfiles;
  149. return scope.toggled = void 0;
  150. }
  151. };
  152. if (ngModel) {
  153. ngModel.$render = function() {
  154. scope.profileName = ngModel.$viewValue;
  155. return updateView();
  156. };
  157. }
  158. scope.setProfileName = function(name) {
  159. if (ngModel) {
  160. ngModel.$setViewValue(name);
  161. return ngModel.$render();
  162. }
  163. };
  164. return scope.getName = function(profile) {
  165. if (profile) {
  166. return scope.dispName(profile) || profile.name;
  167. }
  168. };
  169. }
  170. };
  171. });
  172. }).call(this);