Skip to content

Instantly share code, notes, and snippets.

@heavysixer
Last active August 29, 2015 14:01
Show Gist options
  • Save heavysixer/ff456103771c13f175b5 to your computer and use it in GitHub Desktop.
Save heavysixer/ff456103771c13f175b5 to your computer and use it in GitHub Desktop.
Dismissible Panel Directive
/*
* This directive creates a dismissible message, which can either reappear each
* page refresh or appear only until it is dismissed by setting a cookie.
*
*##### Examples
*
* <div dismissible(data-title='NOTE' data-persist-dismiss >
* click the button to dismiss me!
* </div>
*
* @param {String} data-persist-dismiss - Optional parameter, which when set causes the panel to not reappear once dismissed
* @param {String} data-title - Optional title
*/
angular.module('humansized.com.directives')
.directive('dismissible', function($templateCache, $cookies, $sce) {
'use strict';
$templateCache.put('templates/__dismissible-container.html',
'<div class="panel panel-info panel-dismissable">' +
'<div class="panel-heading clearfix">' +
'<h3 class="panel-title pull-left">{{title}}</h3>' +
'<button type="button" class="close pull-right" ng-click="dismissPanel()" aria-hidden="true">&times;</button>' +
'</div>' +
'<div class="panel-body">' +
'<div ng-bind-html="message"></div>' +
'</div>' +
'<div class="panel-footer clearfix">' +
'<div class="pull-right">' +
'<div ng-click="dismissPanel()" class="btn btn-sm btn-primary">Dismiss</button>' +
'</div>' +
'</div>' +
'</div>');
return {
templateUrl: 'templates/__dismissible-container.html',
restrict: 'EAC',
scope:true,
transclude: true,
controller: function($scope, $element, $attrs, $transclude) {
var id = 'dismissible_' + $element.attr('id');
$scope.dismissPanel = function(){
if($attrs.persistDismiss){
$cookies[id] = (new Date()).getTime();
}
$element.hide();
};
if($cookies[id]){
$element.hide();
} else {
$transclude(function(clone) {
var html = '';
for (var i=0; i<clone.length; i++) {
html += clone[i].outerHTML||'';
}
$scope.message = $sce.trustAsHtml(html);
});
}
},
link: function($scope, element, attrs) {
$scope.title = attrs.title || '';
$scope.container = element;
}
};
});
@skeep
Copy link

skeep commented May 16, 2014

will be great if u change the extension to .js

@lprsd
Copy link

lprsd commented May 16, 2014

Even better, if you can put it on http://plnkr.co/ with a sample. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment