1(function() { 2 // If window.HTMLWidgets is already defined, then use it; otherwise create a 3 // new object. This allows preceding code to set options that affect the 4 // initialization process (though none currently exist). 5 window.HTMLWidgets = window.HTMLWidgets || {}; 6 7 // See if we're running in a viewer pane. If not, we're in a web browser. 8 var viewerMode = window.HTMLWidgets.viewerMode = 9 /\bviewer_pane=1\b/.test(window.location); 10 11 // See if we're running in Shiny mode. If not, it's a static document. 12 // Note that static widgets can appear in both Shiny and static modes, but 13 // obviously, Shiny widgets can only appear in Shiny apps/documents. 14 var shinyMode = window.HTMLWidgets.shinyMode = 15 typeof(window.Shiny) !== "undefined" && !!window.Shiny.outputBindings; 16 17 // We can't count on jQuery being available, so we implement our own 18 // version if necessary. 19 function querySelectorAll(scope, selector) { 20 if (typeof(jQuery) !== "undefined" && scope instanceof jQuery) { 21 return scope.find(selector); 22 } 23 if (scope.querySelectorAll) { 24 return scope.querySelectorAll(selector); 25 } 26 } 27 28 function asArray(value) { 29 if (value === null) 30 return []; 31 if ($.isArray(value)) 32 return value; 33 return [value]; 34 } 35 36 // Implement jQuery's extend 37 function extend(target /*, ... */) { 38 if (arguments.length == 1) { 39 return target; 40 } 41 for (var i = 1; i < arguments.length; i++) { 42 var source = arguments[i]; 43 for (var prop in source) { 44 if (source.hasOwnProperty(prop)) { 45 target[prop] = source[prop]; 46 } 47 } 48 } 49 return target; 50 } 51 52 // IE8 doesn't support Array.forEach. 53 function forEach(values, callback, thisArg) { 54 if (values.forEach) { 55 values.forEach(callback, thisArg); 56 } else { 57 for (var i = 0; i < values.length; i++) { 58 callback.call(thisArg, values[i], i, values); 59 } 60 } 61 } 62 63 // Replaces the specified method with the return value of funcSource. 64 // 65 // Note that funcSource should not BE the new method, it should be a function 66 // that RETURNS the new method. funcSource receives a single argument that is 67 // the overridden method, it can be called from the new method. The overridden 68 // method can be called like a regular function, it has the target permanently 69 // bound to it so "this" will work correctly. 70 function overrideMethod(target, methodName, funcSource) { 71 var superFunc = target[methodName] || function() {}; 72 var superFuncBound = function() { 73 return superFunc.apply(target, arguments); 74 }; 75 target[methodName] = funcSource(superFuncBound); 76 } 77 78 // Add a method to delegator that, when invoked, calls 79 // delegatee.methodName. If there is no such method on 80 // the delegatee, but there was one on delegator before 81 // delegateMethod was called, then the original version 82 // is invoked instead. 83 // For example: 84 // 85 // var a = { 86 // method1: function() { console.log('a1'); } 87 // method2: function() { console.log('a2'); } 88 // }; 89 // var b = { 90 // method1: function() { console.log('b1'); } 91 // }; 92 // delegateMethod(a, b, "method1"); 93 // delegateMethod(a, b, "method2"); 94 // a.method1(); 95 // a.method2(); 96 // 97 // The output would be "b1", "a2". 98 function delegateMethod(delegator, delegatee, methodName) { 99 var inherited = delegator[methodName]; 100 delegator[methodName] = function() { 101 var target = delegatee; 102 var method = delegatee[methodName]; 103 104 // The method doesn't exist on the delegatee. Instead, 105 // call the method on the delegator, if it exists. 106 if (!method) { 107 target = delegator; 108 method = inherited; 109 } 110 111 if (method) { 112 return method.apply(target, arguments); 113 } 114 }; 115 } 116 117 // Implement a vague facsimilie of jQuery's data method 118 function elementData(el, name, value) { 119 if (arguments.length == 2) { 120 return el["htmlwidget_data_" + name]; 121 } else if (arguments.length == 3) { 122 el["htmlwidget_data_" + name] = value; 123 return el; 124 } else { 125 throw new Error("Wrong number of arguments for elementData: " + 126 arguments.length); 127 } 128 } 129 130 // http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex 131 function escapeRegExp(str) { 132 return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); 133 } 134 135 function hasClass(el, className) { 136 var re = new RegExp("\\b" + escapeRegExp(className) + "\\b"); 137 return re.test(el.className); 138 } 139 140 // elements - array (or array-like object) of HTML elements
141 // className - class name to test for 142 // include - if true, only return elements with given className; 143 // if false, only return elements *without* given className 144 function filterByClass(elements, className, include) { 145 var results = []; 146 for (var i = 0; i < elements.length; i++) { 147 if (hasClass(elements[i], className) == include) 148 results.push(elements[i]); 149 } 150 return results; 151 } 152 153 function on(obj, eventName, func) { 154 if (obj.addEventListener) { 155 obj.addEventListener(eventName, func, false); 156 } else if (obj.attachEvent) { 157 obj.attachEvent(eventName, func); 158 } 159 } 160 161 function off(obj, eventName, func) { 162 if (obj.removeEventListener) 163 obj.removeEventListener(eventName, func, false); 164 else if (obj.detachEvent) { 165 obj.detachEvent(eventName, func); 166 } 167 } 168 169 // Translate array of values to top/right/bottom/left, as usual with 170 // the "padding" CSS property 171 // https://developer.mozilla.org/en-US/docs/Web/CSS/padding 172 function unpackPadding(value) { 173 if (typeof(value) === "number") 174 value = [value]; 175 if (value.length === 1) { 176 return {top: value[0], right: value[0], bottom: value[0], left: value[0]}; 177 } 178 if (value.length === 2) { 179 return {top: value[0], right: value[1], bottom: value[0], left: value[1]}; 180 } 181 if (value.length === 3) { 182 return {top: value[0], right: value[1], bottom: value[2], left: value[1]}; 183 } 184 if (value.length === 4) { 185 return {top: value[0], right: value[1], bottom: value[2], left: value[3]}; 186 } 187 } 188 189 // Convert an unpacked padding object to a CSS value 190 function paddingToCss(paddingObj) { 191 return paddingObj.top + "px " + paddingObj.right + "px " + paddingObj.bottom + "px " + paddingObj.left + "px"; 192 } 193 194 // Makes a number suitable for CSS 195 function px(x) { 196 if (typeof(x) === "number") 197 return x + "px"; 198 else 199 return x; 200 } 201 202 // Retrieves runtime widget sizing information for an element. 203 // The return value is either null, or an object with fill, padding, 204 // defaultWidth, defaultHeight fields. 205 function sizingPolicy(el) { 206 var sizingEl = document.querySelector("script[data-for='" + el.id + "'][type='application/htmlwidget-sizing']"); 207 if (!sizingEl) 208 return null; 209 var sp = JSON.parse(sizingEl.textContent || sizingEl.text || "{}"); 210 if (viewerMode) { 211 return sp.viewer; 212 } else { 213 return sp.browser; 214 } 215 } 216 217 // @param tasks Array of strings (or falsy value, in which case no-op). 218 // Each element must be a valid JavaScript expression that yields a 219 // function. 220 // @param target The object that will be "this" for each function 221 // execution. 222 // @param args Array of arguments to be passed to the functions. (The 223 // same arguments will be passed to all functions.) 224 function evalAndRun(tasks, target, args) { 225 if (tasks) { 226 forEach(tasks, function(task) { 227 var taskFunc = eval("(" + task + ")"); 228 if (typeof(taskFunc) !== "function") { 229 throw new Error("Task must be a function! Source:\n" + task); 230 } 231 taskFunc.apply(target, args); 232 }); 233 } 234 } 235 236 function initSizing(el) { 237 var sizing = sizingPolicy(el); 238 if (!sizing) 239 return; 240 241 var cel = document.getElementById("htmlwidget_container"); 242 if (!cel) 243 return; 244 245 if (typeof(sizing.padding) !== "undefined") { 246 document.body.style.margin = "0"; 247 document.body.style.padding = paddingToCss(unpackPadding(sizing.padding)); 248 } 249 250 if (sizing.fill) { 251 document.body.style.overflow = "hidden"; 252 document.body.style.width = "100%"; 253 document.body.style.height = "100%"; 254 document.documentElement.style.width = "100%"; 255 document.documentElement.style.height = "100%"; 256 if (cel) { 257 cel.style.position = "absolute"; 258 var pad = unpackPadding(sizing.padding); 259 cel.style.top = pad.top + "px"; 260 cel.style.right = pad.right + "px"; 261 cel.style.bottom = pad.bottom + "px"; 262 cel.style.left = pad.left + "px"; 263 el.style.width = "100%"; 264 el.style.height = "100%"; 265 } 266 267 return { 268 getWidth: function() { return cel.offsetWidth; }, 269 getHeight: function() { return cel.offsetHeight; } 270 }; 271 272 } else { 273 el.style.width = px(sizing.width); 274 el.style.height = px(sizing.height); 275 276 return { 277 getWidth: function() { return el.offsetWidth; }, 278 getHeight: function() { return el.offsetHeight; } 279 }; 280 } 281 } 282 283 // Default implementations for methods 284 var defaults = { 285 find: function(scope) { 286 return querySelectorAll(scope, "." + this.name); 287 }, 288 renderError: function(el, err) { 289 var $el = $(el); 290 291 this.clearError(el); 292 293 // Add all these error classes, as Shiny does 294 var errClass = "shiny-output-error"; 295 if (err.type !== null) { 296 // use the classes of the error condition as CSS class names 297 errClass = errClass + " " + $.map(asArray(err.type), function(type) { 298 return errClass + "-" + type; 299 }).join(" "); 300 } 301 errClass = errClass + " htmlwidgets-error";
302 303 // Is el inline or block? If inline or inline-block, just display:none it 304 // and add an inline error. 305 var display = $el.css("display"); 306 $el.data("restore-display-mode", display); 307 308 if (display === "inline" || display === "inline-block") { 309 $el.hide(); 310 if (err.message !== "") { 311 var errorSpan = $("<span>").addClass(errClass); 312 errorSpan.text(err.message); 313 $el.after(errorSpan); 314 } 315 } else if (display === "block") { 316 // If block, add an error just after the el, set visibility:none on the 317 // el, and position the error to be on top of the el. 318 // Mark it with a unique ID and CSS class so we can remove it later. 319 $el.css("visibility", "hidden"); 320 if (err.message !== "") { 321 var errorDiv = $("<div>").addClass(errClass).css("position", "absolute") 322 .css("top", el.offsetTop) 323 .css("left", el.offsetLeft) 324 // setting width can push out the page size, forcing otherwise 325 // unnecessary scrollbars to appear and making it impossible for 326 // the element to shrink; so use max-width instead 327 .css("maxWidth", el.offsetWidth) 328 .css("height", el.offsetHeight); 329 errorDiv.text(err.message); 330 $el.after(errorDiv); 331 332 // Really dumb way to keep the size/position of the error in sync with 333 // the parent element as the window is resized or whatever. 334 var intId = setInterval(function() { 335 if (!errorDiv[0].parentElement) { 336 clearInterval(intId); 337 return; 338 } 339 errorDiv 340 .css("top", el.offsetTop) 341 .css("left", el.offsetLeft) 342 .css("maxWidth", el.offsetWidth) 343 .css("height", el.offsetHeight); 344 }, 500); 345 } 346 } 347 }, 348 clearError: function(el) { 349 var $el = $(el); 350 var display = $el.data("restore-display-mode"); 351 $el.data("restore-display-mode", null); 352 353 if (display === "inline" || display === "inline-block") { 354 if (display) 355 $el.css("display", display); 356 $(el.nextSibling).filter(".htmlwidgets-error").remove(); 357 } else if (display === "block"){ 358 $el.css("visibility", "inherit"); 359 $(el.nextSibling).filter(".htmlwidgets-error").remove(); 360 } 361 }, 362 sizing: {} 363 }; 364 365 // Called by widget bindings to register a new type of widget. The definition 366 // object can contain the following properties: 367 // - name (required) - A string indicating the binding name, which will be 368 // used by default as the CSS classname to look for. 369 // - initialize (optional) - A function(el) that will be called once per 370 // widget element; if a value is returned, it will be passed as the third 371 // value to renderValue. 372 // - renderValue (required) - A function(el, data, initValue) that will be 373 // called with data. Static contexts will cause this to be called once per 374 // element; Shiny apps will cause this to be called multiple times per 375 // element, as the data changes. 376 window.HTMLWidgets.widget = function(definition) { 377 if (!definition.name) { 378 throw new Error("Widget must have a name"); 379 } 380 if (!definition.type) { 381 throw new Error("Widget must have a type"); 382 } 383 // Currently we only support output widgets 384 if (definition.type !== "output") { 385 throw new Error("Unrecognized widget type '" + definition.type + "'"); 386 } 387 // TODO: Verify that .name is a valid CSS classname 388 389 // Support new-style instance-bound definitions. Old-style class-bound 390 // definitions have one widget "object" per widget per type/class of 391 // widget; the renderValue and resize methods on such widget objects 392 // take el and instance arguments, because the widget object can't 393 // store them. New-style instance-bound definitions have one widget 394 // object per widget instance; the definition that's passed in doesn't 395 // provide renderValue or resize methods at all, just the single method 396 // factory(el, width, height) 397 // which returns an object that has renderValue(x) and resize(w, h). 398 // This enables a far more natural programming style for the widget 399 // author, who can store per-instance state using either OO-style 400 // instance fields or functional-style closure variables (I guess this 401 // is in contrast to what can only be called C-style pseudo-OO which is 402 // what we required before). 403 if (definition.factory) { 404 definition = createLegacyDefinitionAdapter(definition); 405 } 406 407 if (!definition.renderValue) { 408 throw new Error("Widget must have a renderValue function"); 409 } 410 411 // For static rendering (non-Shiny), use a simple widget registration 412 // scheme. We also use this scheme for Shiny apps/documents that also 413 // contain static widgets. 414 window.HTMLWidgets.widgets = window.HTMLWidgets.widgets || []; 415 // Merge defaults into the definition; don't mutate the original definition. 416 var staticBinding = extend({}, defaults, definition); 417 overrideMethod(staticBinding, "find", function(superfunc) { 418 return function(scope) { 419 var results = superfunc(scope); 420 // Filter out Shiny outputs, we only want the static kind 421 return filterByClass(results, "html-widget-output", false); 422 }; 423 }); 424 window.HTMLWidgets.widgets.push(staticBinding); 425 426 if (shinyMode) { 427 // Shiny is running. Register the definition with an output binding. 428 // The definition itself will not be the output binding, instead 429 // we will make an output binding object that delegates to the 430 // definition. This is because we foolishly used the same method 431 // name (renderValue) for htmlwidgets definition and Shiny bindings 432 // but they actually have quite different semantics (the Shiny 433 // bindings receive data that includes lots of metadata that it 434 // strips off before calling htmlwidgets renderValue). We can't 435 // just ignore the difference because in some widgets it's helpful 436 // to call this.renderValue() from inside of resize(), and if 437 // we're not delegating, then that call will go to the Shiny 438 // version instead of the htmlwidgets version. 439 440 // Merge defaults with definition, without mutating either. 441 var bindingDef = extend({}, defaults, definition); 442 443 // This object will be our actual Shiny binding. 444 var shinyBinding = new Shiny.OutputBinding(); 445
446 // With a few exceptions, we'll want to simply use the bindingDef's 447 // version of methods if they are available, otherwise fall back to 448 // Shiny's defaults. NOTE: If Shiny's output bindings gain additional 449 // methods in the future, and we want them to be overrideable by 450 // HTMLWidget binding definitions, then we'll need to add them to this 451 // list. 452 delegateMethod(shinyBinding, bindingDef, "getId"); 453 delegateMethod(shinyBinding, bindingDef, "onValueChange"); 454 delegateMethod(shinyBinding, bindingDef, "onValueError"); 455 delegateMethod(shinyBinding, bindingDef, "renderError"); 456 delegateMethod(shinyBinding, bindingDef, "clearError"); 457 delegateMethod(shinyBinding, bindingDef, "showProgress"); 458 459 // The find, renderValue, and resize are handled differently, because we 460 // want to actually decorate the behavior of the bindingDef methods. 461 462 shinyBinding.find = function(scope) { 463 var results = bindingDef.find(scope); 464 465 // Only return elements that are Shiny outputs, not static ones 466 var dynamicResults = results.filter(".html-widget-output"); 467 468 // It's possible that whatever caused Shiny to think there might be 469 // new dynamic outputs, also caused there to be new static outputs. 470 // Since there might be lots of different htmlwidgets bindings, we 471 // schedule execution for later--no need to staticRender multiple 472 // times. 473 if (results.length !== dynamicResults.length) 474 scheduleStaticRender(); 475 476 return dynamicResults; 477 }; 478 479 // Wrap renderValue to handle initialization, which unfortunately isn't 480 // supported natively by Shiny at the time of this writing. 481 482 shinyBinding.renderValue = function(el, data) { 483 // Resolve strings marked as javascript literals to objects 484 if (!(data.evals instanceof Array)) data.evals = [data.evals]; 485 for (var i = 0; data.evals && i < data.evals.length; i++) { 486 window.HTMLWidgets.evaluateStringMember(data.x, data.evals[i]); 487 } 488 if (!bindingDef.renderOnNullValue) { 489 if (data.x === null) { 490 el.style.visibility = "hidden"; 491 return; 492 } else { 493 el.style.visibility = "inherit"; 494 } 495 } 496 if (!elementData(el, "initialized")) { 497 initSizing(el); 498 499 elementData(el, "initialized", true); 500 if (bindingDef.initialize) { 501 var result = bindingDef.initialize(el, el.offsetWidth, 502 el.offsetHeight); 503 elementData(el, "init_result", result); 504 } 505 evalAndRun(data.jsHooks.render, elementData(el, "init_result"), [el, data.x]); 506 } 507 Shiny.renderDependencies(data.deps); 508 bindingDef.renderValue(el, data.x, elementData(el, "init_result")); 509 }; 510 511 // Only override resize if bindingDef implements it 512 if (bindingDef.resize) { 513 shinyBinding.resize = function(el, width, height) { 514 // Shiny can call resize before initialize/renderValue have been 515 // called, which doesn't make sense for widgets. 516 if (elementData(el, "initialized")) { 517 bindingDef.resize(el, width, height, elementData(el, "init_result")); 518 } 519 }; 520 } 521 522 Shiny.outputBindings.register(shinyBinding, bindingDef.name); 523 } 524 }; 525 526 var scheduleStaticRenderTimerId = null; 527 function scheduleStaticRender() { 528 if (!scheduleStaticRenderTimerId) { 529 scheduleStaticRenderTimerId = setTimeout(function() { 530 scheduleStaticRenderTimerId = null; 531 window.HTMLWidgets.staticRender(); 532 }, 1); 533 } 534 } 535 536 // Render static widgets after the document finishes loading 537 // Statically render all elements that are of this widget's class 538 window.HTMLWidgets.staticRender = function() { 539 var bindings = window.HTMLWidgets.widgets || []; 540 forEach(bindings, function(binding) { 541 var matches = binding.find(document.documentElement); 542 forEach(matches, function(el) { 543 var sizeObj = initSizing(el, binding); 544 545 if (hasClass(el, "html-widget-static-bound")) 546 return; 547 el.className = el.className + " html-widget-static-bound"; 548 549 var initResult; 550 if (binding.initialize) { 551 initResult = binding.initialize(el, 552 sizeObj ? sizeObj.getWidth() : el.offsetWidth, 553 sizeObj ? sizeObj.getHeight() : el.offsetHeight 554 ); 555 elementData(el, "init_result", initResult); 556 } 557 558 if (binding.resize) { 559 var lastSize = {}; 560 var resizeHandler = function(e) { 561 var size = { 562 w: sizeObj ? sizeObj.getWidth() : el.offsetWidth, 563 h: sizeObj ? sizeObj.getHeight() : el.offsetHeight 564 }; 565 if (size.w === 0 && size.h === 0) 566 return; 567 if (size.w === lastSize.w && size.h === lastSize.h) 568 return; 569 lastSize = size; 570 binding.resize(el, size.w, size.h, initResult); 571 }; 572 573 on(window, "resize", resizeHandler); 574 575 // This is needed for cases where we're running in a Shiny 576 // app, but the widget itself is not a Shiny output, but 577 // rather a simple static widget. One example of this is 578 // an rmarkdown document that has runtime:shiny and widget 579 // that isn't in a render function. Shiny only knows to 580 // call resize handlers for Shiny outputs, not for static 581 // widgets, so we do it ourselves. 582 if (window.jQuery) { 583 window.jQuery(document).on("shown.htmlwidgets shown.bs.tab.htmlwidgets", resizeHandler); 584 window.jQuery(document).on("hidden.htmlwidgets hidden.bs.tab.htmlwidgets", resizeHandler); 585 } 586 587 // This is needed for the specific case of ioslides, which 588 // flips slides between display:none and display:block. 589 // Ideally we would not have to have ioslide-specific code 590 // here, but rather have ioslides raise a generic event, 591 // but the rmarkdown package just went to CRAN so the 592 // window to getting that fixed may be long. 593 if (window.addEventListener) { 594 // It's OK to limit this to window.addEventListener 595 // browsers because ioslides itself only supports 596 // such browsers. 597 on(document, "slideenter", resizeHandler); 598 on(document, "slideleave", resizeHandler); 599 } 600 } 601 602 var scriptData = document.querySelector("script[data-for='" + el.id + "'][type='application/json']"); 603 if (scriptData) { 604 var data = JSON.parse(scriptData.textContent || scriptData.text);
605 // Resolve strings marked as javascript literals to objects 606 if (!(data.evals instanceof Array)) data.evals = [data.evals]; 607 for (var k = 0; data.evals && k < data.evals.length; k++) { 608 window.HTMLWidgets.evaluateStringMember(data.x, data.evals[k]); 609 } 610 binding.renderValue(el, data.x, initResult); 611 evalAndRun(data.jsHooks.render, initResult, [el, data.x]); 612 } 613 }); 614 }); 615 616 invokePostRenderHandlers(); 617 } 618 619 // Wait until after the document has loaded to render the widgets. 620 if (document.addEventListener) { 621 document.addEventListener("DOMContentLoaded", function() { 622 document.removeEventListener("DOMContentLoaded", arguments.callee, false); 623 window.HTMLWidgets.staticRender(); 624 }, false); 625 } else if (document.attachEvent) { 626 document.attachEvent("onreadystatechange", function() { 627 if (document.readyState === "complete") { 628 document.detachEvent("onreadystatechange", arguments.callee); 629 window.HTMLWidgets.staticRender(); 630 } 631 }); 632 } 633 634 635 window.HTMLWidgets.getAttachmentUrl = function(depname, key) { 636 // If no key, default to the first item 637 if (typeof(key) === "undefined") 638 key = 1; 639 640 var link = document.getElementById(depname + "-" + key + "-attachment"); 641 if (!link) { 642 throw new Error("Attachment " + depname + "/" + key + " not found in document"); 643 } 644 return link.getAttribute("href"); 645 }; 646 647 window.HTMLWidgets.dataframeToD3 = function(df) { 648 var names = []; 649 var length; 650 for (var name in df) { 651 if (df.hasOwnProperty(name)) 652 names.push(name); 653 if (typeof(df[name]) !== "object" || typeof(df[name].length) === "undefined") { 654 throw new Error("All fields must be arrays"); 655 } else if (typeof(length) !== "undefined" && length !== df[name].length) { 656 throw new Error("All fields must be arrays of the same length"); 657 } 658 length = df[name].length; 659 } 660 var results = []; 661 var item; 662 for (var row = 0; row < length; row++) { 663 item = {}; 664 for (var col = 0; col < names.length; col++) { 665 item[names[col]] = df[names[col]][row]; 666 } 667 results.push(item); 668 } 669 return results; 670 }; 671 672 window.HTMLWidgets.transposeArray2D = function(array) { 673 if (array.length === 0) return array; 674 var newArray = array[0].map(function(col, i) { 675 return array.map(function(row) { 676 return row[i] 677 }) 678 }); 679 return newArray; 680 }; 681 // Split value at splitChar, but allow splitChar to be escaped 682 // using escapeChar. Any other characters escaped by escapeChar 683 // will be included as usual (including escapeChar itself). 684 function splitWithEscape(value, splitChar, escapeChar) { 685 var results = []; 686 var escapeMode = false; 687 var currentResult = ""; 688 for (var pos = 0; pos < value.length; pos++) { 689 if (!escapeMode) { 690 if (value[pos] === splitChar) { 691 results.push(currentResult); 692 currentResult = ""; 693 } else if (value[pos] === escapeChar) { 694 escapeMode = true; 695 } else { 696 currentResult += value[pos]; 697 } 698 } else { 699 currentResult += value[pos]; 700 escapeMode = false; 701 } 702 } 703 if (currentResult !== "") { 704 results.push(currentResult); 705 } 706 return results; 707 } 708 // Function authored by Yihui/JJ Allaire 709 window.HTMLWidgets.evaluateStringMember = function(o, member) { 710 var parts = splitWithEscape(member, '.', '\\'); 711 for (var i = 0, l = parts.length; i < l; i++) { 712 var part = parts[i]; 713 // part may be a character or 'numeric' member name 714 if (o !== null && typeof o === "object" && part in o) { 715 if (i == (l - 1)) { // if we are at the end of the line then evalulate 716 if (typeof o[part] === "string") 717 o[part] = eval("(" + o[part] + ")"); 718 } else { // otherwise continue to next embedded object 719 o = o[part]; 720 } 721 } 722 } 723 }; 724 725 // Retrieve the HTMLWidget instance (i.e. the return value of an 726 // HTMLWidget binding's initialize() or factory() function) 727 // associated with an element, or null if none. 728 window.HTMLWidgets.getInstance = function(el) { 729 return elementData(el, "init_result"); 730 }; 731 732 // Finds the first element in the scope that matches the selector, 733 // and returns the HTMLWidget instance (i.e. the return value of 734 // an HTMLWidget binding's initialize() or factory() function) 735 // associated with that element, if any. If no element matches the 736 // selector, or the first matching element has no HTMLWidget 737 // instance associated with it, then null is returned. 738 // 739 // The scope argument is optional, and defaults to window.document. 740 window.HTMLWidgets.find = function(scope, selector) { 741 if (arguments.length == 1) { 742 selector = scope; 743 scope = document; 744 } 745 746 var el = scope.querySelector(selector); 747 if (el === null) { 748 return null; 749 } else { 750 return window.HTMLWidgets.getInstance(el); 751 } 752 }; 753 754 // Finds all elements in the scope that match the selector, and 755 // returns the HTMLWidget instances (i.e. the return values of 756 // an HTMLWidget binding's initialize() or factory() function) 757 // associated with the elements, in an array. If elements that 758 // match the selector don't have an associated HTMLWidget 759 // instance, the returned array will contain nulls. 760 // 761 // The scope argument is optional, and defaults to window.document. 762 window.HTMLWidgets.findAll = function(scope, selector) { 763 if (arguments.length == 1) { 764 selector = scope; 765 scope = document; 766 } 767 768 var nodes = scope.querySelectorAll(selector); 769 var results = []; 770 for (var i = 0; i < nodes.length; i++) {
771 results.push(window.HTMLWidgets.getInstance(nodes[i])); 772 } 773 return results; 774 }; 775 776 var postRenderHandlers = []; 777 function invokePostRenderHandlers() { 778 while (postRenderHandlers.length) { 779 var handler = postRenderHandlers.shift(); 780 if (handler) { 781 handler(); 782 } 783 } 784 } 785 786 // Register the given callback function to be invoked after the 787 // next time static widgets are rendered. 788 window.HTMLWidgets.addPostRenderHandler = function(callback) { 789 postRenderHandlers.push(callback); 790 }; 791 792 // Takes a new-style instance-bound definition, and returns an 793 // old-style class-bound definition. This saves us from having 794 // to rewrite all the logic in this file to accomodate both 795 // types of definitions. 796 function createLegacyDefinitionAdapter(defn) { 797 var result = { 798 name: defn.name, 799 type: defn.type, 800 initialize: function(el, width, height) { 801 return defn.factory(el, width, height); 802 }, 803 renderValue: function(el, x, instance) { 804 return instance.renderValue(x); 805 }, 806 resize: function(el, width, height, instance) { 807 return instance.resize(width, height); 808 } 809 }; 810 811 if (defn.find) 812 result.find = defn.find; 813 if (defn.renderError) 814 result.renderError = defn.renderError; 815 if (defn.clearError) 816 result.clearError = defn.clearError; 817 818 return result; 819 } 820})(); 821
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.