From a5cde5f11ba4d773bd7b369596f5bb00a0e1666e Mon Sep 17 00:00:00 2001 From: Joakim Reinert Date: Fri, 1 May 2015 23:50:38 +0200 Subject: Extract tablesorter js and encapsulate --- data/js/application.js | 12 +++++++- data/js/script.js | 63 -------------------------------------- data/js/tablesorter.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 64 deletions(-) create mode 100644 data/js/tablesorter.js diff --git a/data/js/application.js b/data/js/application.js index 9db8d8398..dedefbc3b 100644 --- a/data/js/application.js +++ b/data/js/application.js @@ -8,9 +8,18 @@ define( 'tabwidth-input', 'thumbnail-view', 'uploader', + 'tablesorter', 'vendor' ], - function (require, Util, LexerInput, TabwidthInput, ThumbnailView, Uploader) { + function ( + require, + Util, + LexerInput, + TabwidthInput, + ThumbnailView, + Uploader, + TableSorter + ) { require(['script']); var App = { // Gets called for every request (before page load) @@ -29,6 +38,7 @@ define( LexerInput.initialize(); ThumbnailView.initialize(); Uploader.initialize(); + TableSorter.initialize(); this.configureTooltips(); this.setupToggleSelectAllEvent(); this.setupLineWrapToggle(); diff --git a/data/js/script.js b/data/js/script.js index b9f196fab..7b103699a 100644 --- a/data/js/script.js +++ b/data/js/script.js @@ -1,68 +1,5 @@ (function($) { $(function() { - if (typeof $.tablesorter !== 'undefined') { - // source: https://projects.archlinux.org/archweb.git/tree/sitestatic/archweb.js - $.tablesorter.addParser({ - id: 'filesize', - re: /^(\d+(?:\.\d+)?)(bytes?|[KMGTPEZY]i?B|B)$/, - is: function(s) { - return this.re.test(s); - }, - format: function(s) { - var matches = this.re.exec(s); - if (!matches) { - return 0; - } - var size = parseFloat(matches[1]), - suffix = matches[2]; - - switch(suffix) { - /* intentional fall-through at each level */ - case 'YB': - case 'YiB': - size *= 1024; - case 'ZB': - case 'ZiB': - size *= 1024; - case 'EB': - case 'EiB': - size *= 1024; - case 'PB': - case 'PiB': - size *= 1024; - case 'TB': - case 'TiB': - size *= 1024; - case 'GB': - case 'GiB': - size *= 1024; - case 'MB': - case 'MiB': - size *= 1024; - case 'KB': - case 'KiB': - size *= 1024; - } - return size; - }, - type: 'numeric' - }); - - $(".tablesorter").tablesorter({ - textExtraction: function(node) { - var attr = $(node).attr('data-sort-value'); - if (typeof attr !== 'undefined' && attr !== false) { - var intAttr = parseInt(attr); - if (!isNaN(intAttr)) { - return intAttr; - } - return attr; - } - return $(node).text(); - } - }); - } - if ($("img.lazyload").length) { $("img.lazyload").show().lazyload({treshold: 200}); } diff --git a/data/js/tablesorter.js b/data/js/tablesorter.js new file mode 100644 index 000000000..d500f2ade --- /dev/null +++ b/data/js/tablesorter.js @@ -0,0 +1,82 @@ +(function () { +'use strict'; +define(['jquery', 'jquery.tablesorter'], function ($) { + var PrivateFunctions = { + setupParser: function () { + // source: https://projects.archlinux.org/archweb.git/tree/sitestatic/archweb.js + $.tablesorter.addParser({ + id: 'filesize', + re: /^(\d+(?:\.\d+)?)(bytes?|[KMGTPEZY]i?B|B)$/, + is: function(s) { + return this.re.test(s); + }, + format: function(s) { + var matches = this.re.exec(s); + if (!matches) { + return 0; + } + var size = parseFloat(matches[1]), + suffix = matches[2]; + + switch(suffix) { + /* intentional fall-through at each level */ + case 'YB': + case 'YiB': + size *= 1024; + case 'ZB': + case 'ZiB': + size *= 1024; + case 'EB': + case 'EiB': + size *= 1024; + case 'PB': + case 'PiB': + size *= 1024; + case 'TB': + case 'TiB': + size *= 1024; + case 'GB': + case 'GiB': + size *= 1024; + case 'MB': + case 'MiB': + size *= 1024; + case 'KB': + case 'KiB': + size *= 1024; + } + return size; + }, + type: 'numeric' + }); + }, + + textExtraction: function (node) { + var attr = $(node).attr('data-sort-value'); + if (!_.isUndefined(attr) && attr !== false) { + var intAttr = parseInt(attr); + if (!_.isNaN(intAttr)) { + return intAttr; + } + return attr; + } + return $(node).text(); + }, + + setupTableSorter: function () { + $(".tablesorter").tablesorter({ + textExtraction: this.textExtraction + }); + } + }; + + var TableSorter = { + initialize: function () { + PrivateFunctions.setupParser(); + PrivateFunctions.setupTableSorter(); + } + }; + + return TableSorter; +}); +})(); -- cgit v1.2.3-24-g4f1b