Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatically show/hide slideable content #18

Open
jesseflorig opened this issue Jan 19, 2016 · 3 comments
Open

Programmatically show/hide slideable content #18

jesseflorig opened this issue Jan 19, 2016 · 3 comments

Comments

@jesseflorig
Copy link

What is the best way to programmatically show/hide the slideable content from a controller/service?

I'd like to avoid something like $('#toggle-slide').click()

@elewin
Copy link

elewin commented Jun 15, 2016

I've been searching for a way to do this too, it would be great if I could toggle from within the controller etc

@ricfwolff
Copy link

Hi!
I've made some changes that allow that behavior. But, as I'm new to AngularJS, I don't know if that is correct.

I've created an attribute sl-expanded-model to bind a watch to a bool.

Html:

<div class="slideable" sl-expanded-model="card.isExpanded">

Directive:

.directive('slideable', function () {
            return {
                restrict: 'C',

                // added new attribute to scope:
                scope: {
                    slExpandedModel: '='
                },

                compile: function (element, attr) {
                    // wrap tag
                    var contents = element.html();
                    element.html('<div class="slideable_content" style="margin:0 !important; padding:0 !important" >' + contents + '</div>');

                    return function postLink(scope, element, attrs) {
                        // default properties
                        attrs.duration = (!attrs.duration) ? '1s' : attrs.duration;
                        attrs.easing = (!attrs.easing) ? 'ease-in-out' : attrs.easing;
                        element.css({
                            'overflow': 'hidden',
                            'height': '0px',
                            'transitionProperty': 'height',
                            'transitionDuration': attrs.duration,
                            'transitionTimingFunction': attrs.easing
                        });

                        var target, content;

                        // added a watch to the new attribute and expanded/collapsed according to its value:
                        scope.$watch('slExpandedModel', function (expanded) {
                            if (angular.isUndefined(expanded)) {
                                expanded = false;
                            }

                            if (!target) target = element[0];
                            if (!content) content = target.querySelector('.slideable_content');

                            if (expanded) {
                                content.style.border = '1px solid rgba(0,0,0,0)';
                                var y = content.clientHeight;
                                content.style.border = 0;
                                target.style.height = y + 'px';
                            } else {
                                target.style.height = '0px';
                            }
                        }, true);
                    };
                }
            };
        })

Now my div expands and collapses when I change card.isExpanded from the controller.

The second directive (slideToggle) became useless in my case. The div itself knows if it should slide or not. I don't know if that's ok, specially regarding performance. But maybe it can help someone.

@elewin
Copy link

elewin commented Sep 12, 2017

This works great! Good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants