Skip to content

Commit

Permalink
Update to class-based
Browse files Browse the repository at this point in the history
  • Loading branch information
megheaiulian committed Jan 13, 2019
1 parent b69deee commit 5fcfef0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 99 deletions.
1 change: 0 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"cosmoz-bottom-bar": "Neovici/cosmoz-bottom-bar#^2.0.0",
"cosmoz-i18next": "Neovici/cosmoz-i18next#^1.0.2",
"cosmoz-page-router": "Neovici/cosmoz-page-router#^2.0.0",
"cosmoz-templatize": "Neovici/cosmoz-templatize#^1.0.0",
"iron-flex-layout": "PolymerElements/iron-flex-layout#^2.0.3",
"paper-icon-button": "PolymerElements/paper-icon-button#^2.2.0",
"paper-spinner": "PolymerElements/paper-spinner#^2.1.0"
Expand Down
1 change: 0 additions & 1 deletion cosmoz-data-nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

<link rel="import" href="../cosmoz-bottom-bar/cosmoz-bottom-bar-view.html">
<link rel="import" href="../cosmoz-page-router/cosmoz-page-location.html">
<link rel="import" href="../cosmoz-templatize/cosmoz-templatize.html">

<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<link rel="import" href="../paper-spinner/paper-spinner-lite.html">
Expand Down
63 changes: 35 additions & 28 deletions cosmoz-data-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@
_asyncPeriod(callStep, timeout);
};
return _asyncPeriod(callStep, timeout);
};

