/*\ title: $:/core/modules/widgets/widget.js type: application/javascript module-type: widget Widget base class \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; /* Create a widget object for a parse tree node parseTreeNode: reference to the parse tree node to be rendered options: see below Options include: wiki: mandatory reference to wiki associated with this render tree parentWidget: optional reference to a parent renderer node for the context chain document: optional document object to use instead of global document */ var Widget = function(parseTreeNode,options) { if(arguments.length > 0) { this.initialise(parseTreeNode,options); } }; /* Initialise widget properties. These steps are pulled out of the constructor so that we can reuse them in subclasses */ Widget.prototype.initialise = function(parseTreeNode,options) { options = options || {}; // Save widget info this.parseTreeNode = parseTreeNode; this.wiki = options.wiki; this.parentWidget = options.parentWidget; this.variablesConstructor = function() {}; this.variablesConstructor.prototype = this.parentWidget ? this.parentWidget.variables : {}; this.variables = new this.variablesConstructor(); this.document = options.document; this.attributes = {}; this.children = []; this.domNodes = []; this.eventListeners = {}; // Hashmap of the widget classes if(!this.widgetClasses) { Widget.prototype.widgetClasses = $tw.modules.applyMethods("widget"); } }; /* Render this widget into the DOM */ Widget.prototype.render = function(parent,nextSibling) { this.parentDomNode = parent; this.execute(); this.renderChildren(parent,nextSibling); }; /* Compute the internal state of the widget */ Widget.prototype.execute = function() { this.makeChildWidgets(); }; /* Set the value of a context variable name: name of the variable value: value of the variable params: array of {name:, default:} for each parameter */ Widget.prototype.setVariable = function(name,value,params) { this.variables[name] = {value: value, params: params}; }; /* Get the prevailing value of a context variable name: name of variable options: see below Options include params: array of {name:, value:} for each parameter defaultValue: default value if the variable is not defined */ Widget.prototype.getVariable = function(name,options) { options = options || {}; var actualParams = options.params || [], parentWidget = this.parentWidget; // Check for the variable defined in the parent widget (or an ancestor in the prototype chain) if(parentWidget && name in parentWidget.variables) { var variable = parentWidget.variables[name], value = variable.value; // Substitute any parameters specified in the definition value = this.substituteVariableParameters(value,variable.params,actualParams); value = this.substituteVariableReferences(value); return value; } // If the variable doesn't exist in the parent widget then look for a macro module return this.evaluateMacroModule(name,actualParams,options.defaultValue); }; Widget.prototype.substituteVariableParameters = function(text,formalParams,actualParams) { if(formalParams) { var nextAnonParameter = 0, // Next candidate anonymous parameter in macro call paramInfo, paramValue; // Step through each of the parameters in the macro definition for(var p=0; p 0) { var nextAnonParameter = 0, // Next candidate anonymous parameter in macro call paramInfo, paramValue; // Step through each of the parameters in the macro definition for(var p=0; p 0) { return this.parentWidget.children[index-1]; } } return null; }; /* Render the children of this widget into the DOM */ Widget.prototype.renderChildren = function(parent,nextSibling) { $tw.utils.each(this.children,function(childWidget) { childWidget.render(parent,nextSibling); }); }; /* Add a list of event listeners from an array [{type:,handler:},...] */ Widget.prototype.addEventListeners = function(listeners) { var self = this; $tw.utils.each(listeners,function(listenerInfo) { self.addEventListener(listenerInfo.type,listenerInfo.handler); }); }; /* Add an event listener */ Widget.prototype.addEventListener = function(type,handler) { var self = this; if(typeof handler === "string") { // The handler is a method name on this widget this.eventListeners[type] = function(event) { return self[handler].call(self,event); }; } else { // The handler is a function this.eventListeners[type] = function(event) { return handler.call(self,event); }; } }; /* Dispatch an event to a widget. If the widget doesn't handle the event then it is also dispatched to the parent widget */ Widget.prototype.dispatchEvent = function(event) { // Dispatch the event if this widget handles it var listener = this.eventListeners[event.type]; if(listener) { // Don't propagate the event if the listener returned false if(!listener(event)) { return false; } } // Dispatch the event to the parent widget if(this.parentWidget) { return this.parentWidget.dispatchEvent(event); } return true; }; /* Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering */ Widget.prototype.refresh = function(changedTiddlers) { return this.refreshChildren(changedTiddlers); }; /* Rebuild a previously rendered widget */ Widget.prototype.refreshSelf = function() { var nextSibling = this.findNextSiblingDomNode(); this.removeChildDomNodes(); this.render(this.parentDomNode,nextSibling); }; /* Refresh all the children of a widget */ Widget.prototype.refreshChildren = function(changedTiddlers) { var self = this, refreshed = false; $tw.utils.each(this.children,function(childWidget) { refreshed = childWidget.refresh(changedTiddlers) || refreshed; }); return refreshed; }; /* Find the next sibling in the DOM to this widget. This is done by scanning the widget tree through all next siblings and their descendents that share the same parent DOM node */ Widget.prototype.findNextSiblingDomNode = function(startIndex) { // Refer to this widget by its index within its parents children var parent = this.parentWidget, index = startIndex !== undefined ? startIndex : parent.children.indexOf(this); if(index === -1) { throw "node not found in parents children"; } // Look for a DOM node in the later siblings while(++index < parent.children.length) { var domNode = parent.children[index].findFirstDomNode(); if(domNode) { return domNode; } } // Go back and look for later siblings of our parent if it has the same parent dom node var grandParent = parent.parentWidget; if(grandParent && parent.parentDomNode === this.parentDomNode) { index = grandParent.children.indexOf(parent); return parent.findNextSiblingDomNode(index); } return null; }; /* Find the first DOM node generated by a widget or its children */ Widget.prototype.findFirstDomNode = function() { // Return the first dom node of this widget, if we've got one if(this.domNodes.length > 0) { return this.domNodes[0]; } // Otherwise, recursively call our children for(var t=0; t 0) { $tw.utils.each(this.domNodes,function(domNode) { domNode.parentNode.removeChild(domNode); }); this.domNodes = []; } else { // Otherwise, ask the child widgets to delete their DOM nodes $tw.utils.each(this.children,function(childWidget) { childWidget.removeChildDomNodes(); }); } }; /* Invoke the action widgets that are descendents of the current widget. */ Widget.prototype.invokeActions = function(triggeringWidget,event) { var handled = false; // For each child widget for(var t=0; t