(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
'use strict';

var _conditional = require('./conditional');

// initialize all elements
var componentsList = document.querySelectorAll(_conditional.Conditional.getSelector()); // Import all components

for (var i = 0; i < componentsList.length; i++) {
	new _conditional.Conditional(componentsList[i]);
}
},{"./conditional":2}],2:[function(require,module,exports){
'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * conditional.js
 * set classnames based on condition
 */

/**
* Declare constants
*/
var attributeBase = 'data-rs-conditional';

var Conditional = exports.Conditional = function () {
	function Conditional(element) {
		_classCallCheck(this, Conditional);

		this.element = element;
		this.triggerElement = document.querySelector(this.element.getAttribute(this.attributes.trigger));
		this.condition = this.element.getAttribute(this.attributes.condition);
		this.match = null;
		this.addEventHandlers();
		this.validate();
	}

	/**
 * Declare attribute constants
 */


	_createClass(Conditional, [{
		key: 'getClassList',

		/**
   * get result classes from attribute
   * @param {string} type
   */
		value: function getClassList(type) {
			var classNamesRaw = this.element.getAttribute(this.attributes[type].class);
			return classNamesRaw === null ? [] : classNamesRaw.split(',').map(function (value) {
				return value.trim();
			});
		}

		/**
   * Add event handlers
   */

	}, {
		key: 'addEventHandlers',
		value: function addEventHandlers() {
			var _this = this;

			this.triggerElement.addEventListener('change', function () {
				_this.validate();
			});

			window.addEventListener('load', function () {
				_this.validate();
			});
		}

		/**
   * validate if the given condition is true
   */

	}, {
		key: 'validate',
		value: function validate() {
			this.match = document.querySelector(this.condition) !== null;
			if (this.match === true) {
				this.success();
			} else {
				this.failure();
			}
		}

		/**
   * Handle validation result on success
   */

	}, {
		key: 'success',
		value: function success() {
			// add success classes
			this.addClassList(this.classes.success);
			// remove failure classes
			this.removeClassList(this.classes.failure);
		}

		/**
   * Handle validation result on failure
   */

	}, {
		key: 'failure',
		value: function failure() {
			// add failure classes
			this.addClassList(this.classes.failure);
			// remove success classes
			this.removeClassList(this.classes.success);
		}

		/**
   * add classList to conditional element
   * @param {array} classList
   */

	}, {
		key: 'addClassList',
		value: function addClassList(classList) {
			for (var i = 0; i < classList.length; i++) {
				this.element.classList.add(classList[i]);
			}
		}

		/**
   * remove classList from conditional element
   * @param {array} classList
   */

	}, {
		key: 'removeClassList',
		value: function removeClassList(classList) {
			for (var i = 0; i < classList.length; i++) {
				this.element.classList.remove(classList[i]);
			}
		}

		/**
   * Get selector
   */

	}, {
		key: 'attributes',
		get: function get() {
			return {
				trigger: attributeBase + '-trigger',
				condition: attributeBase + '-condition',
				success: {
					class: attributeBase + '-success-class'
				},
				failure: {
					class: attributeBase + '-failure-class'

				}
			};
		}

		/**
  * Declare classes
  */

	}, {
		key: 'classes',
		get: function get() {
			return {
				success: this.getClassList('success'),
				failure: this.getClassList('failure')
			};
		}
	}], [{
		key: 'getSelector',
		value: function getSelector() {
			return '[' + attributeBase + ']';
		}
	}]);

	return Conditional;
}();
},{}]},{},[1]);
