require.php 10.5 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2016, 2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
# require.php
# -----------
#
# A simple library for managing dependencies with libraries and templates. Libraries are JavaScript code that is included with <script src="something.js"></script>. Templates are raw text blobs that are directly included in the DOM.
#
# ---------
# Libraries
# ---------
#
# Every top-level PHP file that uses JS code should contain a block of code near the end that prints out all script tags for all libraries. It looks like this:
#    REQUIRE_LIBRARY();
#    REQUIRE_OTHER_LIBRARY();
#    SPITREQUIRE("js/somemain.js");
#
# The REQUIRE_* functions are defined below to include the specified library and all dependencies. SPITREQUIRE() is used to include the main JS file for this page. If there is no main JS file, SPITNULLREQUIRE() includes a default 'main.js' which provides the minimum amount of javascript to make it behave like other pages.
# REQUIRE_* function must be invoked before SPITREQUIRE(). Both of these must be invoked before SPITFOOTER().
#
# ---------
# Templates
# ---------
# Templates are embedded by adding them to the template dependency list at any point before SPITFOOTER(). The key is used as the id for the script tag and is is used to look up the template in the JS code. So the key should only be alphanumeric characters plus '-' and use of that id elsewhere in the page should be avoided.
#    # Creates a template with key 'foo' from text at path 'template/foo.html'
#    AddTemplate("foo");
#    # Creates templates with keys 'foo' and 'bar' from text at paths 'template/foo.html' and 'template/bar.html'
#    AddTemplateList(array("foo", "bar")); 
#    # Creates a template with key 'arbitrary-key' from text at 'arbitrary/path/here.something'
#    AddTemplateKey("arbitrary-key", "arbitrary/path/here.something");
#    # Actually spit out the encoded text files along with the page footer.
#    SPITFOOTER()
#
# Both libraries and templates will only ever be included once per page. Library includes happen before including the main JS file and each libraries dependencies will be included before it is. Templates are included at the very end of the resulting file.
#
# ------------------
# Creating Libraries
# ------------------
#
# Most standard JS libraries can be included simply by adding a REQUIRE_LIBRARY() function below which adds them to the dependency list. Whatever symbols are exported to the global (window) namespace in JS will be available to use to any page which invokes the REQUIRE_LIBRARY() function. If a library has dependencies on other libraries, its REQUIRE_LIBRARY() function simply needs to invoke the REQUIRE_ function for those other libraries.
#
# Custom libraries can use both other libraries and templates. Their REQUIRE_ functions will include appropriate AddTemplate calls. Then they simply need this boilerplate around them in their JS file:
#
#   $(function() {
#     var templates = APT_OPTIONS.fetchTemplateList(['template-foo', 'template-bar']);
#     // templates['template-foo'] and templates['template-bar'] can now be used whenever the text is required.
#     window.SOME_SYMBOL = ...;
#     // Libraries that this library is dependant on can be accessed using their global 'window' symbols.
#   });
#
# ------------
# Main JS File
# ------------
#
# The Main JS File should have this boilerplate:
#
#   $(function () {
#     var templates = APT_OPTIONS.fetchTemplateList(['template-foo', 'template-bar']);
#     // templates['template-foo'] and templates['template-bar'] can now be used whenever the text is required.
#     // Libraries that this main file is dependant on can be accessed using their global 'window' symbols.
#   });

84 85 86 87 88 89 90
# Dependency lists

$PORTAL_TEMPLATES = array();
$PORTAL_LIBRARIES = array();

# Flag to signal that a requires was spit. For errors.
$spatrequired = 0;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

#########################################################################################

function REQUIRE_APTFORMS()
{
  REQUIRE_UNDERSCORE();
  REQUIRE_SUP();
  AddLibrary("js/aptforms.js");
}

function REQUIRE_BILEVEL()
{
  AddLibrary("js/bilevel.js");
}

function REQUIRE_CONTEXTMENU()
{
  AddLibrary("js/lib/bootstrap-contextmenu.js");
}

