1 /** 2 * Olives http://flams.github.com/olives 3 * The MIT License (MIT) 4 * Copyright (c) 2012-2014 Olivier Scherrer <pode.fr@gmail.com> - Olivier Wietrich <olivier.wietrich@gmail.com> 5 */ 6 "use strict"; 7 8 var OObject = require("./OObject"), 9 Tools = require("emily").Tools; 10 11 /** 12 * @class 13 * Place plugin places OObject in the DOM. 14 * @requires OObject, Tools 15 */ 16 17 /** 18 * Intilialize a Place.plugin with a list of OObjects 19 * @param {Object} $uis a list of OObjects such as: 20 * { 21 * "header": new OObject(), 22 * "list": new OObject() 23 * } 24 * @Constructor 25 */ 26 module.exports = function PlacePluginConstructor($uis) { 27 28 /** 29 * The list of uis currently set in this place plugin 30 * @private 31 */ 32 var _uis = {}; 33 34 /** 35 * Attach an OObject to this DOM element 36 * @param {HTML|SVGElement} node the dom node where to attach the OObject 37 * @param {String} the name of the OObject to attach 38 * @throws {NoSuchOObject} an error if there's no OObject for the given name 39 */ 40 this.place = function place(node, name) { 41 if (_uis[name] instanceof OObject) { 42 _uis[name].place(node); 43 } else { 44 throw new Error(name + " is not an OObject UI in place:"+name); 45 } 46 }; 47 48 /** 49 * Add an OObject that can be attached to a dom element 50 * @param {String} the name of the OObject to add to the list 51 * @param {OObject} ui the OObject to add the list 52 * @returns {Boolean} true if the OObject was added 53 */ 54 this.set = function set(name, ui) { 55 if (typeof name == "string" && ui instanceof OObject) { 56 _uis[name] = ui; 57 return true; 58 } else { 59 return false; 60 } 61 }; 62 63 /** 64 * Add multiple dom elements at once 65 * @param {Object} $uis a list of OObjects such as: 66 * { 67 * "header": new OObject(), 68 * "list": new OObject() 69 * } 70 */ 71 this.setAll = function setAll(uis) { 72 Tools.loop(uis, function (ui, name) { 73 this.set(name, ui); 74 }, this); 75 }; 76 77 /** 78 * Returns an OObject from the list given its name 79 * @param {String} the name of the OObject to get 80 * @returns {OObject} OObject for the given name 81 */ 82 this.get = function get(name) { 83 return _uis[name]; 84 }; 85 86 this.setAll($uis); 87 88 };