Skip to content

Commit

Permalink
Build version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Rusch committed Apr 16, 2018
1 parent a9cf0a8 commit 1bdd92b
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 191 deletions.
198 changes: 105 additions & 93 deletions dist/pacto.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,26 @@ class __Resolver {
__refs$3.set(this, refs);
}

add(key, value) {
add(namespace, value) {
const {register} = __refs$3.get(this);
register[key] = value;
register[namespace] = value;
return this;
}

remove(key) {
remove(namespace) {
const {register} = __refs$3.get(this);
register[key] = undefined;
delete(register[key]);
register[namespace] = undefined;
delete(register[namespace]);
return this;
}

get(key) {
get(namespace) {
const {register} = __refs$3.get(this);
return register[key];
return register[namespace];
}

has(key) {
return !!this.get(key);
has(namespace) {
return !!this.get(namespace);
}

}
Expand Down Expand Up @@ -295,102 +295,127 @@ class Context extends EventEmitter {

}

function __isBoolean(value) {
return typeof value === 'boolean';
function __isFalse(value) {
return typeof value === 'boolean' && !value;
}

function __getSettings(instance) {
const settings = instance.settings;

if (!settings || typeof settings !== 'object') {
throw new Error('Define settings object');
}

if (!settings.view) {
throw new Error('Define a view');
}

if (!settings.selector) {
throw new Error('Define a selector');
}

if (!settings.namespace) {
throw new Error('Define a namespace');
}

return settings;
}


class Initialize {

get settings() {
return {};
return null;
}

run() {
this.beforeAll();

const
{context, event, settings} = this,
settings = __getSettings(this),
{context, event} = this,
{data} = event,
{viewoptions} = settings,
views = context.values.get(settings.namespace) || [],
root = data && data.root ? root : document.body
root = data && data.root ? data.root : document.body
;

if (!settings.viewclass) {
throw new Error('Define a view class');
}

if (!settings.selector) {
throw new Error('Define a selector');
}
let result;

if (!settings.namespace) {
throw new Error('Define a namespace');
result = this.beforeAll();
if (__isFalse(result)) {
return;
}

[...root.querySelectorAll(settings.selector)].forEach((el, index) => {
const options = {el, context, ...viewoptions};
let
result = null,
view = null
;
if (views.some((view) => view.el == el)) {
return;
}

const options = {el, context, ...settings.params};
let view = null;

result = this.beforeEach(options, el, index);
if (!__isBoolean(result)) {
throw new Error('The return value of beforeEach() must be a boolean.');
} else if (!result) {
if (__isFalse(result)) {
return;
}

view = new settings.viewclass(options).render();
view = new settings.view(options);
view.render();

result = this.afterEach(view, el, index);
if (!__isBoolean(result)) {
throw new Error('The return value of afterEach() must be a boolean.');
} else if (!result) {
if (__isFalse(result)) {
return;
}

views.push(view);
});

if (views.length) {
if (views.length > 0) {
context.values.add(settings.namespace, views);
}

this.afterAll();
this.afterAll(views);
}

beforeAll() {
// Overwrite this...
}

beforeEach(/* options, element, index */) {
beforeEach(/* options, el, index */) {
// Overwrite this...
return true;
}

afterAll() {
afterEach(/* view, el, index */) {
// Overwrite this...
}

afterEach(/* view, element, index */) {
afterAll(/* views */) {
// Overwrite this...
return true;
}

}

function __getSettings$1(instance) {
const settings = instance.settings;

if (!settings || typeof settings !== 'object') {
throw new Error('Define settings object');
}

if (!settings.selector) {
throw new Error('Define a selector');
}

return settings;
}


class InitializeLazy {

constructor() {
this._onIntersect = this._onIntersect.bind(this);
this._fetched = false;
}

get settings() {
return {};
return null;
}

get import() {
Expand All @@ -400,98 +425,85 @@ class InitializeLazy {
get observerSettings() {
return {
rootMargin: '0px',
threshold: [0.0001, 0.9999]
threshold: [0, 0.5, 1]
};
}

run() {
const {settings} = this;

if (!settings.selector) {
throw new Error('Define a selector');
}

const settings = __getSettings$1(this);
this._lookup(settings.selector);
}

_lookup(selector) {
const
{event} = this,
{data} = event,
root = data && data.root ? root : document.body,
root = data && data.root ? data.root : document.body,
elements = root.querySelectorAll(selector)
;

if (elements.length) {
if (window.IntersectionObserver) {
this._observe(elements);
} else {
this._fetch();
}
if (elements.length === 0) {
return;
}

if (window.IntersectionObserver) {
this._observe(elements);
} else {
this._fetch();
}
}

_observe(elements) {
this._observers = [...elements].map((element) => {
const observer = new window.IntersectionObserver(
this._onIntersect,
this.observerSettings
);

observer.observe(element);
return observer;
});
this._observer = new window.IntersectionObserver(this._onIntersect, this.observerSettings);
[...elements].forEach((element) => this._observer.observe(element));
}

_release() {
if (this._observers && this._observers.length) {
this._observers.forEach((observer) => observer.disconnect());
this._observers = null;
}
_disconnect() {
this._observer.disconnect();
this._observer = null;
}

_fetch() {
const {event} = this;

if (this._fetched) {
return;
}

this._fetched = true;
this.import.then((module) => {
const Initialize = module.Action || module.default;
const Action = module.Action || module.default;
let error = null;

if (!Initialize) {
throw new Error('Module must return Action or default');
if (!Action) {
error = new Error('Module must export Action or default');
this.context.trigger(this.event.type + ':error', {error}); // Only for testing reasons
throw error;
}

if (!(typeof Initialize.prototype.run === 'function')) {
throw new Error('Module must be an Action');
if (!(typeof Action.prototype.run === 'function')) {
error = new Error('Module must be an Action');
this.context.trigger(this.event.type + ':error', {error}); // Only for testing reasons
throw error;
}

// Replace the proxy action with the loaded action
this.context.actions
.add(event.type, Initialize)
.add(event.type, Action)
.remove(event.type, this.constructor);

// Execute the current action:
this._execute(Initialize);
this._execute(Action);
});
}

_execute(Initialize) {
const action = new Initialize();
_execute(Action) {
const action = new Action();
action.context = this.context;
action.event = this.event;
action.run();
}

_onIntersect(entries) {
let isVisible = false;
entries.forEach((entry) => isVisible = entry.intersectionRatio > 0 || isVisible);
const isVisible = entries.some((entry) => entry.isIntersecting);

if (isVisible) {
this._release();
this._disconnect();
this._fetch();
}
}
Expand All @@ -500,7 +512,7 @@ class InitializeLazy {

class View extends EventEmitter {

constructor(options = {}) {
constructor(options) {
super();
this.options = options;
this.context = options.context;
Expand Down
Loading

0 comments on commit 1bdd92b

Please sign in to comment.