From f756c72659f4c8e04372b9a40926e72eb7d19371 Mon Sep 17 00:00:00 2001
From: Andrew Nicols <andrew@nicols.co.uk>
Date: Thu, 10 Oct 2013 11:36:37 +0800
Subject: [PATCH 1/2] MDL-42152 AJAX: Add rollup support to YUI loader

---
 theme/yui_combo.php | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 84 insertions(+), 1 deletion(-)

diff --git a/theme/yui_combo.php b/theme/yui_combo.php
index 26a0edf..f9c528b 100644
--- a/theme/yui_combo.php
+++ b/theme/yui_combo.php
@@ -68,7 +68,8 @@ $parts = explode('&', $parts);
 $cache = true;
 $lastmodified = 0;
 
-foreach ($parts as $part) {
+while (count($parts)) {
+    $part = array_shift($parts);
     if (empty($part)) {
         continue;
     }
@@ -81,6 +82,88 @@ foreach ($parts as $part) {
     }
     //debug($bits);
     $version = array_shift($bits);
+    if ($version === 'rollup') {
+        $revision = array_shift($bits);
+        $rollupname = array_shift($bits);
+
+        // Determine whether we should minify this rollup.
+        preg_match('/(-min)?\.js/', $rollupname, $matches);
+        $filesuffix = '.js';
+        if (isset($matches[1])) {
+            $filesuffix = '-min.js';
+        }
+
+        if (strpos($rollupname, 'simpleyui') !== false) {
+            $yuimodules = array(
+                // Include everything from original SimpleYUI.
+                'yui',
+                'yui-base',
+                'get',
+                'features',
+                'loader-base',
+                'loader-rollup',
+                'loader-yui3',
+                'oop',
+                'event-custom-base',
+                'dom-core',
+                'dom-base',
+                'color-base',
+                'dom-style',
+                'selector-native',
+                'selector',
+                'node-core',
+                'node-base',
+                'event-base',
+                'event-base-ie',
+                'pluginhost-base',
+                'pluginhost-config',
+                'event-delegate',
+                'node-event-delegate',
+                'node-pluginhost',
+                'dom-screen',
+                'node-screen',
+                'node-style',
+                'querystring-stringify-simple',
+                'io-base',
+                'json-parse',
+                'transition',
+                'selector-css2',
+                'selector-css3',
+                'dom-style-ie',
+
+                // Some extras we use everywhere.
+                'escape',
+            );
+
+            // We need to add these new parts to the beginning of the $parts list, not the end.
+            $newparts = array();
+            foreach ($yuimodules as $module) {
+                $newparts[] = $revision . '/' . $module . '/' . $module . $filesuffix;
+            }
+            $parts = array_merge($newparts, $parts);
+        }
+
+        // Handle the mcore rollup.
+        if (strpos($rollupname, 'mcore') !== false) {
+            $yuimodules = array(
+                'core/tooltip/tooltip',
+                'core/notification/notification-dialogue',
+            );
+
+            $filesuffix = '.js';
+            if (isset($matches[1])) {
+                $filesuffix = '-min.js';
+            }
+
+            // We need to add these new parts to the beginning of the $parts list, not the end.
+            $newparts = array();
+            foreach ($yuimodules as $module) {
+                $newparts[] = 'm/' . $revision . '/' . $module . $filesuffix;
+            }
+            $parts = array_merge($newparts, $parts);
+        }
+        continue;
+    }
     if ($version === 'm') {
         $version = 'moodle';
     }
-- 
1.8.1.2

