browser-module-sandbox
Advanced tools
Comparing version 0.1.0 to 0.1.1
52
index.js
var inherits = require('inherits') | ||
var iframe = require('iframe') | ||
var editor = require('javascript-editor') | ||
var events = require('events') | ||
var extend = require('extend') | ||
var toolbar = require('toolbar') | ||
var elementClass = require('element-class') | ||
var request = require('browser-request') | ||
@@ -17,50 +14,21 @@ | ||
if (!opts) opts = {} | ||
this.defaultCode = opts.defaultCode || "var url = require('url')\n" | ||
this.outputEl = opts.output || document.body | ||
this.editorEl = opts.editor || document.body | ||
this.container = opts.container || document.body | ||
this.iframeHead = opts.iframeHead || "" | ||
this.iframeBody = opts.iframeBody || "" | ||
this.snuggieAPI = opts.snuggieAPI || window.location.protocol + '//' + window.location.host | ||
this.showControls(opts.controls) | ||
var defaultEditorOptions = { | ||
value: this.defaultCode, | ||
container: this.editorEl | ||
} | ||
var editorOptions = extend({}, defaultEditorOptions, opts.codemirrorOptions || {}) | ||
this.editor = editor(editorOptions) | ||
this.iframe = iframe({ container: this.outputEl, scrollingDisabled: true }) | ||
this.iframe = iframe({ container: this.container, scrollingDisabled: true }) | ||
this.iframeStyle = "<style type='text/css'> html, body { margin: 0; padding: 0; border: 0; } </style>" | ||
self.on('output', function() { | ||
if (typeof game !== "undefined") game = undefined | ||
self.emit('bundleStart') | ||
var body = self.editor.editor.getValue() | ||
request({method: "POST", body: body, url: this.snuggieAPI, json: true}, function(err, resp, json) { | ||
// setTimeout is because iframes report inaccurate window.innerWidth/innerHeight, even after DOMContentLoaded! | ||
var body = self.iframeBody + '<script type="text/javascript"> setTimeout(function(){' + json.bundle + '}, 0)</script>' | ||
self.iframe.setHTML({ head: self.iframeHead + self.iframeStyle, body: body }) | ||
self.emit('bundleEnd') | ||
}) | ||
}) | ||
} | ||
Sandbox.prototype.showControls = function(container) { | ||
Sandbox.prototype.bundle = function(body) { | ||
var self = this | ||
this.controls = toolbar({el: container, noKeydown: true}) | ||
this.controls.on('select', function(item) { | ||
var className = '#' + item | ||
var el = document.querySelector(className) | ||
if (el === self.outputEl) { | ||
elementClass(self.outputEl).remove('hidden') | ||
elementClass(self.editorEl).add('hidden') | ||
self.emit('output') | ||
} | ||
if (el === self.editorEl) { | ||
if (!self.editorEl.className.match(/hidden/)) return | ||
elementClass(self.editorEl).remove('hidden') | ||
elementClass(self.outputEl).add('hidden') | ||
self.emit('edit') | ||
} | ||
}) | ||
self.emit('bundleStart') | ||
request({method: "POST", body: body, url: this.snuggieAPI, json: true}, function(err, resp, json) { | ||
// setTimeout is because iframes report inaccurate window.innerWidth/innerHeight, even after DOMContentLoaded! | ||
var body = self.iframeBody + '<script type="text/javascript"> setTimeout(function(){' + json.bundle + '}, 0)</script>' | ||
self.iframe.setHTML({ head: self.iframeHead + self.iframeStyle, body: body }) | ||
self.emit('bundleEnd') | ||
}) | ||
} | ||
inherits(Sandbox, events.EventEmitter) |
{ | ||
"name": "browser-module-sandbox", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "browser editor for code that gets 'compiled' on the server with node and run on the client", | ||
@@ -10,6 +10,3 @@ "repository": { | ||
"dependencies": { | ||
"javascript-editor": "0.0.1", | ||
"iframe": "0.0.2", | ||
"toolbar": "0.0.4", | ||
"element-class": "0.0.1", | ||
"browser-request": "0.2.0", | ||
@@ -16,0 +13,0 @@ "inherits": "1.0.0", |
4
197871
4529
- Removedelement-class@0.0.1
- Removedjavascript-editor@0.0.1
- Removedtoolbar@0.0.4
- Removedelement-class@0.0.1(transitive)
- Removedjavascript-editor@0.0.1(transitive)
- Removedtoolbar@0.0.4(transitive)