Skip to content

Instantly share code, notes, and snippets.

@cburgdorf
Created May 8, 2013 12:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cburgdorf/5540239 to your computer and use it in GitHub Desktop.
Save cburgdorf/5540239 to your computer and use it in GitHub Desktop.
angular.module('sdk.directives.ccZippy')
.directive('ccZippy', function() {
var defaultIfUndefined = function(scope, property, defaultVal){
return scope[property] = scope[property] === undefined ? defaultVal : scope[property];
};
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
caption: '=?',
opened: '=?'
},
templateUrl: '../sdk/src/directives/ccZippy/cczippy.tpl.html',
link: function(scope, element, attrs){
var $element = $(element[0]),
$caption = $element.children('.cc-zippy-caption').first(),
$icon = $element.find('.cc-zippy-icon').first(),
openedIconClass = 'icon-chevron-up',
closedIconClass = 'icon-chevron-down';
defaultIfUndefined(scope, 'caption', 'default');
defaultIfUndefined(scope, 'opened', false);
var setOpen = function(opened){
$element.removeClass(opened ? 'cc-zippy-closed' : 'cc-zippy-opened');
$element.addClass(opened ? 'cc-zippy-opened' : 'cc-zippy-closed');
$icon.removeClass(opened ? closedIconClass : openedIconClass);
$icon.addClass(opened ? openedIconClass : closedIconClass);
};
var toggle = function(){
scope.opened = !scope.opened;
setOpen(scope.opened);
};
$caption.bind('click', toggle);
scope.$watch('opened', setOpen);
setOpen(scope.opened);
}
};
});
In the html we can either write:
<cc-zippy caption="'some text'">
some content
</cc-zippy>
or
<cc-zippy caption="'some text'" opened="true">
some content
</cc-zippy>
or
<cc-zippy caption="stringPropertyOnScope" opened="booleanPropertyOnScope">
some content
</cc-zippy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment