mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 03:36:18 +08:00
FIX: Don't load JS via SCRIPT tag in QUnit mode
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
import loadScript from 'discourse/lib/load-script';
|
import loadScript from 'discourse/lib/load-script';
|
||||||
import { observes } from 'ember-addons/ember-computed-decorators';
|
import { observes } from 'ember-addons/ember-computed-decorators';
|
||||||
|
|
||||||
|
const LOAD_ASYNC = !Ember.Test;
|
||||||
|
|
||||||
export default Ember.Component.extend({
|
export default Ember.Component.extend({
|
||||||
mode: 'css',
|
mode: 'css',
|
||||||
classNames: ['ace-wrapper'],
|
classNames: ['ace-wrapper'],
|
||||||
@ -23,7 +25,7 @@ export default Ember.Component.extend({
|
|||||||
|
|
||||||
@observes('mode')
|
@observes('mode')
|
||||||
modeChanged() {
|
modeChanged() {
|
||||||
if (this._editor && !this._skipContentChangeEvent) {
|
if (LOAD_ASYNC && this._editor && !this._skipContentChangeEvent) {
|
||||||
this._editor.getSession().setMode("ace/mode/" + this.get('mode'));
|
this._editor.getSession().setMode("ace/mode/" + this.get('mode'));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -56,10 +58,14 @@ export default Ember.Component.extend({
|
|||||||
if (!this.element || this.isDestroying || this.isDestroyed) { return; }
|
if (!this.element || this.isDestroying || this.isDestroyed) { return; }
|
||||||
const editor = loadedAce.edit(this.$('.ace')[0]);
|
const editor = loadedAce.edit(this.$('.ace')[0]);
|
||||||
|
|
||||||
|
if (LOAD_ASYNC) {
|
||||||
editor.setTheme("ace/theme/chrome");
|
editor.setTheme("ace/theme/chrome");
|
||||||
|
}
|
||||||
editor.setShowPrintMargin(false);
|
editor.setShowPrintMargin(false);
|
||||||
editor.setOptions({fontSize: "14px"});
|
editor.setOptions({fontSize: "14px"});
|
||||||
|
if (LOAD_ASYNC) {
|
||||||
editor.getSession().setMode("ace/mode/" + this.get('mode'));
|
editor.getSession().setMode("ace/mode/" + this.get('mode'));
|
||||||
|
}
|
||||||
editor.on('change', () => {
|
editor.on('change', () => {
|
||||||
this._skipContentChangeEvent = true;
|
this._skipContentChangeEvent = true;
|
||||||
this.set('content', editor.getSession().getValue());
|
this.set('content', editor.getSession().getValue());
|
||||||
|
@ -35,6 +35,11 @@ export default function loadScript(url, opts) {
|
|||||||
|
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
|
|
||||||
|
$('script').each((i, tag) => {
|
||||||
|
_loaded[tag.getAttribute('src')] = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
return new Ember.RSVP.Promise(function(resolve) {
|
return new Ember.RSVP.Promise(function(resolve) {
|
||||||
url = Discourse.getURL(url);
|
url = Discourse.getURL(url);
|
||||||
|
|
||||||
@ -42,7 +47,7 @@ export default function loadScript(url, opts) {
|
|||||||
if (_loaded[url]) { return resolve(); }
|
if (_loaded[url]) { return resolve(); }
|
||||||
if (_loading[url]) { return _loading[url].then(resolve);}
|
if (_loading[url]) { return _loading[url].then(resolve);}
|
||||||
|
|
||||||
var done;
|
let done;
|
||||||
_loading[url] = new Ember.RSVP.Promise(function(_done){
|
_loading[url] = new Ember.RSVP.Promise(function(_done){
|
||||||
done = _done;
|
done = _done;
|
||||||
});
|
});
|
||||||
@ -60,7 +65,7 @@ export default function loadScript(url, opts) {
|
|||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
|
|
||||||
var cdnUrl = url;
|
let cdnUrl = url;
|
||||||
|
|
||||||
// Scripts should always load from CDN
|
// Scripts should always load from CDN
|
||||||
// CSS is type text, to accept it from a CDN we would need to handle CORS
|
// CSS is type text, to accept it from a CDN we would need to handle CORS
|
||||||
@ -72,6 +77,9 @@ export default function loadScript(url, opts) {
|
|||||||
// to dynamically load more JS. In that case, add the `scriptTag: true`
|
// to dynamically load more JS. In that case, add the `scriptTag: true`
|
||||||
// option.
|
// option.
|
||||||
if (opts.scriptTag) {
|
if (opts.scriptTag) {
|
||||||
|
if (Ember.Test) {
|
||||||
|
throw `In test mode scripts cannot be loaded async ${cdnUrl}`;
|
||||||
|
}
|
||||||
loadWithTag(cdnUrl, cb);
|
loadWithTag(cdnUrl, cb);
|
||||||
} else {
|
} else {
|
||||||
ajax({url: cdnUrl, dataType: opts.css ? "text": "script", cache: true}).then(cb);
|
ajax({url: cdnUrl, dataType: opts.css ? "text": "script", cache: true}).then(cb);
|
||||||
|
@ -48,6 +48,7 @@ window.MessageBus.stop();
|
|||||||
|
|
||||||
// Trick JSHint into allow document.write
|
// Trick JSHint into allow document.write
|
||||||
var d = document;
|
var d = document;
|
||||||
|
d.write('<script src="/javascripts/ace/ace.js"></script>');
|
||||||
d.write('<div id="ember-testing-container"><div id="ember-testing"></div></div>');
|
d.write('<div id="ember-testing-container"><div id="ember-testing"></div></div>');
|
||||||
d.write('<style>#ember-testing-container { position: absolute; background: white; bottom: 0; right: 0; width: 640px; height: 384px; overflow: auto; z-index: 9999; border: 1px solid #ccc; } #ember-testing { zoom: 50%; }</style>');
|
d.write('<style>#ember-testing-container { position: absolute; background: white; bottom: 0; right: 0; width: 640px; height: 384px; overflow: auto; z-index: 9999; border: 1px solid #ccc; } #ember-testing { zoom: 50%; }</style>');
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user