UNPKG

@palmabit/angular-cookie-law

Version:

Angular module that provides a banner to inform users about cookie law

110 lines (97 loc) 4.33 kB
angular.module('angular-cookie-law') .directive('cookieLawBanner', ['$compile', 'CookieLawService', function ($compile, CookieLawService) { return { restrict: 'EA', replace: true, scope: { position: '@', message: '@', acceptText: '@', declineText: '@', policyText: '@', policyURL: '@', acceptButton: '@', declineButton: '@', policyButton: '@', policyBlank: '@', expireDays: '@', element: '@', }, link: function (scope, element, attr) { var template, options, expireDate; scope.$watchGroup([ 'position', 'message', 'acceptText', 'declineText', 'policyText', 'policyURL', 'acceptButton', 'declineButton', 'policyButton', 'policyBlank', 'expireDays', 'element', ], function() { if (CookieLawService.isEnabled()) { return; } options = { position: attr.position === 'bottom' ? 'bottom' : 'top', //Position of the banner. (Default: 'top') message: attr.message || 'We use cookies to track usage and preferences.', //Message displayed on bar acceptButton: attr.acceptButton === 'false' ? false : true, //Set to true to show accept/enable button acceptText: attr.acceptText || 'I Understand', //Text on accept/enable button declineButton: attr.declineButton || false, //Set to true to show decline/disable button declineText: attr.declineText || 'Disable Cookies', //Text on decline/disable button policyButton: attr.policyButton || false, //Set to true to show Privacy Policy button policyText: attr.policyText || 'Privacy Policy', //Text on Privacy Policy button policyURL: attr.policyUrl || '/privacy-policy/', //URL of Privacy Policy policyBlank: attr.policyBlank && attr.policyBlank === 'true' ? 'target="_blank"' : '', expireDays: attr.expireDays || 365, //Number of days for cookieBar cookie to be stored for element: attr.element || 'body' //Element to append/prepend cookieBar to. Remember "." for class or "#" for id. }; //Sets expiration date for cookie expireDate = new Date(); expireDate.setTime(expireDate.getTime() + (options.expireDays * 24 * 60 * 60 * 1000)); expireDate = expireDate.toGMTString(); if (options.acceptButton) { acceptButton = '<a href="" class="cl-accept" ng-click="accept()">' + options.acceptText + '</a>'; } if (options.declineButton) { declineButton = ' <a href="" class="cl-disable" ng-click="decline()">' + options.declineText + '</a>'; } if (options.policyButton) { policyButton = ' <a href="' + options.policyURL + '" class="cl-policy" ' + options.policyBlank + '>' + options.policyText + '</a>'; } template = '<div class="cl-banner ' + options.position + '"><p>' + options.message + '<br>' + acceptButton + declineButton + policyButton + '</p></div>'; element.html(template); $compile(element.contents())(scope); scope.accept = function() { CookieLawService.accept(expireDate); scope.onAccept(); element.remove(); scope.onDismiss(); }; scope.decline = function() { CookieLawService.decline(expireDate); scope.onDecline(); element.remove(); scope.onDismiss(); }; }); }, controller: ['$rootScope', '$scope', function ($rootScope, scope) { scope.onAccept = function () { $rootScope.$broadcast('cookieLaw.accept'); }; scope.onDismiss = function () { $rootScope.$broadcast('cookieLaw.dismiss'); }; scope.onDecline = function () { $rootScope.$broadcast('cookieLaw.decline'); }; }] } }]);