function REQUIRE_DATEFORMAT()
{
  AddLibrary("js/lib/date.format.js");
}

function REQUIRE_EXTEND()
{
  REQUIRE_UNDERSCORE();
  REQUIRE_SUP();
120
  AddTemplateList(array("user-extend-modal"));
121 122 123 124 125
  AddLibrary("js/extend.js");
}

function REQUIRE_FILESIZE()
{
126
  AddLibrary("js/lib/filesize.min.js");
127 128 129 130 131 132 133 134 135
}

function REQUIRE_FILESTYLE()
{
  AddLibrary("js/lib/filestyle.js");
}

function REQUIRE_FORMHELPERS()
{
136
  AddLibrary("js/lib/bootstrap-formhelpers.js");
137 138
}

139 140 141 142 143 144
function REQUIRE_GENI_AUTH()
{
  AddLibrary("https://www.emulab.net/protogeni/speaks-for/geni-auth.js");
}

function REQUIRE_IDLEGRAPHS()
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
  REQUIRE_UNDERSCORE();
  REQUIRE_SUP();
  REQUIRE_MOMENT();
  AddLibrary("js/idlegraphs.js");
}

function REQUIRE_IMAGE()
{
  REQUIRE_UNDERSCORE();
  REQUIRE_SUP();
  REQUIRE_FILESIZE();
  AddTemplate("imaging-modal");
  AddLibrary("js/image.js");
}

function REQUIRE_JACKS()
{
163
  REQUIRE_JACKSMOD();
164 165 166
  AddLibrary("https://www.emulab.net/protogeni/jacks-utah/js/jacks.js");
}

167 168
function REQUIRE_JACKSMOD()
{
169
  $root = "https://www.emulab.net/protogeni/app/jacksmod/";
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  AddLibrary($root . "jacksmod.js");
  AddLibrary($root . "common/loadbase.js");
  AddLibrary($root . "common/base.js");
  AddLibrary($root . "common/Component.js");
  AddLibrary($root . "common/util.js");
  AddLibrary($root . "common/ForceGraph.js");
  AddLibrary($root . "common/Graph.js");
  AddLibrary($root . "common/RspecLib.js");
  AddLibrary($root . "common/RspecParser.js");
  AddLibrary($root . "common/component/WaitingComponent.js");
  AddLibrary($root . "common/component/MapComponent.js");
  AddLibrary($root . "common/component/GraphNodeComponent.js");
  AddLibrary($root . "common/component/GraphLanComponent.js");
  AddLibrary($root . "common/component/GraphComponent.js");
  AddLibrary($root . "common/component/FailedComponent.js");
  AddLibrary($root . "common/component/ImagePickerComponent.js");
  AddLibrary($root . "thumb/ThumbComponent.js");
  AddLibrary($root . "imagepicker/main.js");
  AddLibrary($root . "thumb/main.js");
  AddLibrary($root . "common/loadcomplete.js");
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
function REQUIRE_JACKS_EDITOR()
{
  REQUIRE_UNDERSCORE();
  REQUIRE_JACKS();
  AddTemplate("edit-modal");
  AddTemplate("edit-inline");
  AddLibrary("js/JacksEditor.js");
}

function REQUIRE_JQUERY_STEPS()
{
  AddLibrary("js/lib/jquery.steps.min.js");
}

function REQUIRE_LIQUIDFILLGAUGE()
{
  AddLibrary("js/liquidFillGauge.js");
}

function REQUIRE_MARKED()
{
  AddLibrary("js/lib/marked.js");
}

function REQUIRE_MOMENT()
{
  AddLibrary("js/lib/moment.js");
}

function REQUIRE_OPENSTACKGRAPHS()
{
  REQUIRE_UNDERSCORE();
  REQUIRE_SUP();
  REQUIRE_MOMENT();
226
  AddLibrary("js/openstackgraphs.js");
227 228 229 230 231 232 233 234 235 236
}

function REQUIRE_PPWIZARDSTART()
{
  REQUIRE_UNDERSCORE();
  REQUIRE_SUP();
  REQUIRE_JACKS_EDITOR();
  AddTemplate("ppform-wizard");
  AddTemplate("ppform-wizard-body");
  AddTemplate("choose-am");
237
  AddTemplate("image-picker-modal");
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
  AddLibrary("js/ppwizardstart.js");
}

function REQUIRE_SUP()
{
  REQUIRE_DATEFORMAT();
  REQUIRE_MARKED();
  REQUIRE_JACKS();
  AddLibrary("js/quickvm_sup.js");
}

function REQUIRE_UNDERSCORE()
{
  AddLibrary("js/lib/underscore-min.js");
}

function REQUIRE_URITEMPLATE()
{
  AddLibrary("js/lib/uritemplate.js");
}

function REQUIRE_WIZARD_TEMPLATE()
{
  REQUIRE_UNDERSCORE();
  AddLibrary("js/wizard-template.js");
}

265 266 267 268 269 270
function REQUIRE_PICKER()
{
  REQUIRE_UNDERSCORE();
  AddLibrary("js/picker.js");
}

271 272 273 274 275 276 277 278 279 280
function REQUIRE_GENILIB_EDITOR()
{
  REQUIRE_UNDERSCORE();
  REQUIRE_SUP();
  REQUIRE_APTFORMS();
  REQUIRE_JACKS();
  AddTemplate("genilib-editor");
  AddLibrary("js/genilib-editor.js");
}

281 282
#########################################################################################

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
function SPITREQUIRE($main, $extras = "")
{
    global $spatrequired, $PORTAL_LIBRARIES;
    
    echo $extras;
    echo "<script src='js/lib/bootstrap.js'></script>\n";
    AddLibrary($main);
    EchoLibraryList($PORTAL_LIBRARIES);
    $spatrequired = 1;
}

function SPITNULLREQUIRE()
{
    REQUIRE_SUP();
    SPITREQUIRE("js/main.js");
}

300 301 302 303 304 305 306 307 308 309 310 311
function SPITREQUIRE_DATASET()
{
    REQUIRE_UNDERSCORE();
    REQUIRE_SUP();
    REQUIRE_MOMENT();
    REQUIRE_APTFORMS();
    SPITREQUIRE("js/create-dataset.js",
                "<script src='js/lib/jquery-ui.js'></script>");
}

#########################################################################################

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

#
# Echos a plaintext <script> tag with a base64-encoded template inside.
# The <script> tag has an id of $baseName. The template is loaded from
# the path 'template/$baseName.html'
#
function EchoTemplate($key, $value)
{
    echo "\n<script type='text/plain' id='" . $key . "'>\n";
#    echo base64_encode(file_get_contents("template/" . $baseName . ".html")) . "\n";
    echo base64_encode(file_get_contents($value)) . "\n";
    echo "</script>\n";
}

function EchoTemplateList($nameList)
{
    foreach ($nameList as $key => $value) {
        EchoTemplate($key, $value);
    }
}

function AddTemplate($baseName)
{
  global $PORTAL_TEMPLATES;
#  array_push($PORTAL_TEMPLATES, $baseName);
  $PORTAL_TEMPLATES[$baseName] = "template/" . $baseName . ".html";
}

function AddTemplateKey($key, $location)
{
  global $PORTAL_TEMPLATES;
  $PORTAL_TEMPLATES[$key] = $location;
}

function AddTemplateList($nameList)
{
  global $PORTAL_TEMPLATES;
  foreach ($nameList as $index => $name) {
    AddTemplate($name);
#    array_push($PORTAL_TEMPLATES, $name);
  }
}

function AddLibrary($baseName)
{
  global $PORTAL_LIBRARIES;
  $found = 0;
  foreach ($PORTAL_LIBRARIES as $index => $name) {
    if ($name === $baseName) {
      $found = 1;
      break;
    }
  }
  if ($found != 1) {
    array_push($PORTAL_LIBRARIES, $baseName);
  }
}

function EchoLibraryList($nameList)
{
    foreach ($nameList as $index => $name) {
      echo "<script src='$name'></script>\n";
    }
}