UNPKG

3.85 kBJavaScriptView Raw
1import $ from './jquery';
2
3var $document = $(document);
4
5//convenience function because this needs to be run for all the events.
6var getExpanderProperties = function (event) {
7 var properties = {};
8
9 properties.$trigger = $(event.currentTarget);
10 properties.$content = $document.find('#' + properties.$trigger.attr('aria-controls'));
11 properties.triggerIsParent = properties.$content.parent().filter(properties.$trigger).length !== 0;
12 properties.$shortContent = properties.triggerIsParent ? properties.$trigger.find('.aui-expander-short-content') : null;
13 properties.height = properties.$content.css('min-height');
14 properties.isCollapsible = properties.$trigger.data('collapsible') !== false;
15 properties.replaceText = properties.$trigger.attr('data-replace-text'); //can't use .data here because it doesn't update after the first call
16 properties.replaceSelector = properties.$trigger.data('replace-selector');
17
18 return properties;
19};
20
21var replaceText = function (properties) {
22 if (properties.replaceText) {
23 var $replaceElement = properties.replaceSelector ?
24 properties.$trigger.find(properties.replaceSelector) :
25 properties.$trigger;
26
27 properties.$trigger.attr('data-replace-text', $replaceElement.text());
28 $replaceElement.text(properties.replaceText);
29 }
30};
31
32//events that the expander listens to
33var EXPANDER_EVENTS = {
34 'aui-expander-invoke': function (event) {
35 var $trigger = $(event.currentTarget);
36 var $content = $document.find('#' + $trigger.attr('aria-controls'));
37 var isCollapsible = $trigger.data('collapsible') !== false;
38
39 //determine if content should be expanded or collapsed
40 if ($content.attr('aria-expanded') === 'true' && isCollapsible) {
41 $trigger.trigger('aui-expander-collapse');
42 } else {
43 $trigger.trigger('aui-expander-expand');
44 }
45 },
46
47 'aui-expander-expand': function (event) {
48 var properties = getExpanderProperties(event);
49
50 // If the expander is already expanded, do nothing.
51 if (properties.$content.attr('aria-expanded') === 'true') {
52 return;
53 }
54
55 properties.$content.attr('aria-expanded', 'true');
56 properties.$trigger.attr('aria-expanded', 'true');
57
58 properties.$content.get(0).removeAttribute('hidden');
59
60 //handle replace text
61 replaceText(properties);
62
63 //if the trigger is the parent also hide the short-content (default)
64 if (properties.triggerIsParent) {
65 properties.$shortContent.hide();
66 }
67 properties.$trigger.trigger('aui-expander-expanded');
68
69 },
70
71 'aui-expander-collapse': function (event) {
72 var properties = getExpanderProperties(event);
73
74 // If the expander is already collapsed, do nothing.
75 if (properties.$content.attr('aria-expanded') !== 'true') {
76 return;
77 }
78
79 //handle replace text
80 replaceText(properties);
81
82 //collapse the expander
83 properties.$content.attr('aria-expanded', 'false');
84 properties.$trigger.attr('aria-expanded', 'false');
85
86 //if the trigger is the parent also hide the short-content (default)
87 if (properties.triggerIsParent) {
88 properties.$shortContent.show();
89 }
90
91 //handle the height option
92 if (properties.$content.outerHeight() === 0) {
93 properties.$content.get(0).setAttribute('hidden', '');
94 }
95
96 properties.$trigger.trigger('aui-expander-collapsed');
97 },
98
99 'click.aui-expander': function (event) {
100 var $target = $(event.currentTarget);
101 $target.trigger('aui-expander-invoke', event.currentTarget);
102 }
103};
104
105//delegate events to the triggers on the page
106$document.on(EXPANDER_EVENTS, '.aui-expander-trigger');