Encapsulate directive functions in anonymous functions - avoid the global namespace

This commit is contained in:
Kevin Gisi
2015-06-21 23:52:08 -04:00
parent b4c4b891a1
commit 78b424e247
10 changed files with 278 additions and 258 deletions

View File

@@ -1,15 +1,16 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('focusMe', focusMe); .directive('focusMe', focusMe);
focusMe.$inject = [ focusMe.$inject = [
'$timeout', '$timeout',
'$parse' '$parse'
]; ];
function focusMe($timeout, $parse) { function focusMe($timeout, $parse) {
return { return {
link: function(scope, element, attrs) { link: function(scope, element, attrs) {
var model = $parse(attrs.focusMe); var model = $parse(attrs.focusMe);
@@ -20,4 +21,5 @@ function focusMe($timeout, $parse) {
}); });
} }
} }
} }
}());

View File

@@ -1,15 +1,16 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('fromNow', fromNow); .directive('fromNow', fromNow);
fromNow.$inject = [ fromNow.$inject = [
'$interval', '$interval',
'$timeout' '$timeout'
]; ];
function fromNow($interval, $timeout) { function fromNow($interval, $timeout) {
return function(scope, element, attr){ return function(scope, element, attr){
var interval, timeout; var interval, timeout;
@@ -41,4 +42,5 @@ function fromNow($interval, $timeout) {
if(timeout) $timeout.cancel(timeout); if(timeout) $timeout.cancel(timeout);
}); });
} }
} }
}());

View File

@@ -1,15 +1,16 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('habitrpgTasks', habitrpgTasks); .directive('habitrpgTasks', habitrpgTasks);
habitrpgTasks.$inject = [ habitrpgTasks.$inject = [
'$rootScope', '$rootScope',
'User' 'User'
]; ];
function habitrpgTasks($rootScope, User) { function habitrpgTasks($rootScope, User) {
return { return {
restrict: 'EA', restrict: 'EA',
templateUrl: 'templates/habitrpg-tasks.html', templateUrl: 'templates/habitrpg-tasks.html',
@@ -58,4 +59,5 @@ function habitrpgTasks($rootScope, User) {
} }
} }
} }
}());

View File

@@ -1,14 +1,15 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('hrpgSortChecklist', hrpgSortChecklist); .directive('hrpgSortChecklist', hrpgSortChecklist);
hrpgSortChecklist.$inject = [ hrpgSortChecklist.$inject = [
'User' 'User'
]; ];
function hrpgSortChecklist(User) { function hrpgSortChecklist(User) {
return function($scope, element, attrs, ngModel) { return function($scope, element, attrs, ngModel) {
$(element).sortable({ $(element).sortable({
axis: "y", axis: "y",
@@ -27,4 +28,5 @@ function hrpgSortChecklist(User) {
} }
}); });
} }
} }
}());

View File

@@ -1,14 +1,15 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('hrpgSortTags', hrpgSortTags); .directive('hrpgSortTags', hrpgSortTags);
hrpgSortTags.$inject = [ hrpgSortTags.$inject = [
'User' 'User'
]; ];
function hrpgSortTags(User) { function hrpgSortTags(User) {
return function($scope, element, attrs, ngModel) { return function($scope, element, attrs, ngModel) {
$(element).sortable({ $(element).sortable({
start: function (event, ui) { start: function (event, ui) {
@@ -24,4 +25,5 @@ function hrpgSortTags(User) {
} }
}); });
} }
} }
}());

View File

@@ -1,14 +1,15 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('hrpgSortTasks', hrpgSortTasks); .directive('hrpgSortTasks', hrpgSortTasks);
hrpgSortTasks.$inject = [ hrpgSortTasks.$inject = [
'User' 'User'
]; ];
function hrpgSortTasks(User) { function hrpgSortTasks(User) {
return function($scope, element, attrs, ngModel) { return function($scope, element, attrs, ngModel) {
$(element).sortable({ $(element).sortable({
axis: "y", axis: "y",
@@ -29,4 +30,5 @@ function hrpgSortTasks(User) {
} }
}); });
} }
} }
}());

