| Total Complexity | 53 |
| Complexity/F | 1.89 |
| Lines of Code | 243 |
| Function Count | 28 |
| Duplicated Lines | 70 |
| Ratio | 28.81 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like js/directive.range.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | |||
| 2 | app.directive('range', ['$rootScope', |
||
| 3 | '$timeout', |
||
| 4 | 'Core', |
||
| 5 | 'Security', |
||
| 6 | 'MessageService', function($rootScope, $timeout, Core, Security, MessageService) { |
||
| 7 | function RangeConstructor($scope, $element, $attrs) { |
||
| 8 | var constructor = this; |
||
|
|
|||
| 9 | var $ctrl = $scope.rangeCtrl; |
||
| 10 | var tagName = $element[0].tagName.toLowerCase(); |
||
| 11 | |||
| 12 | View Code Duplication | var DirectiveProperties = (function () { |
|
| 13 | var multi; |
||
| 14 | |||
| 15 | function findMultiable(){ |
||
| 16 | var object = $attrs.multi; |
||
| 17 | if(typeof(object) != "undefined") |
||
| 18 | return true; |
||
| 19 | else |
||
| 20 | return false; |
||
| 21 | } |
||
| 22 | |||
| 23 | return { |
||
| 24 | getMultiable: function () { |
||
| 25 | if (!multi) { |
||
| 26 | multi = findMultiable(); |
||
| 27 | } |
||
| 28 | $scope.multi = multi; |
||
| 29 | return multi; |
||
| 30 | } |
||
| 31 | }; |
||
| 32 | })(); |
||
| 33 | |||
| 34 | function TryToCallInitDirective(){ |
||
| 35 | if(typeof $scope.InitDirective == "function"){ |
||
| 36 | $scope.InitDirective($scope, $element, $attrs, $ctrl); |
||
| 37 | }else{ |
||
| 38 | $scope.DefaultInitDirective(); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | $scope.DefaultInitDirective = function(){ |
||
| 42 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 43 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
| 44 | } |
||
| 45 | |||
| 46 | $scope.IsRange = function() |
||
| 47 | { |
||
| 48 | return true; |
||
| 49 | } |
||
| 50 | |||
| 51 | $scope.GetInitRange = function(){ |
||
| 52 | var range = {start: "ALL", end: ""}; |
||
| 53 | |||
| 54 | return range; |
||
| 55 | } |
||
| 56 | |||
| 57 | function InitializeRange() { |
||
| 58 | DirectiveProperties.getMultiable(); |
||
| 59 | |||
| 60 | var range = {start: "ALL", end: "", isAll: false}; |
||
| 61 | $ctrl.ngModel = jQuery.extend({}, range); |
||
| 62 | // $ctrl.ngModel.isAll = true; |
||
| 63 | } |
||
| 64 | $scope.Initialize = function(){ |
||
| 65 | $scope.InitScope(); |
||
| 66 | if(typeof $scope.EventListener == "function"){ |
||
| 67 | $scope.EventListener($scope, $element, $attrs, $ctrl); |
||
| 68 | }else{ |
||
| 69 | EventListener(); |
||
| 70 | } |
||
| 71 | TryToCallInitDirective(); |
||
| 72 | } |
||
| 73 | $scope.InitScope = function(){ |
||
| 74 | InitializeRange(); |
||
| 75 | } |
||
| 76 | |||
| 77 | function InitDirective(){ |
||
| 78 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 79 | console.log("scope.$id:"+$scope.$id+", may implement $scope.InitDirective() function in webapge"); |
||
| 80 | } |
||
| 81 | function EventListener(){ |
||
| 82 | if(Core.GetConfig().debugLog.DirectiveFlow) |
||
| 83 | console.log("scope.$id:"+$scope.$id+", may implement $scope.EventListener() function in webapge"); |
||
| 84 | } |
||
| 85 | |||
| 86 | // chagneRagne = [start | end] |
||
| 87 | View Code Duplication | function RangeStringChange(changeRange, ctrlModel){ |
|
| 88 | var isLocaleCompareSupport = localeCompareSupportsLocales(); |
||
| 89 | |||
| 90 | var strStart = ctrlModel.start; |
||
| 91 | var strEnd = ctrlModel.end; |
||
| 92 | var stringDifference = 0; |
||
| 93 | |||
| 94 | // if user change the start value |
||
| 95 | if(strStart != "ALL"){ |
||
| 96 | $ctrl.ngModel.isAll = false; |
||
| 97 | } |
||
| 98 | |||
| 99 | // string comparison, find the strStart position of strEnd |
||
| 100 | if(isLocaleCompareSupport){ |
||
| 101 | stringDifference = strStart.localeCompare(strEnd); |
||
| 102 | }else{ |
||
| 103 | if(strStart < strEnd) |
||
| 104 | stringDifference = -1; |
||
| 105 | if(strEnd < strStart) |
||
| 106 | stringDifference = 1; |
||
| 107 | if(strStart == strEnd) |
||
| 108 | stringDifference = 0; |
||
| 109 | } |
||
| 110 | |||
| 111 | if(changeRange == "start"){ |
||
| 112 | |||
| 113 | if(stringDifference > 0) |
||
| 114 | { |
||
| 115 | strEnd = strStart |
||
| 116 | } |
||
| 117 | if(stringDifference < 0) |
||
| 118 | { |
||
| 119 | |||
| 120 | } |
||
| 121 | }else if(changeRange == "end"){ |
||
| 122 | if(stringDifference > 0) |
||
| 123 | { |
||
| 124 | strStart = strEnd |
||
| 125 | } |
||
| 126 | if(stringDifference < 0) |
||
| 127 | { |
||
| 128 | if(strStart == "") |
||
| 129 | strStart = strEnd |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | ctrlModel.start = strStart; |
||
| 134 | ctrlModel.end = strEnd; |
||
| 135 | } |
||
| 136 | |||
| 137 | function localeCompareSupportsLocales() { |
||
| 138 | try { |
||
| 139 | 'foo'.localeCompare('bar', 'i'); |
||
| 140 | } catch (e) { |
||
| 141 | return e.name === 'RangeError'; |
||
| 142 | } |
||
| 143 | return false; |
||
| 144 | } |
||
| 145 | |||
| 146 | $scope.SetRange = function(rangeType, value){ |
||
| 147 | if(rangeType == "start"){ |
||
| 148 | $ctrl.ngModel.start = value; |
||
| 149 | }else if(rangeType == "end"){ |
||
| 150 | $ctrl.ngModel.end = value; |
||
| 151 | } |
||
| 152 | |||
| 153 | RangeStringChange(rangeType, $ctrl.ngModel) |
||
| 154 | } |
||
| 155 | |||
| 156 | $scope.GetRange = function(){ |
||
| 157 | var rangeList = []; |
||
| 158 | var range = rangeList[0] = {start: "", end: ""}; |
||
| 159 | |||
| 160 | range.start = $ctrl.ngModel.start; |
||
| 161 | range.end = $ctrl.ngModel.end; |
||
| 162 | |||
| 163 | return range; |
||
| 164 | } |
||
| 165 | $scope.IsLockEndControl = function(){ |
||
| 166 | var isLock = false; |
||
| 167 | var range = $scope.GetRange(); |
||
| 168 | if(range.start == "ALL"){ |
||
| 169 | isLock = true; |
||
| 170 | } |
||
| 171 | return isLock; |
||
| 172 | } |
||
| 173 | |||
| 174 | $scope.CheckAllRange = function(test){ |
||
| 175 | console.dir("CheckAllRange() function") |
||
| 176 | } |
||
| 177 | |||
| 178 | $scope.$watch( |
||
| 179 | function() { return $ctrl.ngModel.isAll; }, |
||
| 180 | function(newValue, oldValue) { |
||
| 181 | var isCheckAll = newValue; |
||
| 182 | |||
| 183 | if(isCheckAll){ |
||
| 184 | $ctrl.ngModel.start = "ALL" |
||
| 185 | $ctrl.ngModel.end = "" |
||
| 186 | |||
| 187 | var editBtn = $element.find('button[ng-click="OpenPageView()"]').last(); |
||
| 188 | editBtn.prop('disabled', true); |
||
| 189 | } |
||
| 190 | else{ |
||
| 191 | if($ctrl.ngModel.start == "ALL") |
||
| 192 | $ctrl.ngModel.start = "" |
||
| 193 | |||
| 194 | var editBtn = $element.find('button[ng-click="OpenPageView()"]').last(); |
||
| 195 | editBtn.prop('disabled', false); |
||
| 196 | } |
||
| 197 | }, |
||
| 198 | true |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | function templateFunction(tElement, tAttrs) { |
||
| 202 | var globalCriteria = $rootScope.globalCriteria; |
||
| 203 | |||
| 204 | var template = '' + |
||
| 205 | '<div class="custom-transclude"></div>'; |
||
| 206 | return template; |
||
| 207 | } |
||
| 208 | |||
| 209 | return { |
||
| 210 | require: ['ngModel'], |
||
| 211 | restrict: 'E', //'EA', //Default in 1.3+ |
||
| 212 | transclude: true, |
||
| 213 | scope: true, |
||
| 214 | |||
| 215 | controller: RangeConstructor, |
||
| 216 | controllerAs: 'rangeCtrl', |
||
| 217 | |||
| 218 | //If both bindToController and scope are defined and have object hashes, bindToController overrides scope. |
||
| 219 | bindToController: { |
||
| 220 | ngModel: '=', |
||
| 221 | }, |
||
| 222 | template: templateFunction, |
||
| 223 | compile: function compile(tElement, tAttrs, transclude) { |
||
| 224 | return { |
||
| 225 | pre: function preLink(scope, iElement, iAttrs, controller) { |
||
| 226 | //console.log("range preLink() compile"); |
||
| 227 | }, |
||
| 228 | post: function postLink(scope, iElement, iAttrs, controller) { |
||
| 229 | //console.log("range postLink() compile"); |
||
| 230 | |||
| 231 | transclude(scope, function (clone, scope) { |
||
| 232 | iElement.find('.custom-transclude').append(clone); |
||
| 233 | }) |
||
| 234 | |||
| 235 | scope.Initialize(); |
||
| 236 | iElement.ready(function() { |
||
| 237 | scope.rangeCtrl.ngModel.start = "ALL"; |
||
| 238 | scope.rangeCtrl.ngModel.isAll = true; |
||
| 239 | }) |
||
| 240 | } |
||
| 241 | } |
||
| 242 | }, |
||
| 243 | }; |
||
| 244 | }]); |