Documentation
This is a reference for the JavaScript library and the XML schema.
Working with widgets is relatively easy and straightforward. (more content)
Members marked with a version 0.x.y were introduced in that version.
Members that are marked for Page are applicable at the page level,
whereas members marked are applicable inside of a widget.
Some members can apply to both page and widget.
JavaScript Reference
Global Object (svidget) Page
Provides access into the root of the Svidget library.
svidget.$ property 0.2.0
svidget.connected() property
svidget.current() property
svidget.load() method Page
svidget.load(selector, url, paramObj, callback)
svidget.load(selector, options, paramObj, callback)
- selector string - A CSS selector to the container element where the widget will be placed.
- url string - The url for the widget.
- options object - An object containing properties to initialize the widget.
- paramObj object - The name of the handler. This can be used to unregister the handler by name.
- callback function - A callback function to call once the widget is loaded.
var myWidget1 = svidget.load("#container", "widgets/graph.svg", {
data: [1, 2, 3],
color: "#da3333",
backColor: "#ffac33"
},
function (widget) {
// do something
}
);
var myWidget2 = svidget.load("#container", {
url: "widgets/graph.svg",
connected: false,
crossdomain: false,
}, {
data: [4, 5, 6],
color: "#3333da",
backColor: "#33ffac"
},
function (widget) {
// do something
}
);
svidget.off() method Page
svidget.off(type, handler)
svidget.off(type, name)
- type string - The event type. See svidget Events for the list of types.
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
svidget.offload() method Page
svidget.offload(handler)
svidget.offload(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
svidget.offwidgetload() method Page
svidget.offwidgetload(type, handler)
svidget.offwidgetload(type, name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
svidget.on() method Page
svidget.on(type, data, name, handler)
svidget.on(type, data, handler)
svidget.on(type, handler)
- type string - The event type. See svidget Events for the list of types.
- data object - An arbitrary value to pass to the Event object when the event is triggered.
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = svidget.on("load" { message: "hiya!" }, "myHandler", function (e) {
console.log(e.data.message); // "hiya"
});
svidget.onload() method Page
svidget.onload(data, name, handler)
svidget.onload(data, handler)
svidget.onload(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = svidget.onload({ message: "hiya!" }, "myHandler", function (e) {
console.log(e.data.message); // "hiya"
});
svidget.onwidgetload() method Page
svidget.onwidgetload(data, name, handler)
svidget.onwidgetload(data, handler)
svidget.onwidgetload(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = svidget.onwidgetload({ message: "ok" }, "myHandler", function (e) {
console.log(e.value); // e.value == the widget ID
});
svidget.widget() property Page
svidget.widget(id)
svidget.widget(index)
- id string - An arbitrary value to pass to the Event object when the event is triggered
- index number - The name of the handler. This can be used to unregister the handler by name.
var myWidget1 = svidget.widget("myWidget");
var myWidget2 = svidget.widget(2);
svidget.widgets() property Page
svidget.widgets()
svidget Events Page
- load Page - Triggers when all widgets are loaded.
- load - Triggers when the widget is ready.
- widgetload Page - Triggers when any widget is loaded.
Svidget.Action class
Represents an invocable action on the widget. This object corresponds to a <svidget:action> element in the widget.
addParam() method
action.addParam(actionParam)
action.addParam(name, options)
- actionParam string - The action param to add.
- name string - The action param name to add.
- options string - The action param options to use when creating the action param.
binding() property function
action.binding()
action.binding(globalFuncName)
action.binding(func)
var action = svidget.$.action("invokeMe");
alert(action.binding()); // can be string or function, depending on which was set
action.binding(invokeMeFunc);
function invokeMeFunc(actionParam1) {
// do something
}
bindingFunc() property function
action.bindingFunc()
var action = svidget.$.action("invokeMe");
action.binding("invokeMeFunc");
var func = action.bindingFunc();
function invokeMeFunc(actionParam1) {
// do something
}
description() property string
action.description()
action.description(desc)
var action = svidget.$.action("invokeMe");
document.getElementById("mydesc").innerHTML = action.description();
action.description("Updated description");
enabled() property bool
action.enabled()
action.enabled(isEnabled)
var action = svidget.$.action("invokeMe");
if (!action.enabled()) action.enabled(true);
external() property bool
action.external()
action.external(isExternal)
var action = svidget.$.action("invokeMe");
if (!action.external()) action.external(true);
invoke() method
action.invoke(arg1, arg2, ...)
var action = svidget.$.action("getArea");
// register handler for invoke
action.oninvoke(function (e) {
alert(e.returnValue); //28.2743
});
// invoke
action.invoke(3);
function areaOfCircle(radius) {
return Math.pow(3, 2) * Math.PI;
}
name() property string
action.name()
off() method
action.off(type, handler)
action.off(type, name)
- type string - The event type. See Svidget.Action Events for the list of types.
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
offchange() method
action.offchange(handler)
action.offchange(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
offinvoke() method
action.offinvoke(handler)
action.offinvoke(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
offparamadd() method
action.offparamadd(handler)
action.offparamadd(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
offparamchange() method
action.offparamchange(handler)
action.offparamchange(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
offparamremove() method
action.offparamremove(handler)
action.offparamremove(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
on() method
action.on(type, data, name, handler)
action.on(type, data, handler)
action.on(type, handler)
- type string - The event type. See Svidget.Action Events for the list of types.
- data object - An arbitrary value to pass to the Event object when the event is triggered.
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = action.on("invoke", { message: "hiya!" }, "myHandler", function (e) {
console.log(e.data.message); // "hiya"
});
onchange() method
action.onchange(data, name, handler)
action.onchange(data, handler)
action.onchange(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = action.onchange({ message: "ok" }, "myHandler1", function (e) {
console.log(e.data.message); // "ok"
});
oninvoke() method
action.oninvoke(data, name, handler)
action.oninvoke(data, handler)
action.oninvoke(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = action.oninvoke(function (e) {
console.log(e.returnValue); // value returned from invoke
});
onparamadd() method
action.onparamadd(data, name, handler)
action.onparamadd(data, handler)
action.onparamadd(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = action.onparamadd(function (e) {
console.log(e.value.name()); // value == action param that was added
});
onparamchange() method
action.onparamchange(data, name, handler)
action.onparamchange(data, handler)
action.onparamchange(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = action.onparamchange(function (e) {
console.log(e.property); // property that was changed
});
onparamremove() method
action.onparamremove(data, name, handler)
action.onparamremove(data, handler)
action.onparamremove(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = action.onparamremove(function (e) {
console.log(e.value); // name of action param that was removed
});
param() property Svidget.ObjectCollection
action.param(name)
action.param(index)
- name string - The name of the param.
- index number - The index of the param.
var myParam1 = action.param("myWidget");
var myParam1 = action.param(2);
params() property Svidget.ObjectCollection
action.params()
removeParam() method
action.removeParam(name)
- name string - The action param name to remove.
Svidget.Action Events
- change - Triggers when one the action properties has changed.
- invoke - Triggers when the action is invoked.
- paramadd - Triggers when an action param is added to the action.
- paramchange - Triggers when any of the action params have changed.
- paramremove - Triggers when an action param is removed from the action.
Svidget.ActionProxy class Page
Represents a proxy to a Svidget.Action instance contained within the widget. The members are proxies to members of the underlying object, so the same documentation applies.
Svidget.ActionParam class
Represents an param for an action. This object corresponds to a <svidget:actionparam> element in the widget, contained within an action. Note that action params are used primarily for discovery. A consumer of your widget may find it useful what params the action expects.
defvalue() property
actionparam.defvalue()
var param = svidget.$.param("color");
var defvalue = param.defvalue();
var curvalue = param.value();
alert("Has it changed? = " + defvalue !== curvalue);
description() property string
actionparam.description()
actionparam.description(desc)
var param = svidget.$.action("myAction").param(0);
document.getElementById("mydesc").innerHTML = param.description();
param.description("New description for action param 0.");
name() property string
actionparam.name()
off() method
actionparam.off(type, handler)
actionparam.off(type, name)
- type string - The event type. See Svidget.Param Events for the list of types.
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
offchange() method
actionparam.offchange(handler)
actionparam.offchange(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
on() method
actionparam.on(type, data, name, handler)
actionparam.on(type, data, handler)
actionparam.on(type, handler)
- type string - The event type. See Svidget.Param Events for the list of types.
- data object - An arbitrary value to pass to the Event object when the event is triggered.
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
actionparam.on("change", { message: "something changed!" }, "myHandler", function (e) {
console.log(e.data.message); // "something changed!"
});
onchange() method
actionparam.onchange(data, name, handler)
actionparam.onchange(data, handler)
actionparam.onchange(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = actionparam.onchange({ message: "ok" }, "myHandler1", function (e) {
console.log(e.data.message); // "ok"
});
subtype() property string
actionparam.subtype()
actionparam.subtype(val)
var param = svidget.$.action("myAction").addParam("backgroundColor", "transparent", { type: "string" });
var isOk = param.subtype("color");
console.log(isOk); // true
isOk = param.subtype("somethingElse");
console.log(isOk); // false
type() property string
actionparam.type()
actionparam.type(val)
var param = svidget.$.action("myAction").addParam("backgroundColor", "transparent", { type: "string" });
console.log(param.type()); // "string"
var isOk = param.type("object");
console.log(isOk); // true
isOk = param.type("myMadeUpType");
console.log(isOk); // false
Svidget.ActionParam Events
- change - Triggers when one the action param properties has changed.
Svidget.ActionParamProxy class Page
Represents a proxy to a Svidget.ActionParam instance contained within a widget action. The members are proxies to members of the underlying object, so the same documentation applies.
Svidget.Collection class Page
Represents a collection of items. Extends JavaScript Array.
Svidget.Event class Page
An event object that is passed to the handler when any event is triggered. Note: This object is patterned closely with jQuery's Event object implementation.
currentTarget property object
event.currentTarget
svidget.$.param("myParam").onchange(handleParamChange);
svidget.$.onparamchange(handleWidgetParamChange);
function handleParamChange(e) {
console.log(e.currentTarget.toString()); // [Svidget.Param]
}
function handleWidgetParamChange(e) {
console.log(e.currentTarget.toString()); // [Svidget.Widget]
}
data property object
event.data
svidget.$.param("myParam").onchange(handleParamChange, { my: "data" });
function handleParamChange(e) {
console.log(e.data.my); // data
}
isImmediatePropagationStopped() method bool
Note: if this is true, then isPropagationStopped() will also return true.
event.isImmediatePropagationStopped()
isPropagationStopped() method bool
event.isPropagationStopped()
name property string
event.name
svidget.$.param("myParam").onchange(handleParamChange, 3, "handler1");
function handleParamChange(e) {
console.log(e.data); // 3
console.log(e.name); // handler1
}
stopImmediatePropagation() method void
event.stopImmediatePropagation()
svidget.$.param("myParam").onchange(handleParamChange);
svidget.$.param("myParam").onchange(handleParamChange2);
svidget.$.onparamchange(handleWidgetParamChange);
function handleParamChange(e) {
console.log("handleParamChange");
e.stopImmediatePropagation();
}
// this is never called because immediate propagation was stopped
function handleParamChange2(e) {
console.log("handleParamChange2");
}
// this is never called because immediate propagation and propagation were stopped
function handleWidgetParamChange(e) {
console.log("handleWidgetParamChange");
}
stopPropagation() method void
event.stopPropagation()
svidget.$.param("myParam").onchange(handleParamChange);
svidget.$.onparamchange(handleWidgetParamChange);
function handleParamChange(e) {
console.log("handleParamChange");
e.stopPropagation();
}
// this is never called because propagation was stopped
function handleWidgetParamChange(e) {
console.log("handleWidgetParamChange");
}
target property string
event.target
svidget.$.param("myParam").onchange(handleParamChange);
svidget.$.onparamchange(handleWidgetParamChange);
function handleParamChange(e) {
console.log(e.target.toString()); // [Svidget.Param]
}
function handleWidgetParamChange(e) {
console.log(e.target.toString()); // [Svidget.Param]
}
timeStamp property string
event.timeStamp
svidget.$.param("myParam").onchange(handleParamChange);
function handleParamChange(e) {
var timeDiff = +(new Date()) - e.timeStamp;
console.log("Millisecs elapsed since event was triggered = " + timeDiff);
}
type property string
event.type
svidget.$.param("myParam").onchange(handleParamChange);
function handleParamChange(e) {
console.log(e.type); // change
}
value property string
event.value
svidget.$.param("myParam").onchange(handleParamChange);
function handleParamChange(e) {
var changeObj = e.value; // change events always have this object layout: { property: "propName", value: val }
console.log(changeObj.property); // enabled
console.log(changeObj.value); // true (note: this is the new value)
}
Svidget.EventDesc class
Represents an event for the widget. This object corresponds to a <svidget:event> element in the widget.
Svidget.EventDescProxy class Page
Represents a proxy to a Svidget.EventDesc instance contained within the widget. The members are proxies to members of the underlying object, so the same documentation applies.
Svidget.ObjectCollection class Page
Represents a collection of object types used by the widget. Extends Svidget.Collection.
Svidget.Param class
Represents a param for a widget. This object corresponds to a <svidget:param> element in the widget.
binding() property string
param.binding()
param.binding(bindingStr)
var param = svidget.$.param("backColor");
param.binding("rect@stroke"); // map param to the stroke attribute of every rect element in the document
coerce() property bool
param.coerce()
param.coerce(val)
var param = svidget.$.param("data");
param.type("object");
param.coerce(true);
param.value("{ firstName: 'Joe', lastName: 'Pesci' }");
console.log(param.value()); // [object Object]
// ok, no more coersion
param.coerce(false);
param.value("{ firstName: 'Joe', lastName: 'Pesci' }");
console.log(param.value()); // "{ firstName: 'Joe', lastName: 'Pesci' }"
defvalue() property
param.defvalue()
var param = svidget.$.param("color");
var defvalue = param.defvalue();
var curvalue = param.value();
alert("Has it changed? = " + defvalue !== curvalue);
description() property string
param.description()
param.description(desc)
var param = svidget.$.param("data");
document.getElementById("mydesc").innerHTML = param.description();
param.description("Updated description");
enabled() property bool
param.enabled()
param.enabled(isEnabled)
var param = svidget.$.param("invokeMe");
if (!v.enabled()) param.enabled(true);
name() property string
param.name()
off() method
param.off(type, handler)
param.off(type, name)
- type string - The event type. See Svidget.Param Events for the list of types.
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
offchange() method
param.offchange(handler)
param.offchange(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
offset() method
param.offset(handler)
param.offset(name)
- handler function - The handler function that was used to register the function.
- name string - The handler name that was used to register the function.
on() method
param.on(type, data, name, handler)
param.on(type, data, handler)
param.on(type, handler)
- type string - The event type. See Svidget.Param Events for the list of types.
- data object - An arbitrary value to pass to the Event object when the event is triggered.
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
param.on("set", { message: "something changed!" }, "myHandler", function (e) {
console.log(e.data.message); // "something changed!"
});
onchange() method
param.onchange(data, name, handler)
param.onchange(data, handler)
param.onchange(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = param.onchange({ message: "ok" }, "myHandler1", function (e) {
console.log(e.data.message); // "ok"
});
onset() method
param.onset(data, name, handler)
param.onset(data, handler)
param.onset(handler)
- data object - An arbitrary value to pass to the Event object when the event is triggered
- name string - The name of the handler. This can be used to unregister the handler by name.
- handler function - The handler function that is invoked when the event is triggered.
var added = param.onset(function (e) {
console.log(e.value); // new value for param
});
sanitizer() property function
Note: When coerce is true the value is coerced prior to being sanitized.
param.sanitizer()
param.sanitizer(globalFuncName)
param.sanitizer(func)
var colorParam = svidget.$.param("color");
colorParam.sanitizer(function (param, val) {
if (!/^#(?:[0-9a-fA-F]{3}){1,2}$/.test(val)) return "#000";
return val;
});
colorParam.value("NotReallyAColor");
console.log(colorParam.value()); // "#000"
shortname() property string
param.shortname()
param.shortname(sn)
var param = svidget.$.param("data");
document.getElementById("mysn").innerHTML = param.shortname();
param.shortname("d2");
subtype() property string
param.subtype()
param.subtype(val)
var param = svidget.$.addParam("backgroundColor", "transparent", { type: "string" });
var isOk = param.subtype("color");
console.log(isOk); // true
isOk = param.subtype("somethingElse");
console.log(isOk); // false
type() property string
Note: Changing the type will NOT coerce the value when coerce is true. Work around this by setting the value to its current value.
param.type()
param.type(val)
var param = svidget.$.addParam("backgroundColor", "transparent", { type: "string" });
console.log(param.type()); // "string"
var isOk = param.type("object");
console.log(isOk); // true
isOk = param.type("myMadeUpType");
console.log(isOk); // false
typedata() property string
param.typedata()
param.typedata(td)
var amParam = svidget.$.addParam("fruit", "none", { type: "string", subtype: "choice" });
amParam.value("banana");
console.log(amParam.value()); // "none", because it's the only choice in the list
amParam.typedata("banana|pear|grape|watermelon|mango");
amParam.value("banana");
console.log(amParam.value()); // "banana"
value() property any
param.value()
param.value(val)
var ratioParam = svidget.$.addParam("ratio", 0, { type: "number" });
ratioParam.value(0.5);
console.log(ratioParam.value()); // 0.5
Svidget.Param Events
- change - Triggers when one the param properties has changed.
- set - Triggers when the param value has changed.
Svidget.ParamProxy class Page
Represents a proxy to a Svidget.Param instance contained within the widget. The members are proxies to members of the underlying object, so the same documentation applies.
Svidget.Proxy class Page
Provides base class functionality for all Proxy classes in the framework.
Svidget.Widget class
Represents the widget object.
Svidget.WidgetReference class Page
Represents a reference to a widget from a page.
Xml Reference
<svg>
Describes custom Svidget attributes for the <svg> root element.
Attributes
- xmlns:svidget string - The svidget namespace. Required. Must be set to http://www.svidget.org/svidget.
- svidget:version string - The version of svidget.js that the widget is targeted. This is mostly for informational purposes.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:svidget="http://www.svidget.org/svidget"
svidget:version="0.2.0"
width="200" height="200"
>
<svidget:actions>
Contains <svidget:action> elements.
Attributes
- onadd string - The name of the global function handling the addaction event when adding actions to the widget.
- onremove string - The name of the global function handling the removeaction event when removing actions from the widget.
<svidget:actions>
<svidget:action name="spin" external="true" binding="spin" description="Spins the star." />
<svidget:action name="stopSpin" external="true" binding="stopSpin" description="Stops any currently running spin animation." />
</svidget:actions>
<svidget:action>
Defines an Svidget.Action for the widget.
Attributes
- name string - The action name. Required, and must be unique among all actions.
- binding string - The global function name to invoke when the action is invoked.
- description string - The action param name to add.
- external bool - Whether the action is external.
- onchange string - The name of the global function handling the change event.
- oninvoke string - The name of the global function handling the invoke event.
- onparamadd string - The name of the global function handling the paramadd event.
- onparamchange string - The name of the global function handling the paramchange event.
- onparamremove string - The name of the global function handling the paramremove event.
<svidget:action
name="spin" external="true" binding="spin" description="Spins the star."
onchange="spinChange" oninvoke="spinInvoke"
onparamchange="spinParamChange" onparamadd="spinParamAdd" onparamremove="spinParamRemove"
/>
<svidget:actionparams>
Contains <svidget:actionparam> elements.
<svidget:actionparam>
Defines an Svidget.ActionParam contained within an Svidget.Action for the widget.
<svidget:events>
Contains <svidget:event> elements.
<svidget:event>
Defines an Svidget.EventDesc for the widget.
<svidget:params>
Contains <svidget:param> elements.
<svidget:param>
Defines an Svidget.Param for the widget.
<object> Page
Loads a widget onto an HTML page.
Attributes
- role string - Overrides the ARIA attribute to tell the framework that this object tag is specifically a Svidget.js widget. Required. Must be set to svidget.
- data-connected bool - Whether the widget is connected to the page. When false, the widget is is standalone mode, and no communication between the page and the widget will take place after the widget is initialized. Default is true.
- data-crossdomain bool - A hint that indicates that the widget is hosted on another domain. Set this to true if you know that your widget is hosted on another domain to speed up loading, or if you prefer your widget to be hosted in an <iframe>. Default is false.
<object id="someWidget" role="svidget" data="widgets/donut.svg" data-connected="true" data-crossdomain="false"
type="image/svg+xml" width="200" height="200">
<param name="data" value="0.55" />
<param name="color" value="#da3333" />
<param name="backColor" value="#ffac33" />
<param name="textColor" value="#da3333" />
</object>