View source
<?php
define('ANGULARJS_DEFAULT_VERSION', '1.0.8');
define('ANGULARJS_CDN_URL', 'https://ajax.googleapis.com/ajax/libs/angularjs');
function angularjs_page_delivery_callback_alter(&$callback) {
if (!empty($_SERVER['HTTP_X_ANGULARJS']) && $callback == 'drupal_deliver_html_page') {
$callback = 'angularjs_deliver_page_content';
}
}
function angularjs_deliver_page_content($page_callback_result) {
if (is_int($page_callback_result)) {
drupal_deliver_html_page($page_callback_result);
}
else {
if (is_null(drupal_get_http_header('Content-Type'))) {
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
}
$output = '<title>' . drupal_get_title() . '</title>' . render($page_callback_result);
$output .= drupal_get_js();
print $output;
drupal_page_footer();
}
}
function angularjs_help($path, $arg) {
switch ($path) {
case 'admin/config/development/angularjs':
return '<p>' . t('Configure what files <a href="@angularjs">AngularJS</a> are loaded onto the site. Select which AngularJS version, the compression level and whether or not to use a CDN.', array(
'@jquery' => 'http://angularjs.com',
)) . '</p>';
}
}
function angularjs_menu() {
$items = array();
$items['admin/config/development/angularjs'] = array(
'title' => t('AngularJS'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'angularjs_settings_form',
),
'file' => 'angularjs.admin.inc',
'access arguments' => array(
'administer angularjs',
),
'description' => 'Configure settings related to the AngularJS library',
);
return $items;
}
function angularjs_version_options($reset = FALSE) {
$cid = 'angularjs:versions';
if (!$reset && ($cache = cache_get($cid)) && !empty($cache->data)) {
$data = $cache->data;
}
else {
$result = drupal_http_request('https://code.angularjs.org/');
$re = "/href=\"([0-9][0-9\\.a-z\\-]+)\\/\"/";
if (TRUE === isset($result->data)) {
preg_match_all($re, $result->data, $matches);
if ($matches && 2 == sizeof($matches)) {
$data = $matches[1];
cache_set($cid, $data);
}
}
}
return drupal_map_assoc($data);
}
function angularjs_version_files($version = NULL, $reset = FALSE) {
$data = array();
if ($version) {
$cid = 'angularjs:versions:' . $version;
if (!$reset && ($cache = cache_get($cid)) && !empty($cache->data)) {
$data = $cache->data;
}
else {
$result = drupal_http_request('https://code.angularjs.org/' . $version);
if (FALSE === isset($result->error)) {
$re = "/href=\"([a-z][a-z\\-\\.0-9]+)(?<!min).js\"/";
preg_match_all($re, $result->data, $matches);
if ($matches && 2 == sizeof($matches)) {
$data = $matches[1];
$angular_position = array_search('angular', $data);
if (FALSE !== $angular_position) {
unset($data[$angular_position]);
}
cache_set($cid, $data);
}
}
}
}
return $data;
}
function angularjs_permission() {
return array(
'administer angularjs' => array(
'title' => t('Administer AngularJS'),
'description' => t('Perform administration for AngularJS'),
),
);
}
function angularjs_library() {
$libraries = array();
$library_path = libraries_get_path('angular');
$version = variable_get('angularjs_version', ANGULARJS_DEFAULT_VERSION);
$files = variable_get('angularjs_files', array());
$compression = variable_get('angularjs_compression_type', 'min');
$cdn = TRUE == variable_get('angularjs_cdn', '0');
$extension = FALSE === empty($compression) ? $compression . '.js' : 'js';
$path = FALSE == $cdn ? $library_path : ANGULARJS_CDN_URL . '/' . $version;
$js = array(
$path . '/angular.' . $extension => array(
'external' => $cdn,
),
);
foreach ($files as $file) {
$js[$path . '/' . $file . '.' . $extension] = array(
'external' => $cdn,
);
}
foreach (array(
'controllers',
'filters',
'directives',
'services',
) as $type) {
foreach (angularjs_get_modules($type, FALSE, $version) as $path => $options) {
$js[$path] = $options;
}
}
$libraries['angularjs'] = array(
'title' => 'AngularJS',
'website' => 'angularjs.org',
'version' => $version,
'js' => $js,
);
$js_settings = array(
'angularjsApp' => array(
'restws_csrf_token' => drupal_get_token('restws'),
'basePath' => url('', array(
'absolute' => TRUE,
)),
),
);
drupal_add_js($js_settings, 'setting');
return $libraries;
}
function angularjs_init_application($name, $base_href = NULL) {
if (NULL == $base_href) {
$base_href = url($_GET['q']) . '/';
}
angularjs_set_app_name($name);
angularjs_set_base_href($base_href);
drupal_add_library('angularjs', 'angularjs');
}
function angularjs_get_app_name() {
return drupal_static('angularjs_app_name');
}
function angularjs_set_app_name($app_name) {
$name =& drupal_static('angularjs_app_name');
$name = $app_name;
return $name;
}
function angularjs_get_base_href() {
return drupal_static('angularjs_base_href');
}
function angularjs_set_base_href($base_href) {
$href =& drupal_static('angularjs_base_href');
$href = $base_href;
return $href;
}
function angularjs_preprocess_html(&$vars) {
$app_name = angularjs_get_app_name();
$base_href = angularjs_get_base_href();
if (FALSE === empty($app_name)) {
$vars['attributes_array']['ng-app'] = $app_name;
$vars['html_attributes_array']['ng-app'] = $app_name;
}
if (FALSE === empty($base_href)) {
drupal_add_html_head(array(
'#tag' => 'base',
'#attributes' => array(
'href' => $base_href,
),
), 'angularjs_base_href');
}
}
function angularjs_element_info() {
$types['ng_select'] = array(
'#input' => TRUE,
'#multiple' => FALSE,
'#theme' => 'ng_select',
'#theme_wrappers' => array(
'form_element',
),
'#process' => array(
'form_process_select',
'ajax_process_form',
'angularjs_process_select',
),
'#ng_model' => '',
'#ng_controller' => '',
'#attached' => array(
'library' => array(),
),
);
return $types;
}
function angularjs_theme() {
return array(
'ng_select' => array(
'render element' => 'element',
),
'ng_table' => array(
'variables' => array(
'header' => NULL,
'attributes' => array(),
'caption' => NULL,
'sticky' => TRUE,
'empty' => '',
'row' => array(),
'ng_model' => '',
'ng_controller' => '',
'ng_repeat' => '',
'ng_empty' => '',
),
),
);
}
function angularjs_process_select($element, $form_state) {
$options = array();
if (TRUE === isset($element['#options'])) {
foreach ($element['#options'] as $value => $name) {
$options[] = array(
'value' => $value,
'name' => $name,
);
}
}
if (TRUE === isset($element['#ng_controller'])) {
$data['angularjs'][$element['#ng_controller']][$element['#ng_model']] = array(
'options' => $options,
'required' => $element['#required'],
'selected' => $element['#default_value'],
'_type' => 'select',
'_name' => $element['#name'],
);
$element['#attached']['js'][] = array(
'type' => 'setting',
'data' => $data,
);
}
return $element;
}
function theme_ng_select($variables) {
$element = $variables['element'];
element_set_attributes($element, array(
'id',
'name',
'size',
));
_form_set_class($element, array(
'form-select',
));
return '<select' . drupal_attributes($element['#attributes']) . ' ng-model="' . $element['#ng_model'] . '.selected" ng-options="option.value as option.name for option in ' . $element['#ng_model'] . '.options"></select> ';
}
function theme_ng_table($variables) {
$header = $variables['header'];
$row = $variables['row'];
$attributes = $variables['attributes'];
$caption = $variables['caption'];
$sticky = $variables['sticky'];
$empty = $variables['empty'];
$ng_controller = $variables['ng_controller'];
$ng_model = $variables['ng_model'];
$ng_repeat = !empty($variables['ng_repeat']) ? $variables['ng_repeat'] : "item in {$ng_model}";
$ng_empty = !empty($variables['ng_empty']) ? $variables['ng_empty'] : "!{$ng_model}.length";
if (count($header) && $sticky) {
drupal_add_js('misc/tableheader.js');
$attributes['class'][] = 'sticky-enabled';
}
$output = '<table' . drupal_attributes($attributes) . ">\n";
if (isset($caption)) {
$output .= '<caption>' . $caption . "</caption>\n";
}
if (empty($row) && $empty) {
$header_count = 0;
foreach ($header as $header_cell) {
if (is_array($header_cell)) {
$header_count += 1;
}
else {
$header_count++;
}
}
$rows[] = array(
array(
'data' => $empty,
'class' => array(
'empty',
'message',
),
),
);
}
if (count($header)) {
$output .= count($row) ? ' <thead><tr>' : ' <tr>';
foreach ($header as $cell) {
$output .= '<th>' . $cell . '</th>';
}
$output .= count($row) ? " </tr></thead>\n" : "</tr>\n";
}
if (!empty($row)) {
$output .= "<tbody>\n";
$output .= ' <tr ng-repeat="' . $ng_repeat . '">';
foreach ($row as $cell) {
$output .= '<td>' . $cell . '</td>';
}
$output .= " </tr>\n";
$output .= '<tr ng-show="' . $ng_empty . '"><td>' . $empty . '</td></tr>';
$output .= "</tbody>\n";
}
$output .= "</table>\n";
return $output;
}
function angularjs_get_modules($type, $reset = FALSE, $version = NULL) {
$name = 'angularjs_' . $type;
$modules =& drupal_static($name, array());
if (TRUE === empty($modules)) {
$cached_modules = cache_get($name);
if (FALSE === isset($cached_modules->data) || TRUE == $reset) {
$modules = module_invoke_all($name, $version);
drupal_alter($name, $modules, $version);
cache_set($name, $modules);
}
else {
$modules = $cached_modules->data;
}
}
return $modules;
}
function angularjs_get_filters($reset = FALSE, $version = NULL) {
return angularjs_get_modules('filters', $reset, $version);
}
function angularjs_get_directives($reset = FALSE, $version = NULL) {
return angularjs_get_modules('directives', $reset, $version);
}
function angularjs_get_services($reset = FALSE, $version = NULL) {
return angularjs_get_modules('services', $reset, $version);
}
function angularjs_get_controllers($reset = FALSE, $version = NULL) {
return angularjs_get_modules('controllers', $reset, $version);
}
function angularjs_angularjs_services($version = NULL) {
return array(
drupal_get_path('module', 'angularjs') . '/js/angular_resource.js' => array(),
);
}
function angularjs_template_output($var) {
drupal_add_http_header('Content-Type', 'text/html');
echo drupal_get_js();
if (isset($var)) {
echo $var;
}
}
function angularjs_add_partial($name = NULL, $output = NULL) {
$partials =& drupal_static(__FUNCTION__, array());
if (NULL != $name) {
$partials[$name] = $output;
}
return $partials;
}
function angularjs_page_alter(&$variables) {
$partials = angularjs_add_partial();
drupal_alter('angularjs_partials', $partials);
foreach ($partials as $name => $output) {
if (TRUE === is_array($output)) {
$output = render($output);
}
$element = array(
'#tag' => 'script',
'#value' => $output,
'#attributes' => array(
'type' => 'text/ng-template',
'id' => $name,
),
);
$variables['footer']['partial_' . $name] = array(
'#markup' => theme('html_tag', array(
'element' => $element,
)),
);
}
}