View File

@@ -1,15 +1,16 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('popoverHtmlPopup', popoverHtmlPopup) .directive('popoverHtmlPopup', popoverHtmlPopup)
.run(loadPopupTemplate); .run(loadPopupTemplate);
popoverHtmlPopup.$inject = [ popoverHtmlPopup.$inject = [
'$sce' '$sce'
]; ];
function popoverHtmlPopup($sce) { function popoverHtmlPopup($sce) {
return { return {
restrict: 'EA', restrict: 'EA',
replace: true, replace: true,
@@ -21,18 +22,18 @@ function popoverHtmlPopup($sce) {
}, },
templateUrl: 'template/popover/popover-html.html' templateUrl: 'template/popover/popover-html.html'
}; };
} }
/* /*
* TODO: Review whether it's appropriate to be seeding this into the * TODO: Review whether it's appropriate to be seeding this into the
* templateCache like this. Feel like this might be an antipattern? * templateCache like this. Feel like this might be an antipattern?
*/ */
loadPopupTemplate.$inject = [ loadPopupTemplate.$inject = [
'$templateCache' '$templateCache'
]; ];
function loadPopupTemplate($templateCache) { function loadPopupTemplate($templateCache) {
$templateCache.put("template/popover/popover-html.html", $templateCache.put("template/popover/popover-html.html",
"<div class=\"popover {{placement}}\" ng-class=\"{ in: isOpen(), fade: animation() }\">\n" + "<div class=\"popover {{placement}}\" ng-class=\"{ in: isOpen(), fade: animation() }\">\n" +
" <div class=\"arrow\"></div>\n" + " <div class=\"arrow\"></div>\n" +
@@ -42,4 +43,5 @@ function loadPopupTemplate($templateCache) {
" <div class=\"popover-content\" ng-bind-html=\"unsafeContent\" style=\"word-wrap: break-word\"> </div>\n" + " <div class=\"popover-content\" ng-bind-html=\"unsafeContent\" style=\"word-wrap: break-word\"> </div>\n" +
" </div>\n" + " </div>\n" +
"</div>\n"); "</div>\n");
} }
}());

View File

@@ -1,17 +1,19 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('popoverHtml', popoverHtml); .directive('popoverHtml', popoverHtml);
popoverHtml.$inject = [ popoverHtml.$inject = [
'$compile', '$compile',
'$timeout', '$timeout',
'$parse', '$parse',
'$window', '$window',
'$tooltip' '$tooltip'
]; ];
function popoverHtml($compile, $timeout, $parse, $window, $tooltip) { function popoverHtml($compile, $timeout, $parse, $window, $tooltip) {
return $tooltip('popoverHtml', 'popover', 'click'); return $tooltip('popoverHtml', 'popover', 'click');
} }
}());

View File

@@ -1,17 +1,18 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('taskFocus', taskFocus); .directive('taskFocus', taskFocus);
taskFocus.$inject = ['$timeout']; taskFocus.$inject = ['$timeout'];
/** /**
* Directive that places focus on the element it is applied to when the * Directive that places focus on the element it is applied to when the
* expression it binds to evaluates to true. * expression it binds to evaluates to true.
*/ */
function taskFocus($timeout) { function taskFocus($timeout) {
return function(scope, elem, attrs) { return function(scope, elem, attrs) {
scope.$watch(attrs.taskFocus, function(newVal) { scope.$watch(attrs.taskFocus, function(newVal) {
if (newVal) { if (newVal) {
@@ -21,4 +22,5 @@ function taskFocus($timeout) {
} }
}); });
} }
} }
}());

View File

@@ -1,10 +1,11 @@
'use strict'; 'use strict';
angular (function(){
angular
.module('habitrpg') .module('habitrpg')
.directive('whenScrolled', whenScrolled); .directive('whenScrolled', whenScrolled);
function whenScrolled() { function whenScrolled() {
return function(scope, elm, attr) { return function(scope, elm, attr) {
var raw = elm[0]; var raw = elm[0];
@@ -14,4 +15,5 @@ function whenScrolled() {
} }
}); });
}; };
} }
}());