/* * Copyright 2012, Mozilla Foundation and contributors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 'use strict'; var util = require('../util/util'); var host = require('../util/host'); var domtemplate = require('../util/domtemplate'); /** * We want to avoid commands having to create DOM structures because that's * messy and because we're going to need to have command output displayed in * different documents. A View is a way to wrap an HTML template (for * domtemplate) in with the data and options to render the template, so anyone * can later run the template in the context of any document. * View also cuts out a chunk of boiler place code. * @param options The information needed to create the DOM from HTML. Includes: * - html (required): The HTML source, probably from a call to require * - options (default={}): The domtemplate options. See domtemplate for details * - data (default={}): The data to domtemplate. See domtemplate for details. * - css (default=none): Some CSS to be added to the final document. If 'css' * is used, use of cssId is strongly recommended. * - cssId (default=none): An ID to prevent multiple CSS additions. See * util.importCss for more details. * @return An object containing a single function 'appendTo()' which runs the * template adding the result to the specified element. Takes 2 parameters: * - element (required): the element to add to * - clear (default=false): if clear===true then remove all pre-existing * children of 'element' before appending the results of this template. */ exports.createView = function(options) { if (options.html == null) { throw new Error('options.html is missing'); } return { /** * RTTI. Yeah. */ isView: true, /** * Run the template against the document to which element belongs. * @param element The element to append the result to * @param clear Set clear===true to remove all children of element */ appendTo: function(element, clear) { // Strict check on the off-chance that we later think of other options // and want to replace 'clear' with an 'options' parameter, but want to // support backwards compat. if (clear === true) { util.clearElement(element); } element.appendChild(this.toDom(element.ownerDocument)); }, /** * Actually convert the view data into a DOM suitable to be appended to * an element * @param document to use in realizing the template */ toDom: function(document) { if (options.css) { util.importCss(options.css, document, options.cssId); } var child = host.toDom(document, options.html); domtemplate.template(child, options.data || {}, options.options || {}); return child; } }; };