Skip to content

Commit

Permalink
Allowing for templated urls in iFrame (#460)
Browse files Browse the repository at this point in the history
* Allowing for templated urls in iFrame

This can allow for passing {{ width }} and {{ height }} as dynamic
attributes in the iFrame's URL.

The new method Slice.render_template method could do more eventually
exposing more variables to be used in dynamic strings.

* Passing function references

* js linting
  • Loading branch information
mistercrunch committed May 12, 2016
1 parent 6c333d5 commit aa6e6bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions caravel/assets/javascripts/modules/caravel.js
@@ -1,6 +1,7 @@
var $ = require('jquery');
var jQuery = $;
var d3 = require('d3');
var Mustache = require('mustache');

// vis sources
var sourceMap = {
Expand Down Expand Up @@ -215,6 +216,13 @@ var px = (function () {
getWidgetHeader: function () {
return this.container.parents("li.widget").find(".chart-header");
},
render_template: function (s) {
var context = {
width: this.width,
height: this.height
};
return Mustache.render(s, context);
},
jsonEndpoint: function () {
var parser = document.createElement('a');
parser.href = data.json_endpoint;
Expand Down
1 change: 1 addition & 0 deletions caravel/assets/package.json
Expand Up @@ -60,6 +60,7 @@
"jquery-ui": "^1.10.5",
"less": "^2.6.1",
"less-loader": "^2.2.2",
"mustache": "^2.2.1",
"nvd3": "1.8.2",
"react": "^0.14.7",
"react-bootstrap": "^0.28.3",
Expand Down
3 changes: 2 additions & 1 deletion caravel/assets/visualizations/iframe.js
Expand Up @@ -5,10 +5,11 @@ function iframeWidget(slice) {
function refresh() {
$('#code').attr('rows', '15');
$.getJSON(slice.jsonEndpoint(), function (payload) {
var url = slice.render_template(payload.form_data.url);
slice.container.html('<iframe style="width:100%;"></iframe>');
var iframe = slice.container.find('iframe');
iframe.css('height', slice.height());
iframe.attr('src', payload.form_data.url);
iframe.attr('src', url);
slice.done();
})
.fail(function (xhr) {
Expand Down
7 changes: 6 additions & 1 deletion caravel/forms.py
Expand Up @@ -420,7 +420,12 @@ def __init__(self, viz):
default=default_metric,
choices=datasource.metrics_combo),
'url': TextField(
'URL', default='https://www.youtube.com/embed/JkI5rg_VcQ4',),
'URL',
description=(
"The URL, this field is templated, so you can integrate "
"{{ width }} and/or {{ height }} in your URL string."
),
default='https://www.youtube.com/embed/JkI5rg_VcQ4',),
'where': TextField(
'Custom WHERE clause', default='',
description=(
Expand Down

0 comments on commit aa6e6bd

Please sign in to comment.