0.10.0 • Published 6 years ago

kist-lazyads v0.10.0

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

kist-lazyads

Simple ads manager. Provides async control with Postscribe.

Installation

npm install kist-lazyads --save

API

Lazyads(options)

Type: Function

Module class. Available options are:

el

Type: String|jQuery|Element

Element which will contain ad content. String should be valid CSS selector.

context

Type: Array

List of contexts which will be used to resolve ads.

contentIdDataProp

Type: String

data property which will be used to connect with ad content.

This will inject ad1 content inside element with data-ad-id="ad1"

{
	contentIdDataProp: 'ad-id'
}
<div data-ad-id="ad1"></div>

classes

Type: Object

HTML clasess for DOM elements.

{
	el: 'kist-Lazyads-item',
	isLoaded: 'is-loaded',
	isHidden: 'is-hidden',
	isContentEmpty: 'is-contentEmpty'
}

adapter

Type: Adapter

Adapter for banner system (e.g. Revive Ads, Google DFP, …).

.init(cb)

Type: Function
Returns: Lazyads

Initializes ad manager. This method must be called, either after setting up control, or after creating instance.

.control(options)

Type: Function
Returns: Lazyads

Provides individual control of ads based on specific conditions. Available options are:

name

Type: String

Name of control. Options with same name will be merged.

condition

Type: Function

Condition on which callback method of current control will be run.

ArgumentTypeDescription
eljQueryCurrent ad element which fullfils condition.

callback

Type: Function

Callback to run when condition is true.

ArgumentTypeDescription
eljQueryCurrent ad element which fullfils condition.
emitFunctionAccepts one argument: name of event to emit. Emitted event has form of {Provided argument}:{Control name} and is triggered on corresponding ad element.
waitForLayoutFunctionAlias for setTimeout. Default timeout is 300.

.addPlaceholder(el)

Type: Function
Returns: Lazyads

Dynamically creates placeholder for ad. Useful if you don’t want to display specific ad on Lazyads initialization.

el

Type: String|jQuery|Element

Element which will contain ad content. String should be valid CSS selector.

.recheckControl()

Type: Function
Returns: Lazyads

Iterates over every control entry and fires callback if condition is satisifed.

.destroy()

Type: Function
Returns: Lazyads

Destroys current Lazyads instance.

Examples

import Lazyads from 'kist-lazyads';
import MediaQueryContext from 'kist-lazyads/context/media-query.esm';
import ReviveAdsAdapter from 'kist-lazyads/adaptes/revive-ads.esm';

const lazyads = new Lazyads({
	el: '.Banner',
	adapter: new ReviveAdsAdapter(),
	context: [
		new MediaQueryContext({
			'screen and (min-width:1000px) and (max-width:1199px)': ['ad1','ad2','ad3','ad5'],
			'screen and (min-width:1500px)': ['ad1','ad2','ad3'],
			'screen and (min-width:915px) and (max-width:999px)': ['ad1','ad2','ad3','ad3','ad5'],
			'screen and (min-width:1200px) and (max-width:1499px)': ['ad1','ad2','ad3'],
			'screen and (min-width:728px) and (max-width:914px)': ['ad1','ad3','ad5'],
			'screen and (max-width:599px)': ['ad4']
		})
	]
});

lazyads
	.control({
		name: 'ad1',
		condition: ( el ) => {
			return el.data('ad-id') === 'ad1';
		},
		callback: ( el, emit, wait ) => {
			if ( el.hasClass('is-loaded') ) {
				if ( el.hasClass('is-hidden') ) {
					console.log(1);
					emit('foo');
				}
			}
		}
	})
	.control({
		name: 'ad4',
		condition: ( el ) => {
			return el.data('ad-id') === 'ad4';
		},
		callback: ( el, emit, wait ) => {
			console.log(2);
		}
	})
	.init(() => {
		console.log('Lazyads initialized!');
	});

lazyads.addPlaceholder($('<div clas="Banner" data-ad-id="ad5"></div>'));

$('[data-ad-id="ad1"]').on('foo:ad1', ( e, el ) => {
	console.log(arguments);
});

$(document).on('foo:ad1', ( e, el ) => {
	console.log(e.currentTarget);
	console.log(e.target);
});
<data class="Banner" data-ad-id="ad1"></div>
<data class="Banner" data-ad-id="ad2"></div>
<data class="Banner" data-ad-id="ad3"></div>
<data class="Banner" data-ad-id="ad4"></div>

Browser support

Tested in IE9+ and all modern browsers.

License

MIT © Ivan Nikolić

0.10.0

6 years ago

0.9.0

7 years ago

0.8.0

7 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.5.0-3

8 years ago

0.5.0-2

8 years ago

0.5.0-1

8 years ago

0.5.0-0

8 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.7

10 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago