Skin: Preload jquery.tablesorter based on rough heuristics
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 8 Mar 2017 04:31:10 +0000 (20:31 -0800)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 8 Mar 2017 04:42:26 +0000 (20:42 -0800)
Whenever a page contains table.sortable, mediawiki/page/ready.js
will lazy-load the module. However this can create a significant
delay and hides the entire prodecure from our module loader.

Instead, do a rough check (similarl to mw-ui-button) and preload
the module based on that.

Bug: T159911
Change-Id: I87600f968241c723e6bead3ef96c34f1021e1164

includes/skins/Skin.php
resources/src/mediawiki/page/ready.js

index 3ef646a..c61f5e9 100644 (file)
@@ -149,6 +149,9 @@ abstract class Skin extends ContextSource {
         * Defines the ResourceLoader modules that should be added to the skin
         * It is recommended that skins wishing to override call parent::getDefaultModules()
         * and substitute out any modules they wish to change by using a key to look them up
+        *
+        * For style modules, use setupSkinUserCss() instead.
+        *
         * @return array Array of modules with helper keys for easy overriding
         */
        public function getDefaultModules() {
@@ -171,6 +174,11 @@ abstract class Skin extends ContextSource {
                        'user' => [],
                ];
 
+               // Preload jquery.tablesorter for mediawiki.page.ready
+               if ( strpos( $out->getHTML(), 'sortable' ) !== false ) {
+                       $modules['content'][] = 'jquery.tablesorter';
+               }
+
                // Add various resources if required
                if ( $wgUseAjax && $wgEnableAPI ) {
                        if ( $wgEnableWriteAPI && $user->isLoggedIn()
index d228f3e..860fcf5 100644 (file)
@@ -12,7 +12,7 @@
        }
 
        mw.hook( 'wikipage.content' ).add( function ( $content ) {
-               var $sortableTables;
+               var $sortable;
 
                // Run jquery.placeholder polyfill if placeholder is not supported
                if ( !supportsPlaceholder ) {
                // Run jquery.makeCollapsible
                $content.find( '.mw-collapsible' ).makeCollapsible();
 
-               // Lazy load jquery.tablesorter
-               $sortableTables = $content.find( 'table.sortable' );
-               if ( $sortableTables.length ) {
+               $sortable = $content.find( 'table.sortable' );
+               if ( $sortable.length ) {
+                       // Preloaded by Skin::getDefaultModules()
                        mw.loader.using( 'jquery.tablesorter', function () {
-                               $sortableTables.tablesorter();
+                               $sortable.tablesorter();
                        } );
                }