MCPcopy Index your code
hub / github.com/clips/pattern / widget

Function widget

pattern/canvas.js:3482–3576  ·  view source on GitHub ↗
(canvas, variable, type, options)

Source from the content-addressed store, hash-verified

3480
3481// function widget(canvas, variable, type, {parent: null, value: null, min: 0, max: 1, step: 0.01, index: 1, callback: function(e){}})
3482function widget(canvas, variable, type, options) {
3483 /* Creates a widget linked to the given canvas.
3484 * The type of the widget can be STRING or NUMBER (field), BOOLEAN (checkbox),
3485 * RANGE (slider), LIST (dropdown list), or FUNCTION (button).
3486 * The value of the widget can be retrieved as canvas.variables[variable] (or canvas.variables.variable).
3487 * Optionally, a default value can be given.
3488 * For lists, this is an array and optionally an index of the selected value.
3489 * For sliders, you can also set min, max and step.
3490 * For functions, an optional callback(event){} must be given.
3491 * To get at the current canvas from inside the callback, use event.target.canvas.
3492 * Use event.target.canvas.focus() if drawing occurs in the callback.
3493 */
3494 var v = variable;
3495 var o = options || {};
3496 if (canvas.variables[v] === undefined) {
3497 var parent = (o && o.parent)? o.parent : $(canvas.id + "_widgets");
3498 if (!parent) {
3499 // No widget container is given, or exists.
3500 // Insert a <div id="[canvas.id]_widgets" class="widgets"> after the <canvas> element.
3501 parent = document.createElement("div");
3502 parent.id = (canvas.id + "_widgets");
3503 parent.className = "widgets";
3504 canvas.element.parentNode.insertBefore(parent, canvas.element.nextSibling);
3505 }
3506 // Create <input> element with id [canvas.id]_[variable].
3507 // Create an onchange() that will set the variable to the value of the widget.
3508 // For FUNCTION, it is an onclick() that will call options.callback(e).
3509 var id = canvas.id + "_" + v;
3510 // Fix callback event and event.target on IE8 and Safari2.
3511 // Function does nothing when propagate=false.
3512 var cb = function(e, propagate) {
3513 if (!e)
3514 e = window.event;
3515 if (!e.target)
3516 e.target = e.srcElement || document;
3517 if (e.target.nodeType === 3)
3518 e.target = e.target.parentNode;
3519 if (o.callback && propagate != false)
3520 o.callback(e);
3521 }
3522 // <input type="text" id="id" value="" />
3523 if (type == STRING || type == TEXT) {
3524 var s = "<input type='text' id='"+v+"' value='"+(o.value||"").replace(/'/g,"&#39;")+"' />";
3525 var f = function(e,p) { canvas.variables[this.id] = this.value; cb(e,p); };
3526 // <input type="text" id="id" value="0" />
3527 } else if (type == NUMBER) {
3528 var s = "<input type='text' id='"+v+"' value='"+(o.value||0)+"' />";
3529 var f = function(e,p) { canvas.variables[this.id] = parseFloat(this.value); cb(e,p); };
3530 // <input type="checkbox" id="variable" />
3531 } else if (type == BOOLEAN) {
3532 var s = "<input type='checkbox' id='"+v+"'"+((o.value==true)?" checked":"")+" />";
3533 var f = function(e,p) { canvas.variables[this.id] = this.checked; cb(e,p); };
3534 // <input type="range" id="id" value="0" min="0" max="0" step="0.01" />
3535 } else if (type == RANGE) {
3536 var s = "<input type='range' id='"+v+"' value='"+(o.value||0)+"'"
3537 + " min='"+(o.min||0)+"' max='"+(o.max||1)+"' step='"+(o.step||0.01)+"' />";
3538 var f = function(e,p) { canvas.variables[this.id] = parseFloat(this.value); cb(e,p); };
3539 // <select id="id"><option value="value[i]">value[i]</option>...</select>

Callers 1

canvas.jsFile · 0.85

Calls 4

$Function · 0.85
cbFunction · 0.85
attachEventFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…