View Javadoc
1   /**
2    * This file Copyright (c) 2008-2016 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.templatingkit.setup;
35  
36  import static info.magnolia.module.resources.setup.InstallTextResourceTask.DEFAULT_ENCODING;
37  
38  import info.magnolia.module.delta.ArrayDelegateTask;
39  import info.magnolia.module.delta.Task;
40  import info.magnolia.module.resources.ResourceTypes;
41  import info.magnolia.module.resources.setup.InstallBinaryResourceTask;
42  import info.magnolia.module.resources.setup.InstallBinaryResourcesTask;
43  import info.magnolia.module.resources.setup.InstallReferenceResourceTask;
44  import info.magnolia.module.resources.setup.InstallTextResourceTask;
45  import info.magnolia.module.resources.setup.InstallTextResourcesTask;
46  import info.magnolia.module.templatingkit.resources.STKResourceModel;
47  
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import org.apache.commons.lang3.StringUtils;
52  
53  /**
54   * This class installs all resources (mainly JS) needed for STK.
55   * This class should only be used for install tasks (extrasInstallTasks()), and not for minor delta update tasks, use @class UpdateJavascriptsTask or @class UpdateSingleJavascriptTask instead.
56   *
57   * As the order of the files is relevant we can't use a pattern.
58   */
59  public class InstallSTKResourcesTask extends ArrayDelegateTask {
60  
61      public final static String DEFAULT_JS_PATH = "/templating-kit/js/";
62      public final static String SWF_PATTERN = ".*\\.swf";
63      public final static String JS_PATTERN = ".*\\.js";
64  
65      public final static String SCRIPLOADER_PATH_LIBRARIES = DEFAULT_JS_PATH + "scriptloader-libraries";
66      public final static String SCRIPLOADER_PATH_LIBRARIES_MOBILE = DEFAULT_JS_PATH + "mobile-scriptloader-libraries";
67      public final static String SCRIPLOADER_PATH_PLUGINS = DEFAULT_JS_PATH + "scriptloader-plugin";
68      public final static String SCRIPLOADER_PATH_PLUGINS_MOBILE = DEFAULT_JS_PATH + "mobile-scriptloader-plugin";
69  
70      protected final static String[] processedScripts = {};
71  
72      protected final static String[][] specialResources = {
73              {"shims/FlashCanvas/proxy.php", "resources:css"},
74              {"shims/FlashCanvas/README", "resources:css"},
75              {"shims/FlashCanvas/save.php", "resources:css"},
76              {"shims/i18n/errormessages-de.txt", "resources:css"},
77              {"shims/i18n/errormessages-en.txt", "resources:css"},
78              {"shims/styles/details-arrows.png", "resources:binary"},
79              {"shims/styles/forms-temp.png", "resources:binary"},
80              {"shims/styles/forms.png", "resources:binary"},
81              {"shims/styles/polyfill-loader.gif", "resources:binary"},
82              {"shims/styles/shim.css", "resources:css"},
83      };
84  
85      protected final Map<String, String[]> scriptloaderAndReferences = new HashMap<String, String[]>();
86  
87      protected void initScriptloaderMap() {
88          scriptloaderAndReferences.put(
89                  "scriptloader-libraries.js", new String[]{
90                          "libs/modernizr-all.js",
91                          "libs/jquery.js",
92                          "libs/jquery.touchSwipe-mod.js",
93                          "libs/jquery.ui.widget.js",
94                          "libs/jquery.ui.core.js",
95                          "libs/jquery.ui.mouse.js",
96                          "libs/jquery.ui.slider.js",
97                          "libs/jquery.ui.datepicker.js",
98                          "snippets/ui.a11y.ext.js",
99                          "snippets/jquery.mediaqueries.js",
100                         "mediaplayer/html5flowplayer.js",
101                         "libs/polyfiller.js"
102                 }
103         );
104 
105         scriptloaderAndReferences.put(
106                 "scriptloader-plugin.js", new String[]{
107                         "plugins/jquery.mousewheel.js",
108                         "plugins/jquery.color.js",
109                         "plugins/ui.scroller.js",
110                         "plugins/jquery.tabtree.js",
111                         "plugins/jquery.swfobject.js",
112                         "libs/swfobject.js",
113                         "plugins/jquery.social-b.js",
114                         "snippets/jquery.objscale.js",
115                         "snippets/jquery.imgpreload.js",
116                         "plugins/ui.cOverlay.js",
117                         "snippets/urlIndex.js",
118                         "plugins/showbox.js"
119                 }
120         );
121 
122         scriptloaderAndReferences.put(
123                 "mobile-scriptloader-libraries.js", new String[]{
124                         "libs/modernizr-all.js",
125                         "libs/jquery.js",
126                         "libs/jquery.touchSwipe-mod.js",
127                         "libs/jquery.ui.widget.js",
128                         "libs/jquery.ui.core.js",
129                         "snippets/ui.a11y.ext.js",
130                         "snippets/jquery.mediaqueries.js",
131                         "mediaplayer/html5flowplayer.js",
132                         "libs/polyfiller.js"
133                 }
134         );
135 
136         scriptloaderAndReferences.put(
137                 "mobile-scriptloader-plugin.js", new String[]{
138                         "plugins/ui.scroller.js",
139                         "plugins/jquery.tabtree.js",
140                         "plugins/jquery.swfobject.js",
141                         "libs/swfobject.js",
142                         "plugins/jquery.social-b.js"
143                 }
144         );
145     }
146 
147     public InstallSTKResourcesTask() {
148         super("Install STK resources", "Installs all resources (mainly JS) needed by STK.");
149         this.initScriptloaderMap();
150 
151         //Installing all flash files as binaries
152         this.addTask(new InstallBinaryResourcesTask("", "", DEFAULT_JS_PATH + SWF_PATTERN, false, false));
153 
154         //Installing all JSs as JS resources
155         this.addTask(new InstallTextResourcesTask("", "", DEFAULT_ENCODING, DEFAULT_JS_PATH + JS_PATTERN, ResourceTypes.JS, false, null, false, false));
156 
157 
158         //Installing special resources, mostly shims, differentiation for binaries needed.
159         for (String[] specialResource : specialResources) {
160             if (ResourceTypes.BINARY.equalsIgnoreCase(specialResource[1])) {
161                 this.addTask(new InstallBinaryResourceTask(DEFAULT_JS_PATH + specialResource[0], null, null, null, false, false));
162             } else {
163                 this.addTask(newInstallTextResourceTask(DEFAULT_JS_PATH + specialResource[0], specialResource[1]));
164             }
165         }
166 
167         //Installing processed JSs.
168         for (String processedScript : processedScripts) {
169             this.addTask(newInstallTextResourceTask(DEFAULT_JS_PATH + processedScript, ResourceTypes.PROCESSED_JS));
170         }
171 
172         //Installing scriptloader JS with its references
173         for (String scriptLoader : scriptloaderAndReferences.keySet()) {
174             this.addTask(newInstallTextResourceTask(DEFAULT_JS_PATH + scriptLoader, ResourceTypes.PROCESSED_JS));
175 
176             for (String reference : scriptloaderAndReferences.get(scriptLoader)) {
177                 String referenceName = StringUtils.substringBeforeLast(reference, ".js");
178                 if (referenceName.contains("/")) {
179                     referenceName = StringUtils.substringAfterLast(referenceName, "/");
180                 }
181                 this.addTask(new InstallReferenceResourceTask(DEFAULT_JS_PATH + scriptLoader + "/" + referenceName, DEFAULT_JS_PATH + reference, "resources:reference", false));
182             }
183         }
184     }
185 
186     private Task newInstallTextResourceTask(String resource, String template) {
187         return new InstallTextResourceTask(resource, template, DEFAULT_ENCODING, null, null, null, STKResourceModel.class.getName(), false, false);
188     }
189 }