You are here

public function AddThisScriptManager::attachJsToElement in AddThis 7.4

Attach the widget js to the element.

@todo Change the scope of the addthis.js. See if we can get the scope of the addthis.js into the header just below the settings so that the settings can be used in the loaded addthis.js of our module.

Parameters

array $element: The element to attach the JavaScript to.

File

classes/Services/AddThisScriptManager.php, line 98
Class definition of a script manager.

Class

AddThisScriptManager
@file Class definition of a script manager.

Code

public function attachJsToElement(&$element) {
  if ($this->addthis
    ->getWidgetJsInclude() != AddThis::WIDGET_JS_INCLUDE_NONE) {
    $widget_js = new AddThisWidgetJsUrl($this
      ->getWidgetJsUrl());
    $pubid = $this->addthis
      ->getProfileId();
    if (!empty($pubid) && is_string($pubid)) {
      $widget_js
        ->addAttribute('pubid', $pubid);
    }
    $async = $this->async;
    if ($async) {
      $widget_js
        ->addAttribute('async', 1);
    }
    $domready = $this->domready;
    if ($domready) {
      $widget_js
        ->addAttribute('domready', 1);
    }

    // Only when the script is not loaded after the DOM is ready we include
    // the script with #attached.
    if (!$domready) {
      $element['#attached']['js'][$widget_js
        ->getFullUrl()] = array(
        'type' => 'external',
        'scope' => 'footer',
      );
    }

    // Every setting value passed here overrides previously set values but
    // leaves the values that are already set somewhere else and that are not
    // passed here.
    $element['#attached']['js'][] = array(
      'type' => 'setting',
      'data' => array(
        'addthis' => array(
          'async' => $async,
          'domready' => $domready,
          'widget_url' => $widget_js
            ->getFullUrl(),
          'addthis_config' => $this
            ->getJsAddThisConfig(),
          'addthis_share' => $this
            ->getJsAddThisShare(),
        ),
      ),
    );
  }
}