/* Unobtrusive JavaScript https://github.com/rails/rails/blob/master/actionview/app/assets/javascripts Released under the MIT license */; /* Reduced and customized for ActiveElementUJS */ (function() { var context = this; (function() { (function() { this.ActiveElementUJS = { linkClickSelector: 'a[data-ae-method]' }; }).call(this); }).call(context); var ActiveElementUJS = context.ActiveElementUJS; (function() { (function() { var nonce; nonce = null; ActiveElementUJS.loadCSPNonce = function() { var ref; return nonce = (ref = document.querySelector("meta[name=csp-nonce]")) != null ? ref.content : void 0; }; ActiveElementUJS.cspNonce = function() { return nonce != null ? nonce : ActiveElementUJS.loadCSPNonce(); }; }).call(this); (function() { var expando, m; m = Element.prototype.matches || Element.prototype.matchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector || Element.prototype.webkitMatchesSelector; ActiveElementUJS.matches = function(element, selector) { if (selector.exclude != null) { return m.call(element, selector.selector) && !m.call(element, selector.exclude); } else { return m.call(element, selector); } }; expando = '_ujsData'; ActiveElementUJS.getData = function(element, key) { var ref; return (ref = element[expando]) != null ? ref[key] : void 0; }; ActiveElementUJS.setData = function(element, key, value) { if (element[expando] == null) { element[expando] = {}; } return element[expando][key] = value; }; ActiveElementUJS.$ = function(selector) { return Array.prototype.slice.call(document.querySelectorAll(selector)); }; }).call(this); (function() { var $, csrfParam, csrfToken; $ = ActiveElementUJS.$; csrfToken = ActiveElementUJS.csrfToken = function() { var meta; meta = document.querySelector('meta[name=csrf-token]'); return meta && meta.content; }; csrfParam = ActiveElementUJS.csrfParam = function() { var meta; meta = document.querySelector('meta[name=csrf-param]'); return meta && meta.content; }; ActiveElementUJS.CSRFProtection = function(xhr) { var token; token = csrfToken(); if (token != null) { return xhr.setRequestHeader('X-CSRF-Token', token); } }; ActiveElementUJS.refreshCSRFTokens = function() { var param, token; token = csrfToken(); param = csrfParam(); if ((token != null) && (param != null)) { return $('form input[name="' + param + '"]').forEach(function(input) { return input.value = token; }); } }; }).call(this); (function() { var CustomEvent, fire, matches, preventDefault; matches = ActiveElementUJS.matches; CustomEvent = window.CustomEvent; if (typeof CustomEvent !== 'function') { CustomEvent = function(event, params) { var evt; evt = document.createEvent('CustomEvent'); evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail); return evt; }; CustomEvent.prototype = window.Event.prototype; preventDefault = CustomEvent.prototype.preventDefault; CustomEvent.prototype.preventDefault = function() { var result; result = preventDefault.call(this); if (this.cancelable && !this.defaultPrevented) { Object.defineProperty(this, 'defaultPrevented', { get: function() { return true; } }); } return result; }; } fire = ActiveElementUJS.fire = function(obj, name, data) { var event; event = new CustomEvent(name, { bubbles: true, cancelable: true, detail: data }); obj.dispatchEvent(event); return !event.defaultPrevented; }; ActiveElementUJS.stopEverything = function(e) { fire(e.target, 'ujs:everythingStopped'); e.preventDefault(); e.stopPropagation(); return e.stopImmediatePropagation(); }; ActiveElementUJS.delegate = function(element, selector, eventType, handler) { return element.addEventListener(eventType, function(e) { var target; target = e.target; while (!(!(target instanceof Element) || matches(target, selector))) { target = target.parentNode; } if (target instanceof Element && handler.call(target, e) === false) { e.preventDefault(); return e.stopPropagation(); } }); }; }).call(this); (function() { var AcceptHeaders, CSRFProtection, createXHR, cspNonce, fire, prepareOptions, processResponse; cspNonce = ActiveElementUJS.cspNonce, CSRFProtection = ActiveElementUJS.CSRFProtection, fire = ActiveElementUJS.fire; AcceptHeaders = { '*': '*/*', text: 'text/plain', html: 'text/html', xml: 'application/xml, text/xml', json: 'application/json, text/javascript', script: 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript' }; ActiveElementUJS.ajax = function(options) { var xhr; options = prepareOptions(options); xhr = createXHR(options, function() { var ref, response; response = processResponse((ref = xhr.response) != null ? ref : xhr.responseText, xhr.getResponseHeader('Content-Type')); if (Math.floor(xhr.status / 100) === 2) { if (typeof options.success === "function") { options.success(response, xhr.statusText, xhr); } } else { if (typeof options.error === "function") { options.error(response, xhr.statusText, xhr); } } return typeof options.complete === "function" ? options.complete(xhr, xhr.statusText) : void 0; }); if ((options.beforeSend != null) && !options.beforeSend(xhr, options)) { return false; } if (xhr.readyState === XMLHttpRequest.OPENED) { return xhr.send(options.data); } }; prepareOptions = function(options) { options.url = options.url || location.href; options.type = options.type.toUpperCase(); if (options.type === 'GET' && options.data) { if (options.url.indexOf('?') < 0) { options.url += '?' + options.data; } else { options.url += '&' + options.data; } } if (AcceptHeaders[options.dataType] == null) { options.dataType = '*'; } options.accept = AcceptHeaders[options.dataType]; if (options.dataType !== '*') { options.accept += ', */*; q=0.01'; } return options; }; createXHR = function(options, done) { var xhr; xhr = new XMLHttpRequest(); xhr.open(options.type, options.url, true); xhr.setRequestHeader('Accept', options.accept); if (typeof options.data === 'string') { xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); } if (!options.crossDomain) { xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); CSRFProtection(xhr); } xhr.withCredentials = !!options.withCredentials; xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { return done(xhr); } }; return xhr; }; processResponse = function(response, type) { var parser, script; if (typeof response === 'string' && typeof type === 'string') { if (type.match(/\bjson\b/)) { try { response = JSON.parse(response); } catch (error) {} } else if (type.match(/\b(?:java|ecma)script\b/)) { script = document.createElement('script'); script.setAttribute('nonce', cspNonce()); script.text = response; document.head.appendChild(script).parentNode.removeChild(script); } else if (type.match(/\b(xml|html|svg)\b/)) { parser = new DOMParser(); type = type.replace(/;.+/, ''); try { response = parser.parseFromString(response, type); } catch (error) {} } } return response; }; ActiveElementUJS.href = function(element) { return element.href; }; ActiveElementUJS.isCrossDomain = function(url) { var e, originAnchor, urlAnchor; originAnchor = document.createElement('a'); originAnchor.href = location.href; urlAnchor = document.createElement('a'); try { urlAnchor.href = url; return !(((!urlAnchor.protocol || urlAnchor.protocol === ':') && !urlAnchor.host) || (originAnchor.protocol + '//' + originAnchor.host === urlAnchor.protocol + '//' + urlAnchor.host)); } catch (error) { e = error; return true; } }; }).call(this); (function() { var matches, toArray; matches = ActiveElementUJS.matches; toArray = function(e) { return Array.prototype.slice.call(e); }; ActiveElementUJS.serializeElement = function(element, additionalParam) { var inputs, params; inputs = [element]; if (matches(element, 'form')) { inputs = toArray(element.elements); } params = []; inputs.forEach(function(input) { if (!input.name || input.disabled) { return; } if (matches(input, 'select')) { return toArray(input.options).forEach(function(option) { if (option.selected) { return params.push({ name: input.name, value: option.value }); } }); } else if (input.checked || ['radio', 'checkbox', 'submit'].indexOf(input.type) === -1) { return params.push({ name: input.name, value: input.value }); } }); if (additionalParam) { params.push(additionalParam); } return params.map(function(param) { if (param.name != null) { return (encodeURIComponent(param.name)) + "=" + (encodeURIComponent(param.value)); } else { return param; } }).join('&'); }; ActiveElementUJS.formElements = function(form, selector) { if (matches(form, 'form')) { return toArray(form.elements).filter(function(el) { return matches(el, selector); }); } else { return toArray(form.querySelectorAll(selector)); } }; }).call(this); (function() { var allowAction, fire, stopEverything; fire = ActiveElementUJS.fire, stopEverything = ActiveElementUJS.stopEverything; ActiveElementUJS.handleConfirm = function(e) { if (!allowAction(this)) { return stopEverything(e); } }; allowAction = function(element) { var answer, callback, message; message = element.getAttribute('data-confirm'); if (!message) { return true; } answer = false; if (fire(element, 'confirm')) { try { answer = confirm(message); } catch (error) {} callback = fire(element, 'confirm:complete', [answer]); } return answer && callback; }; }).call(this); (function() { var stopEverything; stopEverything = ActiveElementUJS.stopEverything; ActiveElementUJS.handleMethod = function(e) { var csrfParam, csrfToken, form, formContent, href, link, method; link = this; method = link.getAttribute('data-ae-method'); if (!method) { return; } href = ActiveElementUJS.href(link); csrfToken = ActiveElementUJS.csrfToken(); csrfParam = ActiveElementUJS.csrfParam(); form = document.createElement('form'); formContent = ""; if ((csrfParam != null) && (csrfToken != null) && !ActiveElementUJS.isCrossDomain(href)) { formContent += ""; } formContent += ''; form.method = 'post'; form.action = href; form.target = link.target; form.innerHTML = formContent; form.style.display = 'none'; document.body.appendChild(form); form.querySelector('[type="submit"]').click(); return stopEverything(e); }; }).call(this); (function() { var ajax, fire, getData, isCrossDomain, isRemote, matches, serializeElement, setData, stopEverything, slice = [].slice; matches = ActiveElementUJS.matches, getData = ActiveElementUJS.getData, setData = ActiveElementUJS.setData, fire = ActiveElementUJS.fire, stopEverything = ActiveElementUJS.stopEverything, ajax = ActiveElementUJS.ajax, isCrossDomain = ActiveElementUJS.isCrossDomain, serializeElement = ActiveElementUJS.serializeElement; isRemote = function(element) { var value; value = element.getAttribute('data-remote'); return (value != null) && value !== 'false'; }; ActiveElementUJS.handleRemote = function(e) { var button, data, dataType, element, method, url, withCredentials; element = this; if (!isRemote(element)) { return true; } if (!fire(element, 'ajax:before')) { fire(element, 'ajax:stopped'); return false; } withCredentials = element.getAttribute('data-with-credentials'); dataType = element.getAttribute('data-type') || 'script'; method = element.getAttribute('data-ae-method'); url = ActiveElementUJS.href(element); data = element.getAttribute('data-params'); ajax({ type: method || 'GET', url: url, data: data, dataType: dataType, beforeSend: function(xhr, options) { if (fire(element, 'ajax:beforeSend', [xhr, options])) { return fire(element, 'ajax:send', [xhr]); } else { fire(element, 'ajax:stopped'); return false; } }, success: function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; return fire(element, 'ajax:success', args); }, error: function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; return fire(element, 'ajax:error', args); }, complete: function() { var args; args = 1 <= arguments.length ? slice.call(arguments, 0) : []; return fire(element, 'ajax:complete', args); }, crossDomain: isCrossDomain(url), withCredentials: (withCredentials != null) && withCredentials !== 'false' }); return stopEverything(e); }; ActiveElementUJS.formSubmitButtonClick = function(e) { var button, form; button = this; form = button.form; if (!form) { return; } if (button.name) { setData(form, 'ujs:submit-button', { name: button.name, value: button.value }); } setData(form, 'ujs:formnovalidate-button', button.formNoValidate); setData(form, 'ujs:submit-button-formaction', button.getAttribute('formaction')); return setData(form, 'ujs:submit-button-formmethod', button.getAttribute('formmethod')); }; ActiveElementUJS.preventInsignificantClick = function(e) { var data, insignificantMetaClick, link, metaClick, method, nonPrimaryMouseClick; link = this; method = (link.getAttribute('data-ae-method') || 'GET').toUpperCase(); data = link.getAttribute('data-params'); metaClick = e.metaKey || e.ctrlKey; insignificantMetaClick = metaClick && method === 'GET' && !data; nonPrimaryMouseClick = (e.button != null) && e.button !== 0; if (nonPrimaryMouseClick || insignificantMetaClick) { return e.stopImmediatePropagation(); } }; }).call(this); (function() { var $, CSRFProtection, delegate, disableElement, enableElement, fire, formSubmitButtonClick, getData, handleConfirm, handleDisabledElement, handleMethod, handleRemote, loadCSPNonce, preventInsignificantClick, refreshCSRFTokens; fire = ActiveElementUJS.fire, delegate = ActiveElementUJS.delegate, getData = ActiveElementUJS.getData, $ = ActiveElementUJS.$, refreshCSRFTokens = ActiveElementUJS.refreshCSRFTokens, CSRFProtection = ActiveElementUJS.CSRFProtection, loadCSPNonce = ActiveElementUJS.loadCSPNonce, enableElement = ActiveElementUJS.enableElement, disableElement = ActiveElementUJS.disableElement, handleDisabledElement = ActiveElementUJS.handleDisabledElement, handleConfirm = ActiveElementUJS.handleConfirm, preventInsignificantClick = ActiveElementUJS.preventInsignificantClick, handleRemote = ActiveElementUJS.handleRemote, formSubmitButtonClick = ActiveElementUJS.formSubmitButtonClick, handleMethod = ActiveElementUJS.handleMethod; ActiveElementUJS.start = function() { if (window._active_element_loaded) { throw new Error('active_element-ujs has already been loaded!'); } delegate(document, ActiveElementUJS.linkClickSelector, 'click', handleMethod); document.addEventListener('DOMContentLoaded', refreshCSRFTokens); document.addEventListener('DOMContentLoaded', loadCSPNonce); console.log("ActiveElement-ujs loaded."); return window._active_element_loaded = true; }; if (window.ActiveElementUJS === ActiveElementUJS && fire(document, 'rails:attachBindings')) { ActiveElementUJS.start(); } }).call(this); }).call(this); if (typeof module === "object" && module.exports) { module.exports = ActiveElementUJS; } else if (typeof define === "function" && define.amd) { define(ActiveElementUJS); } }).call(this);