mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-25 20:44:53 -04:00
Refactor JS.
This commit is contained in:
parent
922f83357f
commit
2caaccabc0
8 changed files with 48 additions and 72 deletions
|
@ -1,10 +1,12 @@
|
||||||
|
|
||||||
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 win = window,
|
||||||
fallbackHref = settings.appHref + 'client/images/fallback/',
|
appHref = settings.appHref,
|
||||||
themesHref = settings.appHref + 'client/themes/',
|
imagesHref = appHref + 'client/images/',
|
||||||
scriptsHref = settings.appHref + 'client/js/',
|
fallbackHref = appHref + 'client/images/fallback/',
|
||||||
|
themesHref = appHref + 'client/themes/',
|
||||||
|
scriptsHref = appHref + 'client/js/',
|
||||||
fallbacks = ['file', 'folder', 'folder-page', 'folder-parent', 'ar', 'aud', 'bin', 'img', 'txt', 'vid'],
|
fallbacks = ['file', 'folder', 'folder-page', 'folder-parent', 'ar', 'aud', 'bin', 'img', 'txt', 'vid'],
|
||||||
|
|
||||||
image = function (id) {
|
image = function (id) {
|
||||||
|
@ -43,38 +45,22 @@ modulejs.define('core/resource', ['_', '$', 'config', 'core/settings'], function
|
||||||
|
|
||||||
loadScriptGlob = function (filename, globalId, callback) {
|
loadScriptGlob = function (filename, globalId, callback) {
|
||||||
|
|
||||||
if (window[globalId]) {
|
if (win[globalId]) {
|
||||||
callback(window[globalId]);
|
callback(win[globalId]);
|
||||||
} else {
|
} else {
|
||||||
loadScript(filename, function () { callback(window[globalId]); });
|
loadScript(filename, function () { callback(win[globalId]); });
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
ensureQRCode = function (callback) {
|
|
||||||
|
|
||||||
if ($.fn.qrcode) {
|
|
||||||
callback();
|
|
||||||
} else {
|
|
||||||
loadScript('qrcode.js', callback);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
ensureSH = function (callback) {
|
ensureSH = function (callback) {
|
||||||
|
|
||||||
loadScriptGlob('syntaxhighlighter.js', 'SyntaxHighlighter', callback);
|
loadScriptGlob('syntaxhighlighter.js', 'SyntaxHighlighter', callback);
|
||||||
},
|
|
||||||
|
|
||||||
ensureMarkdown = function (callback) {
|
|
||||||
|
|
||||||
loadScriptGlob('markdown.js', 'markdown', callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
image: image,
|
image: image,
|
||||||
icon: icon,
|
icon: icon,
|
||||||
ensureMarkdown: ensureMarkdown,
|
|
||||||
ensureQRCode: ensureQRCode,
|
|
||||||
ensureSH: ensureSH
|
ensureSH: ensureSH
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
|
|
||||||
modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server', 'core/event', 'core/resource'], function (_, $, allsettings, server, event, resource) {
|
modulejs.define('ext/custom', ['_', '$', 'markdown', 'core/settings', 'core/server', 'core/event', 'core/resource'], function (_, $, markdown, allsettings, server, event, resource) {
|
||||||
|
|
||||||
var settings = _.extend({
|
var settings = _.extend({
|
||||||
enabled: false
|
enabled: false
|
||||||
}, allsettings.custom),
|
}, allsettings.custom),
|
||||||
|
|
||||||
|
$header, $footer,
|
||||||
|
duration = 200,
|
||||||
|
|
||||||
onLocationChanged = function (item) {
|
onLocationChanged = function (item) {
|
||||||
|
|
||||||
server.request({action: 'get', custom: true, customHref: item.absHref}, function (response) {
|
server.request({action: 'get', custom: true, customHref: item.absHref}, function (response) {
|
||||||
|
@ -12,35 +15,32 @@ 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.ensureMarkdown(function (md) {
|
data = response.custom;
|
||||||
|
|
||||||
data = response.custom;
|
if (data.header) {
|
||||||
|
content = data.header;
|
||||||
if (data.header) {
|
if (data.header_type === 'md') {
|
||||||
content = data.header;
|
content = markdown.toHTML(content);
|
||||||
if (md && data.header_type === 'md') {
|
|
||||||
content = md.toHTML(content);
|
|
||||||
}
|
|
||||||
$('#content-header').html(content).stop().slideDown(200);
|
|
||||||
has_header = true;
|
|
||||||
}
|
}
|
||||||
|
$header.html(content).stop().slideDown(duration);
|
||||||
|
has_header = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (data.footer) {
|
if (data.footer) {
|
||||||
content = data.footer;
|
content = data.footer;
|
||||||
if (md && data.footer_type === 'md') {
|
if (data.footer_type === 'md') {
|
||||||
content = md.toHTML(content);
|
content = markdown.toHTML(content);
|
||||||
}
|
|
||||||
$('#content-footer').html(content).stop().slideDown(200);
|
|
||||||
has_footer = true;
|
|
||||||
}
|
}
|
||||||
});
|
$footer.html(content).stop().slideDown(duration);
|
||||||
|
has_footer = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!has_header) {
|
if (!has_header) {
|
||||||
$('#content-header').stop().slideUp(200);
|
$header.stop().slideUp(duration);
|
||||||
}
|
}
|
||||||
if (!has_footer) {
|
if (!has_footer) {
|
||||||
$('#content-footer').stop().slideUp(200);
|
$footer.stop().slideUp(duration);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -51,8 +51,8 @@ modulejs.define('ext/custom', ['_', '$', 'core/settings', 'core/server', 'core/e
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$('<div id="content-header"/>').hide().prependTo('#content');
|
$header = $('<div id="content-header"/>').hide().prependTo('#content');
|
||||||
$('<div id="content-footer"/>').hide().appendTo('#content');
|
$footer = $('<div id="content-footer"/>').hide().appendTo('#content');
|
||||||
|
|
||||||
event.sub('location.changed', onLocationChanged);
|
event.sub('location.changed', onLocationChanged);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'core/resource', 'ext/preview'], function (_, $, allsettings, event, resource, preview) {
|
modulejs.define('ext/preview-txt', ['_', '$', 'markdown', 'core/settings', 'core/event', 'core/resource', 'ext/preview'], function (_, $, markdown, allsettings, event, resource, preview) {
|
||||||
|
|
||||||
var settings = _.extend({
|
var settings = _.extend({
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
@ -106,14 +106,7 @@ modulejs.define('ext/preview-txt', ['_', '$', 'core/settings', 'core/event', 'co
|
||||||
|
|
||||||
} else if (settings.types[currentItem.type] === 'markdown') {
|
} else if (settings.types[currentItem.type] === 'markdown') {
|
||||||
|
|
||||||
$text = $(templateMarkdown).text(textContent);
|
$text = $(templateMarkdown).html(markdown.toHTML(textContent));
|
||||||
|
|
||||||
resource.ensureMarkdown(function (md) {
|
|
||||||
|
|
||||||
if (md) {
|
|
||||||
$text.html(md.toHTML(textContent));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$text = $(templateText).text(textContent);
|
$text = $(templateText).text(textContent);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
modulejs.define('ext/qrcode', ['_', '$', 'modernizr', 'core/settings', 'core/event', 'core/resource'], function (_, $, modernizr, allsettings, event, resource) {
|
modulejs.define('ext/qrcode', ['_', '$', 'modernizr', 'core/settings', 'core/event'], function (_, $, modernizr, allsettings, event) {
|
||||||
|
|
||||||
var settings = _.extend({
|
var settings = _.extend({
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
@ -12,15 +12,13 @@ modulejs.define('ext/qrcode', ['_', '$', 'modernizr', 'core/settings', 'core/eve
|
||||||
|
|
||||||
update = function (item) {
|
update = function (item) {
|
||||||
|
|
||||||
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,
|
height: settings.size,
|
||||||
height: settings.size,
|
color: '#333',
|
||||||
color: '#333',
|
bgColor: '#fff',
|
||||||
bgColor: '#fff',
|
text: window.location.protocol + '//' + window.location.host + item.absHref
|
||||||
text: window.location.protocol + '//' + window.location.host + item.absHref
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,9 @@ modulejs.define('view/ensure', ['$', 'core/event'], function ($, event) {
|
||||||
ensure = function () {
|
ensure = function () {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
$(selr).filter(isVisible).length != 1 ||
|
$(selr).filter(isVisible).length !== 1 ||
|
||||||
$(sela).filter(isVisible).length != 1 ||
|
$(sela).filter(isVisible).length !== 1 ||
|
||||||
$(selr).text() != sequence
|
$(selr).text() !== sequence
|
||||||
) {
|
) {
|
||||||
$(selr).remove();
|
$(selr).remove();
|
||||||
$('<span><a/></span>')
|
$('<span><a/></span>')
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
|
|
||||||
// @include "lib/markdown-*.js"
|
|
|
@ -1,2 +0,0 @@
|
||||||
|
|
||||||
// @include "lib/jquery.qrcode-*.js"
|
|
|
@ -4,6 +4,7 @@
|
||||||
// @include "lib/modernizr-*.js"
|
// @include "lib/modernizr-*.js"
|
||||||
if (window.attachEvent && !window.addEventListener) { throw 'no-browser'; }
|
if (window.attachEvent && !window.addEventListener) { throw 'no-browser'; }
|
||||||
// @include "lib/underscore-*.js"
|
// @include "lib/underscore-*.js"
|
||||||
|
// @include "lib/markdown-*.js"
|
||||||
// @include "lib/modulejs-*.js"
|
// @include "lib/modulejs-*.js"
|
||||||
// @include "lib/moment-*.js"
|
// @include "lib/moment-*.js"
|
||||||
// @include "lib/json2-*.js"
|
// @include "lib/json2-*.js"
|
||||||
|
@ -13,6 +14,7 @@ if (window.attachEvent && !window.addEventListener) { throw 'no-browser'; }
|
||||||
// @include "lib/jquery-*.js"
|
// @include "lib/jquery-*.js"
|
||||||
// @include "lib/jquery.fracs-*.js"
|
// @include "lib/jquery.fracs-*.js"
|
||||||
// @include "lib/jquery.mousewheel-*.js"
|
// @include "lib/jquery.mousewheel-*.js"
|
||||||
|
// @include "lib/jquery.qrcode-*.js"
|
||||||
// @include "lib/jquery.scrollpanel-*.js"
|
// @include "lib/jquery.scrollpanel-*.js"
|
||||||
|
|
||||||
// app
|
// app
|
||||||
|
@ -22,6 +24,7 @@ if (window.attachEvent && !window.addEventListener) { throw 'no-browser'; }
|
||||||
|
|
||||||
/*global jQuery, Modernizr, moment, _ */
|
/*global jQuery, Modernizr, moment, _ */
|
||||||
modulejs.define('$', function () { return jQuery; });
|
modulejs.define('$', function () { return jQuery; });
|
||||||
|
modulejs.define('markdown', function () { return markdown; });
|
||||||
modulejs.define('modernizr', function () { return Modernizr; });
|
modulejs.define('modernizr', function () { return Modernizr; });
|
||||||
modulejs.define('moment', function () { return moment; });
|
modulejs.define('moment', function () { return moment; });
|
||||||
modulejs.define('_', function () { return _; });
|
modulejs.define('_', function () { return _; });
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue