Commit 9df98e41 authored by Olivier Scherrer's avatar Olivier Scherrer

Olives: Update Olives and Emily to latest 1.x versions

parent b9c3b488
......@@ -2,8 +2,8 @@
"name": "todomvc-olives",
"version": "0.0.0",
"dependencies": {
"olives": "~1.4.0",
"emily": "~1.3.5",
"olives": "~1.6.0",
"emily": "~1.8.1",
"requirejs": "~2.1.5",
"todomvc-common": "~0.3.0"
}
......
......@@ -13,6 +13,8 @@ define(["Store", "Observable", "Tools", "DomUtils"],
*/
function BindPlugin(Store, Observable, Tools, DomUtils) {
"use strict";
return function BindPluginConstructor($model, $bindings) {
/**
......@@ -32,14 +34,28 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
* each foreach has its itemRenderer
* @private
*/
_itemRenderers = {};
_itemRenderers = {},
/**
* The observers handlers
* for debugging only
* @private
*/
this.observers = {};
_observers = {};
/**
* Exposed for debugging purpose
* @private
*/
this.observers = _observers;
function _removeObserversForId(id) {
if (_observers[id]) {
_observers[id].forEach(function (handler) {
_model.unwatchValue(handler);
});
delete _observers[id];
}
}
/**
* Define the model to watch for
......@@ -133,7 +149,9 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
_rootNode = rootNode;
renderer = _rootNode.querySelector("*");
this.setRenderer(renderer);
renderer && _rootNode.removeChild(renderer);
if (renderer) {
_rootNode.removeChild(renderer);
}
return true;
} else {
return false;
......@@ -173,7 +191,7 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
* The nodes created from the items are stored here
* @private
*/
this.items = new Store([]);
this.items = {};
/**
* Set the start limit
......@@ -182,7 +200,8 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
* @returns the value
*/
this.setStart = function setStart(start) {
return _start = parseInt(start, 10);
_start = parseInt(start, 10);
return _start;
};
/**
......@@ -201,7 +220,8 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
* @returns the value
*/
this.setNb = function setNb(nb) {
return _nb = nb == "*" ? nb : parseInt(nb, 10);
_nb = nb == "*" ? nb : parseInt(nb, 10);
return _nb;
};
/**
......@@ -223,12 +243,16 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
var node,
next;
if (typeof id == "number" && !this.items.get(id)) {
if (typeof id == "number" && !this.items[id]) {
next = this.getNextItem(id);
node = this.create(id);
if (node) {
// IE (until 9) apparently fails to appendChild when insertBefore's second argument is null, hence this.
next = this.getNextItem(id);
next ? _rootNode.insertBefore(node, next) : _rootNode.appendChild(node);
if (next) {
_rootNode.insertBefore(node, next);
} else {
_rootNode.appendChild(node);
}
return true;
} else {
return false;
......@@ -245,11 +269,18 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
* @returns
*/
this.getNextItem = function getNextItem(id) {
return this.items.alter("slice", id+1).filter(function (value) {
if (DomUtils.isAcceptedType(value)) {
return true;
var keys = Object.keys(this.items).map(function (string) {
return Number(string);
}),
closest = Tools.closestGreater(id, keys),
closestId = keys[closest];
// Only return if different
if (closestId != id) {
return this.items[closestId];
} else {
return;
}
})[0];
};
/**
......@@ -259,10 +290,11 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
* @returns
*/
this.removeItem = function removeItem(id) {
var item = this.items.get(id);
var item = this.items[id];
if (item) {
_rootNode.removeChild(item);
this.items.set(id);
delete this.items[id];
_removeObserversForId(id);
return true;
} else {
return false;
......@@ -286,7 +318,7 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
child.setAttribute("data-" + _plugins.name+"_id", id);
});
this.items.set(id, newNode);
this.items[id] = newNode;
_plugins.apply(newNode);
return newNode;
}
......@@ -310,8 +342,10 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
if (_nb !== null && _start !== null) {
// Loop through the existing items
this.items.loop(function (value, idx) {
Tools.loop(this.items, function (value, idx) {
// If an item is out of the boundary
idx = Number(idx);
if (idx < _start || idx >= (_start + _tmpNb) || !_model.has(idx)) {
// Mark it
marked.push(idx);
......@@ -378,10 +412,7 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
_model.watch("deleted", function (idx) {
itemRenderer.render();
// Also remove all observers
this.observers[idx] && this.observers[idx].forEach(function (handler) {
_model.unwatchValue(handler);
}, this);
delete this.observers[idx];
_removeObserversForId(idx);
},this);
this.setItemRenderer(idItemRenderer, itemRenderer);
......@@ -533,14 +564,14 @@ function BindPlugin(Store, Observable, Tools, DomUtils) {
/**
* Prevents the submit and set the model with all form's inputs
* @param {HTMLFormElement} form
* @param {HTMLFormElement} DOMfrom
* @returns true if valid form
*/
this.form = function form(form) {
if (form && form.nodeName == "FORM") {
this.form = function form(DOMform) {
if (DOMform && DOMform.nodeName == "FORM") {
var that = this;
form.addEventListener("submit", function (event) {
Tools.toArray(form.querySelectorAll("[name]")).forEach(that.set, that);
DOMform.addEventListener("submit", function (event) {
Tools.toArray(DOMform.querySelectorAll("[name]")).forEach(that.set, that);
event.preventDefault();
}, true);
return true;
......
......@@ -6,6 +6,8 @@
define(["Tools"], function (Tools) {
"use strict";
return {
/**
* Returns a NodeList including the given dom node,
......
......@@ -14,6 +14,8 @@ define(["DomUtils"],
*/
function EventPlugin(Utils) {
"use strict";
/**
* The event plugin constructor.
* ex: new EventPlugin({method: function(){} ...}, false);
......
......@@ -15,6 +15,8 @@ define(["Store", "Tools"],
*/
function LocalStore(Store, Tools) {
"use strict";
function LocalStoreConstructor() {
/**
......@@ -96,7 +98,7 @@ function LocalStore(Store, Tools) {
return function LocalStoreFactory(init) {
LocalStoreConstructor.prototype = new Store(init);
return new LocalStoreConstructor;
return new LocalStoreConstructor();
};
});
......@@ -13,6 +13,8 @@ define(["StateMachine", "Store", "Plugins", "DomUtils", "Tools"],
*/
function OObject(StateMachine, Store, Plugins, DomUtils, Tools) {
"use strict";
return function OObjectConstructor(otherStore) {
/**
......@@ -45,7 +47,7 @@ function OObject(StateMachine, Store, Plugins, DomUtils, Tools) {
// as it wouldn't be possible to know which node would belong to which UI
// This is probably a DOM limitation.
if (baseNode.childNodes.length > 1) {
throw Error("UI.template should have only one parent node");
throw new Error("UI.template should have only one parent node");
} else {
UI.dom = baseNode.childNodes[0];
}
......@@ -54,7 +56,7 @@ function OObject(StateMachine, Store, Plugins, DomUtils, Tools) {
} else {
// An explicit message I hope
throw Error("UI.template must be set prior to render");
throw new Error("UI.template must be set prior to render");
}
},
......@@ -63,13 +65,17 @@ function OObject(StateMachine, Store, Plugins, DomUtils, Tools) {
* This dom node should be somewhere in the dom of the application
* @private
*/
place = function place(UI, place, beforeNode) {
if (place) {
place = function place(UI, DOMplace, beforeNode) {
if (DOMplace) {
// IE (until 9) apparently fails to appendChild when insertBefore's second argument is null, hence this.
beforeNode ? place.insertBefore(UI.dom, beforeNode) : place.appendChild(UI.dom);
if (beforeNode) {
DOMplace.insertBefore(UI.dom, beforeNode);
} else {
DOMplace.appendChild(UI.dom);
}
// Also save the new place, so next renderings
// will be made inside it
_currentPlace = place;
_currentPlace = DOMplace;
}
},
......@@ -108,7 +114,7 @@ function OObject(StateMachine, Store, Plugins, DomUtils, Tools) {
* It has set/get/del/has/watch/unwatch methods
* @see Emily's doc for more info on how it works.
*/
this.model = otherStore instanceof Store ? otherStore : new Store;
this.model = otherStore instanceof Store ? otherStore : new Store();
/**
* The module that will manage the plugins for this UI
......
......@@ -12,6 +12,8 @@ define(["OObject", "Tools"],
*/
function PlacePlugin(OObject, Tools) {
"use strict";
/**
* Intilialize a Place.plugin with a list of OObjects
* @param {Object} $uis a list of OObjects such as:
......
......@@ -17,6 +17,8 @@ define(["Tools", "DomUtils"],
*/
function Plugins(Tools, DomUtils) {
"use strict";
return function PluginsConstructor($plugins) {
/**
......
......@@ -12,6 +12,8 @@ define(["Observable", "Tools"],
*/
function SocketIOTransport(Observable, Tools) {
"use strict";
/**
* Defines the SocketIOTransport
* @private
......@@ -47,7 +49,7 @@ function SocketIOTransport(Observable, Tools) {
*/
this.getSocket = function getSocket() {
return _socket;
},
};
/**
* Subscribe to a socket event
......@@ -56,7 +58,7 @@ function SocketIOTransport(Observable, Tools) {
*/
this.on = function on(event, func) {
return _socket.on(event, func);
},
};
/**
* Subscribe to a socket event but disconnect as soon as it fires.
......@@ -95,15 +97,17 @@ function SocketIOTransport(Observable, Tools) {
* @param {Object} scope the scope in which to execute the callback
*/
this.request = function request(channel, data, func, scope) {
if (typeof channel == "string"
&& typeof data != "undefined") {
if (typeof channel == "string" &&
typeof data != "undefined") {
var reqData = {
eventId: Date.now() + Math.floor(Math.random()*1e6),
data: data
},
boundCallback = function () {
func && func.apply(scope || null, arguments);
if (func) {
func.apply(scope || null, arguments);
}
};
this.once(reqData.eventId, boundCallback);
......@@ -125,9 +129,9 @@ function SocketIOTransport(Observable, Tools) {
* @returns
*/
this.listen = function listen(channel, data, func, scope) {
if (typeof channel == "string"
&& typeof data != "undefined"
&& typeof func == "function") {
if (typeof channel == "string" &&
typeof data != "undefined" &&
typeof func == "function") {
var reqData = {
eventId: Date.now() + Math.floor(Math.random()*1e6),
......@@ -135,7 +139,9 @@ function SocketIOTransport(Observable, Tools) {
keepAlive: true
},
boundCallback = function () {
func && func.apply(scope || null, arguments);
if (func) {
func.apply(scope || null, arguments);
}
},
that = this;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment