Skip to content

Commit

Permalink
Change _instances from Array to Set
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonykoerber committed Apr 13, 2016
1 parent 3a73b42 commit c318207
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 50 deletions.
77 changes: 63 additions & 14 deletions src/mm-list-item/index.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
<!DOCTYPE html>
<html>
<head>
<head>
<script language="javascript" src="../../bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="mm-list-item.html">
<link rel="import" href="../mm-input/mm-input.html">
</head>
<body>
<link rel="import" href="mm-list-item.html">
<link rel="import" href="../mm-input/mm-input.html">
<link rel="import" href="../shared/behaviors/resizable.html"/>
</head>
<body>
<!-- testing resizable, which we don't actually use in mm-list-item anymore -->
<!--
<dom-module id="mm-test">
<template>
<style>
:host {
display: block;
position: relative;
}
</style>
<div id="d" style="width:100%; height: 20px; background: #ff0000;"></div>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'mm-test',
properties: {
resizeTarget:{
type:Object,
value: function() { return this.$.d; }
}
},
behaviors: [
StrandTraits.Resizable
],
elementResize: function(w, h, target) {
console.log(w, h, target);
}
});
});
</script>
</dom-module>
-->

<mm-list-item>Item here 00</mm-list-item>
<mm-list-item>I am a moderate amount of text. Just a bit but not too much</mm-list-item>
<mm-list-item >Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</mm-list-item>
<mm-list-item highlight="Lorem">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</mm-list-item>
<template is="dom-bind">
<mm-input value="{{hl}}"></mm-input>
<mm-list-item highlight="{{hl}}">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</mm-list-item>
</template>
</body>
<mm-list-item>Item here 00</mm-list-item>
<mm-list-item>I am a moderate amount of text. Just a bit but not too much</mm-list-item>
<mm-list-item >Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</mm-list-item>
<mm-list-item highlight="Lorem">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</mm-list-item>
<template is="dom-bind">
<mm-input value="{{hl}}"></mm-input>
<mm-list-item highlight="{{hl}}">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</mm-list-item>
</template>

<!--
<mm-test></mm-test>
<mm-test></mm-test>
<mm-test></mm-test>
<mm-test></mm-test>
<mm-test></mm-test>
<script type="text/javascript">
// document.addEventListener("WebComponentsReady", function(e) {
// console.log(e);
// });
</script>
-->

</body>
</html>
6 changes: 3 additions & 3 deletions src/mm-list-item/mm-list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@
}
},

elementResize: function() {
this.debounce("update-title", this.updateTitle, 0);
},
// elementResize: function() {
// this.debounce("update-title", this.updateTitle, 0);
// },

updateTitle: function() {
var m = StrandLib.Measure;
Expand Down
9 changes: 4 additions & 5 deletions src/shared/behaviors/autoclosable.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
*/
(function (scope) {

var _instances = [];
var _instances = new Set();

function _addInstance(instance) {
_instances.push(instance);
_instances.add(instance);
}

function _removeInstance(instance) {
_instances.splice(_instances.indexOf(instance), 1);
_instances.delete(instance);
}

document.addEventListener("mouseup", function(e) {
var normalized = Polymer.dom(e);
var instance;
var scope;
var filter;
for(var i in _instances) {
instance = _instances[i];
for (var instance of _instances) {
filter = instance._closeFilter;
scope = typeof instance.scope === "object" ? instance.scope : instance;
filter.apply(scope, [instance, normalized, e]);
Expand Down
28 changes: 16 additions & 12 deletions src/shared/behaviors/resizable.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,41 @@

(function (scope) {

var _instances = [];
var _instances = new Set();
var _il = 0;
var _backoff = 0;
var _backoff_inc = 200;
var _backoff_max = 2000;

function _addInstance(instance, target) {
_instances.push({
target:target,
var meta = {
target: target,
instance: instance,
width:target.offsetWidth,
height:target.offsetHeight,
});
width: target.offsetWidth,
height: target.offsetHeight,
};
_instances.add(meta);
_il++;
requestAnimationFrame(_measure);
}

function _removeInstance(instance) {
_instances.filter(function(meta) {
return meta.instance === instance;
}).forEach(function(target) {
_instances.splice(_instances.indexOf(target), 1);
_il--;
var metaArr = Array.from(_instances);
var meta = metaArr.filter(function(item){
return item.instance === instance;
});
_instances.delete(meta);
_il--;
}

function _measure() {
_backoff += _backoff_inc;
if (_backoff > _backoff_max) _backoff = _backoff_max;

var _instArr = Array.from(_instances);

for(var i=0; i<_il; i++) {
var meta = _instances[i];
var meta = _instArr[i];
var w = meta.target.offsetWidth;
var h = meta.target.offsetHeight;
if (meta.width !== w) {
Expand Down Expand Up @@ -74,6 +77,7 @@
},

detached:function() {
console.log('detached', this);
_removeInstance(this);
},

Expand Down
13 changes: 6 additions & 7 deletions src/shared/behaviors/stackable.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
modal:{
base:1*_w,
max:2*_w,
items:[],
items: new Set(),
topIndex:1*_w
},
ui:{
base:2*_w,
max:3*_w,
items:[],
items: new Set,
topIndex:2*_w
},
tooltip:{
base:3*_w,
max:4*_w,
items:[],
items: new Set(),
topIndex:3*_w
}
};
Expand All @@ -33,11 +33,10 @@
var cat = _instances[type],
inst = !instance ? host : instance;

if (cat && cat.items.length < cat.max) {
cat.items.push(inst);
if (cat && cat.items.size < cat.max) {
cat.items.add(inst);
host._setDepth(cat.topIndex);
cat.topIndex++;
// instance._setDepth(cat.items.length + cat.base);
} else {
throw(new Error("Could not add item at " + cat.max));
}
Expand All @@ -48,7 +47,7 @@
inst = !instance ? host : instance;

if (cat) {
cat.items.splice(cat.items.indexOf(inst), 1);
cat.items.delete(inst);
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/shared/behaviors/windownotifier.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@
*/
(function (scope) {

var _instances = [];
var _instances = new Set();

function _addInstance(instance) {
_instances.push(instance);
_instances.add(instance);
}

function _removeInstance(instance) {
_instances.splice(_instances.indexOf(instance), 1);
_instances.delete(instance);
}

function _resizeHandler() {
var i;
for (i = _instances.length - 1; i >= 0; i--) {
if (_instances[i].resize) { _instances[i].resize(); }
for (var instance of _instances) {
if (instance.resize) instance.resize();
}
}

function _scrollHandler (argument) {
var i;
for (i = _instances.length - 1; i >= 0; i--) {
if (_instances[i].scroll) { _instances[i].scroll(); }
for (var instance of _instances) {
if (instance.scroll) instance.scroll();
}
}

Expand Down

0 comments on commit c318207

Please sign in to comment.