class CosmozDataNav extends Polymer.mixinBehaviors([
Polymer.IronResizableBehavior,
},
{
Debouncer,
enqueueDebouncer,
FlattenedNodesObserver,
IronResizableBehavior,
Templatize,
mixinBehaviors
} = Polymer;


class CosmozDataNav extends mixinBehaviors([
IronResizableBehavior,
Cosmoz.TranslatableBehavior
], Polymer.Element) {

static get is() {
return 'cosmoz-data-nav';
}

static get properties() {
return {
/**
Expand All @@ -50,9 +61,9 @@
},

/**
* The name of the variable to add to the binding scope with the index
* for the item.
*/
* The name of the variable to add to the binding scope with the index
* for the item.
*/
indexAs: {
type: String,
value: 'index'
Expand Down Expand Up @@ -232,28 +243,37 @@
super();
this._cache = {};
this._preloadIdx = 0;
this._boundOnTemplatesChange = this._onTemplatesChange.bind(this);
}

connectedCallback() {
this._templatesObserver = new Polymer.FlattenedNodesObserver(this.$.templatesSlot, this._onTemplatesChange.bind(this));
super.connectedCallback();
this._templatesObserver = new FlattenedNodesObserver(
this.$.templatesSlot,
this._boundOnTemplatesChange
);
window.addEventListener('cosmoz-cache-purge', this._onCachePurge);
this.addEventListener('tap', this._onTap);
this.addEventListener('transitionend', this._onTransitionEnd);
}

disconnectedCallback() {
super.disconnectedCallback();
if (this._templatesObserver) {
this._templatesObserver.disconnect();
this._templatesObserver = null;
}

if (this._selectDebouncer != null) {
this._selectDebouncer.cancel();
}

this._cache = {};
this._indexRenderQueue = [];
window.removeEventListener('cosmoz-cache-purge', this._onCachePurge);
this.removeEventListener('tap', this._onTap);
this.removeEventListener('transitionend', this._onTransitionEnd);
if (this._selectDebouncer != null) {
this._selectDebouncer.cancel();
}

this.splice('_elements', 0, this._elements.length, this._createElement())
.forEach(element => {
this._removeInstance(element.__instance);
Expand Down Expand Up @@ -296,20 +316,15 @@
nextDisabled: true,
[this.indexAs]: true
};
this._elementCtor = Cosmoz.Templatize.templatize(this._elementTemplate, this, {
this._elementCtor = Templatize.templatize(this._elementTemplate, this, {
instanceProps: Object.assign({ [this.as]: true }, baseProps),
parentModel: true,
forwardParentProp: this._forwardHostProp,
forwardParentPath: this._forwardParentPath,
forwardHostProp: this._forwardHostProp,
forwardInstanceProp: this._notifyInstanceProp,
notifyInstanceProp: this._notifyInstanceProp
});
this._incompleteCtor = Cosmoz.Templatize.templatize(this._incompleteTemplate, this, {
this._incompleteCtor = Templatize.templatize(this._incompleteTemplate, this, {
instanceProps: baseProps,
parentModel: true,
forwardParentProp: this._forwardHostProp,
forwardParentPath: this._forwardParentPath,
forwardHostProp: this._forwardHostProp,
});
}
Expand All @@ -326,14 +341,6 @@
.filter(i => i != null);
}

_forwardParentPath(path, value) {
const instances = this._allInstances;
if (!instances || !instances.length) {
return;
}
instances.forEach(inst => inst.notifyPath(path, value, true));
}

_forwardHostProp(prop, value) {
const instances = this._allInstances;
if (!instances || !instances.length) {
Expand Down Expand Up @@ -698,13 +705,13 @@
if (isNaN(select)) {
return;
}
this._selectDebouncer = Polymer.Debouncer.debounce(this._selectDebouncer,
this._selectDebouncer = enqueueDebouncer(Debouncer.debounce(this._selectDebouncer,
Polymer.Async.timeOut.after(15),
() => {
this.animating = true;
this.select(this.selected + select);
}
);
));
}

/**
Expand Down
84 changes: 23 additions & 61 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,13 @@

setup(done => {
nav = fixture('defaults');
nav._templatesObserver.flush();
nav.items = Array(20).fill('').map((e, i) => i.toString());
if (nav._templatesObserver.flush) {
nav._templatesObserver.flush();
} else {
Polymer.dom.flush();
}
done();
flush(done);
});

test('instantiates a cosmoz-data-nav element', () => {
assert.equal(nav.is, 'cosmoz-data-nav');
assert.equal(nav.constructor.is, 'cosmoz-data-nav');
});

test('creates buffer elements', () => {
Expand Down Expand Up @@ -116,14 +112,9 @@

setup(done => {
nav = fixture('defaults');
nav._templatesObserver.flush();
nav.items = Array(20).fill('').map((e, i) => i.toString());
if (nav._templatesObserver.flush) {
nav._templatesObserver.flush();
} else {
Polymer.dom.flush();
}

done();
flush(done);
});

test('selected property is updated', () => {
Expand Down Expand Up @@ -171,18 +162,12 @@

setup(done => {
nav = fixture('defaults');

nav._templatesObserver.flush();
const items = Array(20).fill('').map((e, i) => i.toString());
items[0] = '0';
items[1] = '0';
nav.items = items;

if (nav._templatesObserver.flush) {
nav._templatesObserver.flush();
} else {
Polymer.dom.flush();
}
done();
flush(done);
});

test('setItemById handlers duplicate ids', done => {
Expand All @@ -205,13 +190,9 @@

setup(done => {
nav = fixture('defaults');
nav._templatesObserver.flush();
nav.items = Array(20).fill('').map((e, i) => i.toString());
if (nav._templatesObserver.flush) {
nav._templatesObserver.flush();
} else {
Polymer.dom.flush();
}
done();
flush(done);
});
test('cache stores one item', done => {
let cache = nav._cache;
Expand Down Expand Up @@ -258,7 +239,7 @@
done();
});

test('removeFromCache called with null or unknown item', done => {
test('removeFromCache called with null or unknown item', () => {
let cache = nav._cache,
cacheKeys;
nav.setItemById('1', { id: 900 });
Expand All @@ -271,8 +252,6 @@
cacheKeys = Object.keys(cache);
nav.removeFromCache({});
assert.equal(cacheKeys.length, Object.keys(cache).length);

done();
});

});
Expand All @@ -282,34 +261,28 @@

setup(done => {
nav = fixture('defaults');
nav.items = Array(20).fill('').map((e, i) => i.toString());
if (nav._templatesObserver.flush) {
nav._templatesObserver.flush();
} else {
Polymer.dom.flush();
}

done();
nav._templatesObserver.flush();
nav.items = Array(20).fill('').map((e, i) => i.toString());
flush(done);
});

test('setItemById warns about a unknown item', done => {
test('setItemById warns about a unknown item', () => {
const warnSpy = sinon.spy(console, 'warn');
const data = { id: 23 };
nav.setItemById('23', data);
assert.isTrue(warnSpy.calledWithExactly('trying to replace an item that is not in the list', '23', data), true);
warnSpy.restore();
done();
});

test('isIncompleteFn checks if an item is incomplete', done => {
test('isIncompleteFn checks if an item is incomplete', () => {
assert.isTrue(nav.isIncompleteFn());
assert.isTrue(nav.isIncompleteFn(null));
assert.isFalse(nav.isIncompleteFn({ id: '1' }));

nav.setItemById('1', { id: 1 });
assert.isTrue(nav.isIncompleteFn(nav.items[0]));
assert.isFalse(nav.isIncompleteFn(nav.items[1]));
done();
});

test('selectById updates selected property', () => {
Expand Down Expand Up @@ -348,32 +321,26 @@

setup(done => {
nav = fixture('elementsBuffer');
nav._templatesObserver.flush();
nav.items = Array(10).fill('').map((e, i) => i.toString());
if (nav._templatesObserver.flush) {
nav._templatesObserver.flush();
} else {
Polymer.dom.flush();
}

done();
flush(done);
});

test('instantiates a cosmoz-data-nav element', () => {
assert.equal(nav.is, 'cosmoz-data-nav');
assert.equal(nav.constructor.is, 'cosmoz-data-nav');
});

test('elementsBuffer property updates _elements', () => {
assert.equal(nav.elementsBuffer, 4);
assert.equal(nav._elements.length, 4);
});

test('_createElement does not create element if length is equal to elementsBuffer', done => {
test('_createElement does not create element if length is equal to elementsBuffer', () => {
assert.equal(nav.elementsBuffer, 4);
assert.equal(nav._elements.length, 4);

nav._createElement();
assert.equal(nav._elements.length, 4);
done();
});

test('_createElement returns new element', () => {
Expand All @@ -387,18 +354,13 @@

setup(done => {
nav = fixture('renderQueue');
nav._templatesObserver.flush();
nav.items = Array(10).fill('').map((e, i) => i.toString());
if (nav._templatesObserver.flush) {
nav._templatesObserver.flush();
} else {
Polymer.dom.flush();
}

done();
flush(done);
});

test('instantiates a cosmoz-data-nav element', () => {
assert.equal(nav.is, 'cosmoz-data-nav');
assert.equal(nav.constructor.is, 'cosmoz-data-nav');
});

test('renderQueue three items', done => {
Expand Down Expand Up @@ -435,4 +397,4 @@
</script>
</body>

</html>
</html>
10 changes: 3 additions & 7 deletions test/resizable.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,13 @@

setup(done => {
nav = fixture('resizable');
nav._templatesObserver.flush();
nav.items = Array(10).fill('').map((e, i) => i.toString());
if (nav._templatesObserver.flush) {
nav._templatesObserver.flush();
} else {
Polymer.dom.flush();
}
done();
flush(done);
});

test('instantiates a cosmoz-data-nav element', () => {
assert.equal(nav.is, 'cosmoz-data-nav');
assert.equal(nav.constructor.is, 'cosmoz-data-nav');
});

test('resize', done => {
Expand Down
3 changes: 2 additions & 1 deletion wct.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"plugins": {
"local": {
"browsers": [
"chrome"
"chrome",
"firefox"
],
"browserOptions": {
"chrome": [
Expand Down

0 comments on commit 5fcfef0

Please sign in to comment.