Deeper FileInfo refactoring.

This commit is contained in:
Nicolas Stepien 2012-02-29 23:49:01 +01:00
parent 23766e8ef9
commit 27a40c46cf
2 changed files with 64 additions and 63 deletions

View File

@ -2320,16 +2320,18 @@
return $.id('backlinkPreview').textContent = conf['backlink'].replace(/%id/, '123456789'); return $.id('backlinkPreview').textContent = conf['backlink'].replace(/%id/, '123456789');
}, },
fileInfo: function() { fileInfo: function() {
FileInfo.type = this.name === 'fileInfoR' ? 0 : 1; var type;
type = this.name === 'fileInfoR' ? 0 : 1;
FileInfo.data = { FileInfo.data = {
link: '<a href="javascript:;">1329791824.png</a>', link: '<a href="javascript:;">1329791824.png</a>',
size: 996, size: 996,
unit: 'KB', unit: 'KB',
resolution: '1366x768', resolution: '1366x768',
filename: 'Untitled.png' filename: 'Untitled.png',
type: type
}; };
FileInfo.funks = FileInfo.setFormats(); FileInfo.setFormats();
return $.id("" + this.name + "Preview").innerHTML = FileInfo.funks[FileInfo.type](FileInfo); return $.id("" + this.name + "Preview").innerHTML = FileInfo.funks[type](FileInfo);
}, },
favicon: function() { favicon: function() {
Favicon["switch"](); Favicon["switch"]();
@ -2931,42 +2933,46 @@
FileInfo = { FileInfo = {
init: function() { init: function() {
if (g.BOARD === 'f') return; if (g.BOARD === 'f') return;
FileInfo.funks = FileInfo.setFormats(); this.setFormats();
return g.callbacks.push(this.node); return g.callbacks.push(this.node);
}, },
node: function(root) { node: function(root) {
var filename, link, node, resolution, size, unit, _, _ref; var filename, link, node, regexp, resolution, size, type, unit, _, _ref;
if (root.className === 'inline' || !(node = $('.filesize', root))) return; if (root.className === 'inline' || !(node = $('.filesize', root))) return;
FileInfo.type = node.childElementCount === 2 ? 0 : 1; type = node.childElementCount === 2 ? 0 : 1;
_ref = node.innerHTML.match(FileInfo.regexp[FileInfo.type]), _ = _ref[0], link = _ref[1], size = _ref[2], unit = _ref[3], resolution = _ref[4], filename = _ref[5]; regexp = [/File:\s(<a.+<\/a>)-\((?:Spoiler Image,\s)?([\d\.]+)\s([BKM]{1,2}),\s(\d+x\d+|PDF),\s<span\stitle=\"([^\"]+)\">/, /File:\s(<a.+<\/a>)-\((?:Spoiler Image,\s)?([\d\.]+)\s([BKM]{1,2}),\s(\d+x\d+|PDF)\)/][type];
_ref = node.innerHTML.match(regexp), _ = _ref[0], link = _ref[1], size = _ref[2], unit = _ref[3], resolution = _ref[4], filename = _ref[5];
FileInfo.data = { FileInfo.data = {
link: link, link: link,
size: size, size: size,
unit: unit, unit: unit,
resolution: resolution, resolution: resolution,
filename: filename filename: filename,
type: type
}; };
return node.innerHTML = FileInfo.funks[FileInfo.type](FileInfo); return node.innerHTML = FileInfo.funks[type](FileInfo);
}, },
setFormats: function() { setFormats: function() {
var code, i, _results; var code, format, funks, i, param;
_results = []; funks = [];
for (i = 0; i <= 1; i++) { for (i = 0; i <= 1; i++) {
code = conf[FileInfo.conf[i]].replace(FileInfo.param[i], function(s, c) { format = conf[['fileInfoR', 'fileInfoT'][i]];
param = [/%([BKlLMnNrs])/g, /%([BKlMrs])/g][i];
code = format.replace(param, function(s, c) {
if (c in FileInfo.formatters) { if (c in FileInfo.formatters) {
return "' + FileInfo.formatters." + c + "() + '"; return "' + f.formatters." + c + "() + '";
} else { } else {
return s; return s;
} }
}); });
_results.push(Function('FileInfo', "return '" + code + "'")); funks.push(Function('f', "return '" + code + "'"));
} }
return _results; return this.funks = funks;
}, },
convertUnit: function(unitT) { convertUnit: function(unitT) {
var i, size, unitF, units; var i, size, unitF, units;
size = FileInfo.data.size; size = this.data.size;
unitF = FileInfo.data.unit; unitF = this.data.unit;
if (unitF !== unitT) { if (unitF !== unitT) {
units = ['B', 'KB', 'MB']; units = ['B', 'KB', 'MB'];
i = units.indexOf(unitF) - units.indexOf(unitT); i = units.indexOf(unitF) - units.indexOf(unitT);
@ -2986,9 +2992,6 @@
} }
return "" + size + " " + unitT; return "" + size + " " + unitT;
}, },
conf: ['fileInfoR', 'fileInfoT'],
param: [/%([BKlLMnNrs])/g, /%([BKlMrs])/g],
regexp: [/File:\s(<a.+<\/a>)-\((?:Spoiler Image,\s)?([\d\.]+)\s([BKM]{1,2}),\s(\d+x\d+|PDF),\s<span\stitle=\"([^\"]+)\">/, /File:\s(<a.+<\/a>)-\((?:Spoiler Image,\s)?([\d\.]+)\s([BKM]{1,2}),\s(\d+x\d+|PDF)\)/],
formatters: { formatters: {
B: function() { B: function() {
return FileInfo.convertUnit('B'); return FileInfo.convertUnit('B');
@ -2996,9 +2999,15 @@
K: function() { K: function() {
return FileInfo.convertUnit('KB'); return FileInfo.convertUnit('KB');
}, },
M: function() {
return FileInfo.convertUnit('MB');
},
s: function() {
return "" + FileInfo.data.size + " " + FileInfo.data.unit;
},
l: function() { l: function() {
if (FileInfo.type === 0) { if (FileInfo.data.type === 0) {
return FileInfo.data.link.replace(/>\d+\.\w+</, '>' + FileInfo.formatters.n() + '<'); return FileInfo.data.link.replace(/>\d+\.\w+</, '>' + this.n() + '<');
} else { } else {
return FileInfo.data.link; return FileInfo.data.link;
} }
@ -3006,13 +3015,10 @@
L: function() { L: function() {
return FileInfo.data.link.replace(/>\d+\.\w+</, '>' + FileInfo.data.filename + '<'); return FileInfo.data.link.replace(/>\d+\.\w+</, '>' + FileInfo.data.filename + '<');
}, },
M: function() {
return FileInfo.convertUnit('MB');
},
n: function() { n: function() {
var ext; var ext;
if ((ext = FileInfo.data.filename.lastIndexOf('.')) > 38) { if ((ext = FileInfo.data.filename.lastIndexOf('.')) > 38) {
return '<span class=fnfull>' + FileInfo.data.filename + '</span><span class=fntrunc>' + FileInfo.data.filename.substr(0, 32) + ' (...)' + FileInfo.data.filename.substr(ext) + '</span>'; return "<span class=fnfull>" + FileInfo.data.filename + "</span><span class=fntrunc>" + (FileInfo.data.filename.substr(0, 32)) + "(...)" + (FileInfo.data.filename.substr(ext)) + "</span>";
} else { } else {
return FileInfo.data.filename; return FileInfo.data.filename;
} }
@ -3022,9 +3028,6 @@
}, },
r: function() { r: function() {
return FileInfo.data.resolution; return FileInfo.data.resolution;
},
s: function() {
return "" + FileInfo.data.size + " " + FileInfo.data.unit;
} }
} }
}; };

View File

@ -1904,15 +1904,16 @@ options =
backlink: -> backlink: ->
$.id('backlinkPreview').textContent = conf['backlink'].replace /%id/, '123456789' $.id('backlinkPreview').textContent = conf['backlink'].replace /%id/, '123456789'
fileInfo: -> fileInfo: ->
FileInfo.type = if @name is 'fileInfoR' then 0 else 1 type = if @name is 'fileInfoR' then 0 else 1
FileInfo.data = FileInfo.data =
link : '<a href="javascript:;">1329791824.png</a>' link: '<a href="javascript:;">1329791824.png</a>'
size : 996 size: 996
unit : 'KB' unit: 'KB'
resolution: '1366x768' resolution: '1366x768'
filename : 'Untitled.png' filename: 'Untitled.png'
FileInfo.funks = FileInfo.setFormats() type: type
$.id("#{@name}Preview").innerHTML = FileInfo.funks[FileInfo.type] FileInfo FileInfo.setFormats()
$.id("#{@name}Preview").innerHTML = FileInfo.funks[type] FileInfo
favicon: -> favicon: ->
Favicon.switch() Favicon.switch()
unread.update true unread.update true
@ -2398,31 +2399,40 @@ Time =
FileInfo = FileInfo =
init: -> init: ->
return if g.BOARD is 'f' return if g.BOARD is 'f'
FileInfo.funks = FileInfo.setFormats() @setFormats()
g.callbacks.push @node g.callbacks.push @node
node: (root) -> node: (root) ->
return if root.className is 'inline' or not node = $ '.filesize', root return if root.className is 'inline' or not node = $ '.filesize', root
FileInfo.type = if node.childElementCount is 2 then 0 else 1 type = if node.childElementCount is 2 then 0 else 1
regexp = [
/File:\s(<a.+<\/a>)-\((?:Spoiler Image,\s)?([\d\.]+)\s([BKM]{1,2}),\s(\d+x\d+|PDF),\s<span\stitle=\"([^\"]+)\">/
/File:\s(<a.+<\/a>)-\((?:Spoiler Image,\s)?([\d\.]+)\s([BKM]{1,2}),\s(\d+x\d+|PDF)\)/
][type]
[_, link, size, unit, resolution, filename] = [_, link, size, unit, resolution, filename] =
node.innerHTML.match FileInfo.regexp[FileInfo.type] node.innerHTML.match regexp
FileInfo.data = FileInfo.data =
link: link link: link
size: size size: size
unit: unit unit: unit
resolution: resolution resolution: resolution
filename: filename filename: filename
node.innerHTML = FileInfo.funks[FileInfo.type] FileInfo type: type
node.innerHTML = FileInfo.funks[type] FileInfo
setFormats: -> setFormats: ->
funks = []
for i in [0..1] for i in [0..1]
code = conf[FileInfo.conf[i]].replace FileInfo.param[i], (s, c) -> format = conf[['fileInfoR', 'fileInfoT'][i]]
param = [/%([BKlLMnNrs])/g, /%([BKlMrs])/g][i]
code = format.replace param, (s, c) ->
if c of FileInfo.formatters if c of FileInfo.formatters
"' + FileInfo.formatters.#{c}() + '" "' + f.formatters.#{c}() + '"
else else
s s
Function 'FileInfo', "return '#{code}'" funks.push Function 'f', "return '#{code}'"
@funks = funks
convertUnit: (unitT) -> convertUnit: (unitT) ->
size = FileInfo.data.size size = @data.size
unitF = FileInfo.data.unit unitF = @data.unit
if unitF isnt unitT if unitF isnt unitT
units = ['B', 'KB', 'MB'] units = ['B', 'KB', 'MB']
i = units.indexOf(unitF) - units.indexOf unitT i = units.indexOf(unitF) - units.indexOf unitT
@ -2434,36 +2444,24 @@ FileInfo =
if size < 1 and size.toString().length > size.toFixed(2).length if size < 1 and size.toString().length > size.toFixed(2).length
size = size.toFixed 2 size = size.toFixed 2
"#{size} #{unitT}" "#{size} #{unitT}"
conf: [
'fileInfoR'
'fileInfoT'
]
param: [
/%([BKlLMnNrs])/g
/%([BKlMrs])/g
]
regexp: [
/File:\s(<a.+<\/a>)-\((?:Spoiler Image,\s)?([\d\.]+)\s([BKM]{1,2}),\s(\d+x\d+|PDF),\s<span\stitle=\"([^\"]+)\">/
/File:\s(<a.+<\/a>)-\((?:Spoiler Image,\s)?([\d\.]+)\s([BKM]{1,2}),\s(\d+x\d+|PDF)\)/
]
formatters: formatters:
B: -> FileInfo.convertUnit 'B' B: -> FileInfo.convertUnit 'B'
K: -> FileInfo.convertUnit 'KB' K: -> FileInfo.convertUnit 'KB'
M: -> FileInfo.convertUnit 'MB'
s: -> "#{FileInfo.data.size} #{FileInfo.data.unit}"
l: -> l: ->
if FileInfo.type is 0 if FileInfo.data.type is 0
FileInfo.data.link.replace />\d+\.\w+</, '>' + FileInfo.formatters.n() + '<' FileInfo.data.link.replace />\d+\.\w+</, '>' + @n() + '<'
else else
FileInfo.data.link FileInfo.data.link
L: -> FileInfo.data.link.replace />\d+\.\w+</, '>' + FileInfo.data.filename + '<' L: -> FileInfo.data.link.replace />\d+\.\w+</, '>' + FileInfo.data.filename + '<'
M: -> FileInfo.convertUnit 'MB'
n: -> n: ->
if (ext = FileInfo.data.filename.lastIndexOf '.') > 38 if (ext = FileInfo.data.filename.lastIndexOf '.') > 38
'<span class=fnfull>' + FileInfo.data.filename + '</span><span class=fntrunc>' + FileInfo.data.filename.substr(0, 32) + ' (...)' + FileInfo.data.filename.substr(ext) + '</span>' "<span class=fnfull>#{FileInfo.data.filename}</span><span class=fntrunc>#{FileInfo.data.filename.substr 0, 32}(...)#{FileInfo.data.filename.substr ext}</span>"
else else
FileInfo.data.filename FileInfo.data.filename
N: -> FileInfo.data.filename N: -> FileInfo.data.filename
r: -> FileInfo.data.resolution r: -> FileInfo.data.resolution
s: -> "#{FileInfo.data.size} #{FileInfo.data.unit}"
getTitle = (thread) -> getTitle = (thread) ->
el = $ '.filetitle', thread el = $ '.filetitle', thread