[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/ -> outputfragmentrequirementslib.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Library functions to facilitate the use of JavaScript in Moodle.
  19   *
  20   * @copyright 2016 Adrian Greeve <adrian@moodle.com>
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   * @package core
  23   * @category output
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * This requirements manager captures the appropriate html for creating a fragment to
  30   * be inserted elsewhere.
  31   *
  32   * @copyright 2016 Adrian Greeve <adrian@moodle.com>
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   * @since Moodle 3.1
  35   * @package core
  36   * @category output
  37   */
  38  class fragment_requirements_manager extends page_requirements_manager {
  39  
  40      /**
  41       * Page fragment constructor.
  42       */
  43      public function __construct() {
  44          parent::__construct();
  45          // As this is a fragment the header should already be done.
  46          $this->headdone = true;
  47      }
  48  
  49      /**
  50       * Returns js code to load amd module loader, then insert inline script tags
  51       * that contain require() calls using RequireJS.
  52       *
  53       * @return string
  54       */
  55      protected function get_amd_footercode() {
  56          global $CFG;
  57          $output = '';
  58  
  59          // First include must be to a module with no dependencies, this prevents multiple requests.
  60          $prefix = "require(['core/first'], function() {\n";
  61          $suffix = "\n});";
  62          $output .= html_writer::script($prefix . implode(";\n", $this->amdjscode) . $suffix);
  63          return $output;
  64      }
  65  
  66  
  67      /**
  68       * Generate any HTML that needs to go at the end of the page.
  69       *
  70       * @return string the HTML code to to at the end of the page.
  71       */
  72      public function get_end_code() {
  73          global $CFG;
  74  
  75          $output = '';
  76  
  77          // Call amd init functions.
  78          $output .= $this->get_amd_footercode();
  79  
  80          // Add other requested modules.
  81          $output .= $this->get_extra_modules_code();
  82  
  83          // All the other linked scripts - there should be as few as possible.
  84          if ($this->jsincludes['footer']) {
  85              foreach ($this->jsincludes['footer'] as $url) {
  86                  $output .= html_writer::script('', $url);
  87              }
  88          }
  89  
  90          if (!empty($this->stringsforjs)) {
  91              // Add all needed strings.
  92              $strings = array();
  93              foreach ($this->stringsforjs as $component => $v) {
  94                  foreach ($v as $indentifier => $langstring) {
  95                      $strings[$component][$indentifier] = $langstring->out();
  96                  }
  97              }
  98              // Append don't overwrite.
  99              $output .= html_writer::script('require(["jquery"], function($) {
 100                  M.str = $.extend(true, M.str, ' . json_encode($strings) . ');
 101              });');
 102          }
 103  
 104          // Add variables.
 105          if ($this->jsinitvariables['footer']) {
 106              $js = '';
 107              foreach ($this->jsinitvariables['footer'] as $data) {
 108                  list($var, $value) = $data;
 109                  $js .= js_writer::set_variable($var, $value, true);
 110              }
 111              $output .= html_writer::script($js);
 112          }
 113  
 114          $inyuijs = $this->get_javascript_code(false);
 115          $ondomreadyjs = $this->get_javascript_code(true);
 116          // See if this is still needed when we get to the ajax page.
 117          $jsinit = $this->get_javascript_init_code();
 118          $handlersjs = $this->get_event_handler_code();
 119  
 120          // There is a global Y, make sure it is available in your scope.
 121          $js = "(function() {{$inyuijs}{$ondomreadyjs}{$jsinit}{$handlersjs}})();";
 122  
 123          $output .= html_writer::script($js);
 124  
 125          return $output;
 126      }
 127  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1