All files / js index.js

5.77% Statements 3/52
5.41% Branches 2/37
23.08% Functions 3/13
5.77% Lines 3/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169              1x                             1x                                                                                 11x                                                                                                                                                                                                                  
import Component from './utils/component';
import * as type from './utils/type';
 
import defaultOptions from './defaultOptions';
 
export default class bulmaCollapsible extends Component {
	constructor(element, options = {}) {
		super(element, options, defaultOptions);
 
		//Bind events to current class
		this.onTriggerClick = this.onTriggerClick.bind(this);
 
		// Initiate plugin
		this._init();
	}
 
	/**
	 * Initiate all DOM element corresponding to selector
	 * @method
	 * @return {Array} Array of all Plugin instances
	 */
	static attach(selector = '.is-collapsible', options = {}) {
		return super.attach(selector, options);
	}
 
	/**
	 * Initiate plugin
	 * @method init
	 * @return {void}
	 */
	_init() {
		this._parent = this.element.dataset.parent;
		if (this._parent) {
			const parent = document.getElementById(this._parent);
			this._siblings = parent.querySelectorAll(this.options.selector) || [];
		}
 
		this._triggers = document.querySelectorAll(`[data-action="collapse"][href="#${this.element.id}"], [data-action="collapse"][data-target="${this.element.id}"]`) || null;
		if (this._triggers) {
			this._triggers.on('click touch', this.onTriggerClick);
		}
 
		// Set initial state
		if (this.element.classList.contains('is-active')) {
			this.expand();
		}  else {
			this.collapse();
		}
	}
 
	destroy() {
		// Unbind all event listener from triggers
		if (this._triggers) {
			this._triggers.off('click touch', this.onTriggerClick, false);
		}
	}
 
	/**
	 * Check is element is collapsed
	 * @method init
	 * @return {Boolean} true if element is collapsed(closed) else false
	 */
	collapsed() {
		return this._collapsed;
	}
 
	/**
	 * Expand(Open) element
	 * @method init
	 * @return {void}
	 */
	expand() {
		if (typeof this._collapsed !== 'undefined' && !this._collapsed) {
			return;
		}
 
		this.emit('before:expand', this);
		
		// Close all siblings (based on data-parent attribute) if allowMultiple option set to False
		if (this._parent && !type.BooleanParse(this.options.allowMultiple)) {
			this._siblings.forEach(sibling => {
				if (!sibling.isSameNode(this.element)) {
					if (sibling.bulmaCollapsible) {
						sibling.bulmaCollapsible('close');
					}
				}
			});
		}
 
		// Apply style to show (expand) collapsible element
		this.element.style.height = this.element.scrollHeight + 'px';
		this.element.classList.add('is-active');
		this.element.setAttribute('aria-expanded', true);
 
		// Add 'is-active" class to all triggers
		if (this._triggers) {
			this._triggers.forEach(trigger => {
				trigger.classList.add('is-active');
			});
		}
 
		this._collapsed = false;
 
		this.emit('after:expand', this);
	}
 
	/**
	 * Shortcut to expand method
	 */
	open()
	{
		this.expand();
	}
 
	/**
	 * Collapse(Close) element
	 * @method init
	 * @return {void}
	 */
	collapse() {
		if (typeof this._collapsed !== 'undefined' && this._collapsed) {
			return;
		}
 
		this.emit('before:collapse', this);
 
		// Apply style to hide (collapse) collapsible element
		this.element.style.height = 0;
		this.element.classList.remove('is-active');
		this.element.setAttribute('aria-expanded', false);
 
		// Remove 'is-active" class from all triggers
		if (this._triggers) {
			this._triggers.forEach(trigger => {
				trigger.classList.remove('is-active');
			});
		}
 
		this._collapsed = true;
 
		this.emit('after:collapse', this);
	}
 
	/**
	 * Shortcut to collapse method
	 */
	close()
	{
		this.collapse();
	}
 
	/**
	 * Trigger listener to Toggle element state
	 * @method init
	 * @return {void}
	 */
	onTriggerClick(event) {
		event.preventDefault();
 
		if (this.collapsed()) {
			event.currentTarget.classList.add('is-active');
			this.expand();
		} else {
			event.currentTarget.classList.remove('is-active');
			this.collapse();
		}
	}
}