Last active
May 30, 2018 14:24
-
-
Save andreasnymark/34fe2ca74f8a37b6a40f36b74f3f632c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Andreas Nymark <[email protected]> | |
* @license MIT | |
* @version 1 | |
**/ | |
var ggb = ggb || {}; | |
ggb.consent = ( function ( window, document ) { | |
'use strict'; | |
var ins = [], | |
defs = { | |
select: '.js-consent', | |
}; | |
/** | |
* @constructor IncrementalSearch | |
* @param {HTMLElement} Element | |
**/ | |
var Consent = function( elem, callback ) { | |
var t = this; | |
t.callback = callback || null; | |
t.xhr = new XMLHttpRequest(); | |
t.xhr.addEventListener( 'readystatechange', t.xhrState.bind( t ) ); | |
t.elem = elem; | |
t.data = JSON.parse( t.elem.getAttribute( defs.attr ) ); | |
t.remove = t.elem.getAttribute( 'data-consent-remove' ); | |
t.submit = t.elem.querySelector( '[type=submit]' ); | |
t.id = t.elem.querySelector( '#id' ).value; | |
t.submit.addEventListener( 'click', t.update.bind( t ) ); | |
}; | |
Consent.prototype = { | |
update: function ( evt ) { | |
var t = this; | |
t.xhrRequest( t.id, t.xhr, '/a/consent' ); | |
evt.preventDefault(); | |
}, | |
xhrRequest: function ( id, xhr, url ) { | |
var data = new FormData(); | |
data.append( 'id', id ); | |
xhr.open( 'POST', url, true ); | |
xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' ); | |
xhr.send( data ); | |
}, | |
xhrState: function ( evt ) { | |
var t = this; | |
var xhr = evt.target; | |
if ( xhr.readyState === XMLHttpRequest.DONE ) { | |
if ( xhr.status === 200 ){ | |
var resp = JSON.parse( xhr.response ); | |
if ( resp.consent ) { | |
t.elem.parentNode.removeChild( t.elem ); | |
// if ( typeof t.callback === 'function' ) t.callback(); | |
if ( t.callback ) { | |
executeFunctionByName( t.callback, window ); | |
// eval ( t.callback ); | |
// str.replace(/\w+/g, tmpl || "<span>$&</span>"); | |
// window['merl']['dialog']['close'](); | |
// var call = t.callback.replace( /\w+/g, '[$&]' ); | |
// call = t.callback.replace( /\./g, '' ); | |
// console.log( call ); | |
} | |
var withClass = document.querySelectorAll( '.' + t.remove ); | |
for ( var i = 0, len = withClass.length; i < len; i++ ) { | |
withClass[ i ].classList.remove( t.remove ); | |
} | |
} | |
} else { | |
alert( 'Error' ); | |
} | |
} | |
}, | |
}; | |
/** | |
* Init | |
* | |
* @method init | |
* @param options {Object} Object | |
**/ | |
var init = function( options ) { | |
if ( options ) { | |
for ( var o in options ) { | |
defs[ o ] = options[ o ]; | |
} | |
} | |
defs.all = document.querySelectorAll( defs.select ); | |
if ( defs.all.length > 0 ) { | |
for( var i = 0, len = defs.all.length; i<len; i++ ) { | |
var consent = defs.all[ i ], | |
callback = consent.getAttribute( 'data-callback' ); | |
if ( callback ) { | |
ins[i] = new Consent( defs.all[ i ], callback ); | |
} else { | |
ins[i] = new Consent( defs.all[ i ] ); | |
} | |
} | |
} | |
}; | |
// https://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string | |
var executeFunctionByName = function ( functionName, context /*, args */) { | |
var args = Array.prototype.slice.call(arguments, 2); | |
var namespaces = functionName.split("."); | |
var func = namespaces.pop(); | |
for(var i = 0; i < namespaces.length; i++) { | |
context = context[namespaces[i]]; | |
} | |
return context[func].apply(context, args); | |
} | |
return { | |
init: init | |
}; | |
} ( window, document ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment