More refactorings.

This commit is contained in:
Lars Jung 2014-06-08 22:26:03 +02:00
parent 9c414c92e2
commit 67bdb1515d
4 changed files with 33 additions and 37 deletions

View file

@ -1,5 +1,5 @@
modulejs.define('core/resource', ['_', 'config', 'core/settings'], function (_, config, settings) { modulejs.define('core/resource', ['_', '$', 'config', 'core/settings'], function (_, $, config, settings) {
var imagesHref = settings.appHref + 'client/images/', var imagesHref = settings.appHref + 'client/images/',
fallbackHref = settings.appHref + 'client/images/fallback/', fallbackHref = settings.appHref + 'client/images/fallback/',
@ -32,37 +32,49 @@ modulejs.define('core/resource', ['_', 'config', 'core/settings'], function (_,
return fallbackHref + 'file.svg'; return fallbackHref + 'file.svg';
}, },
loadScript = function (url, globalId, callback) { loadScript = function (filename, callback) {
$.ajax({
url: scriptsHref + filename,
dataType: 'script',
complete: function () { callback(); }
});
},
loadScriptGlob = function (filename, globalId, callback) {
if (window[globalId]) { if (window[globalId]) {
callback(window[globalId]); callback(window[globalId]);
} else { } else {
$.ajax({ loadScript(filename, function () { callback(window[globalId]); });
url: url,
dataType: 'script',
complete: function () {
callback(window[globalId]);
}
});
} }
}, },
loadSyntaxhighlighter = function (callback) { ensureQRCode = function (callback) {
loadScript(scriptsHref + 'syntaxhighlighter.js', 'SyntaxHighlighter', callback); if ($.fn.qrcode) {
callback();
} else {
loadScript('qrcode.js', callback);
}
}, },
loadMarkdown = function (callback) { ensureSH = function (callback) {
loadScript(scriptsHref + 'markdown.js', 'markdown', callback); loadScriptGlob('syntaxhighlighter.js', 'SyntaxHighlighter', callback);
},
ensureMarkdown = function (callback) {
loadScriptGlob('markdown.js', 'markdown', callback);
}; };
return { return {
image: image, image: image,
icon: icon, icon: icon,
loadSyntaxhighlighter: loadSyntaxhighlighter, ensureMarkdown: ensureMarkdown,
loadMarkdown: loadMarkdown ensureQRCode: ensureQRCode,
ensureSH: ensureSH,
}; };
}); });

View file

@ -12,7 +12,7 @@ modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server', 'core/e
var has_header, has_footer, data, content; var has_header, has_footer, data, content;
if (response) { if (response) {
resource.loadMarkdown(function (md) { resource.ensureMarkdown(function (md) {
data = response.custom; data = response.custom;

View file

@ -108,7 +108,7 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'co
$text = $(templateMarkdown).text(textContent); $text = $(templateMarkdown).text(textContent);
resource.loadMarkdown(function (md) { resource.ensureMarkdown(function (md) {
if (md) { if (md) {
$text.html(md.toHTML(textContent)); $text.html(md.toHTML(textContent));
@ -118,7 +118,7 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'co
$text = $(templateText).text(textContent); $text = $(templateText).text(textContent);
resource.loadSyntaxhighlighter(function (sh) { resource.ensureSH(function (sh) {
if (sh) { if (sh) {
var $table = $('<table/>'); var $table = $('<table/>');

View file

@ -1,5 +1,5 @@
modulejs.define('ext/qrcode', ['_', '$', 'modernizr', 'core/settings', 'core/event'], function (_, $, modernizr, allsettings, event) { modulejs.define('ext/qrcode', ['_', '$', 'modernizr', 'core/settings', 'core/event', 'core/resource'], function (_, $, modernizr, allsettings, event, resource) {
var settings = _.extend({ var settings = _.extend({
enabled: false, enabled: false,
@ -10,25 +10,9 @@ modulejs.define('ext/qrcode', ['_', '$', 'modernizr', 'core/settings', 'core/eve
$qrcode, hideTimeoutId, $qrcode, hideTimeoutId,
loadQrCodeExtension = function (callback) {
if ($.fn.qrcode) {
callback();
} else {
$.ajax({
url: allsettings.appHref + 'client/js/qrcode.js',
dataType: 'script',
complete: function () {
callback();
}
});
}
},
update = function (item) { update = function (item) {
loadQrCodeExtension(function () { resource.ensureQRCode(function () {
$qrcode.empty().qrcode({ $qrcode.empty().qrcode({
render: modernizr.canvas ? 'canvas' : 'div', render: modernizr.canvas ? 'canvas' : 'div',
width: settings.size, width: settings.size,