Created
May 8, 2013 12:53
-
-
Save cburgdorf/5540239 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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