2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 export interface IExpandCollapseScope extends ng.IScope {
25 expandedSelector:string;
27 isCloseOnInit:boolean;
28 loadDataFunction:Function;
29 isLoadingData:boolean;
32 export class ExpandCollapseDirective implements ng.IDirective {
38 expandedSelector: '@',
39 loadDataFunction: '&?',
43 public replace = false;
44 public restrict = 'AE';
45 public transclude = true;
47 template = ():string => {
48 return require('./expand-collapse.html');
51 link = (scope:IExpandCollapseScope, $elem:any) => {
52 scope.collapsed = false;
53 scope.isLoadingData = false;
54 $elem.addClass('expanded');
57 if (scope.isCloseOnInit) {
58 window.setTimeout(function () {
63 // $elem.click(function () {
66 $elem.bind('click', function() {
69 let expand = ():void => {
70 $elem.addClass('expanded');
71 scope.collapsed = false;
73 let element = <HTMLElement>$(scope.expandedSelector)[0];
74 let prevWidth = element.style.height;
75 element.style.height = 'auto';
76 let endWidth = getComputedStyle(element).height;
77 element.style.height = prevWidth;
78 element.offsetHeight; // force repaint
79 element.style.transition = 'height .3s ease-in-out';
80 element.style.height = endWidth;
81 element.hidden = false;
82 element.addEventListener('transitionend', function transitionEnd(event) {
83 if (event['propertyName'] == 'height') {
84 element.style.transition = '';
85 element.style.height = 'auto';
86 element.removeEventListener('transitionend', transitionEnd, false);
91 let collapse = ():void => {
92 $elem.removeClass('expanded');
93 scope.collapsed = true;
95 let element = <HTMLElement>$(scope.expandedSelector)[0];
96 element.style.height = getComputedStyle(element).height;
97 element.style.transition = 'height .5s ease-in-out';
98 element.offsetHeight; // force repaint
99 element.style.height = '0px';
100 element.hidden = true;
103 let toggle = ():void => {
104 if (scope.collapsed === true) {
105 if (scope.loadDataFunction) {
106 scope.isLoadingData = true;
107 let onSuccess = () => {
108 window.setTimeout(function () {
110 scope.isLoadingData = false;
113 scope.loadDataFunction().then(onSuccess);
116 if (scope.isLoadingData === false) {
122 if (scope.isLoadingData === false) {
130 public static factory = ()=> {
131 return new ExpandCollapseDirective();
135 ExpandCollapseDirective.factory.$inject = [];