You are here

live_css.module in Live CSS 8

File

live_css.module
View source
<?php

/**
 * Implements hook_menu().
 */
function live_css_menu() {
  $items['admin/config/development/live_css'] = array(
    'title' => 'Live CSS',
    'description' => 'Configure the Live CSS editor.',
    'route_name' => 'live_css_admin',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function live_css_permission() {
  return array(
    'edit css' => array(
      'title' => t('View, edit and save CSS'),
      'description' => t('View, edit and save CSS with the live editor.  Without this the editor will not load.'),
    ),
  );
}

/**
 * #pre_render callback that alters the LESS files prior to rendering.
 */
function live_css_pre_render($styles) {
  foreach ($styles['#items'] as $key => $info) {
    $input_file = $info['data'];
    if (drupal_substr($input_file, -5) == '.less') {
      $styles['#items'][$key]['type'] = 'external';
      $styles['#items'][$key]['data'] = base_path() . $styles['#items'][$key]['data'];
    }
  }
  return $styles;
}

/**
 * Implements hook_element_info_alter().
 */
function live_css_element_info_alter(&$type) {
  array_unshift($type['styles']['#pre_render'], 'live_css_pre_render');
}

/**
 * Lists all the live editor's themes available.
 */
function live_css_list_themes() {
  $result = array();
  $files = live_css_list_files(dirname(__FILE__) . '/ace/src');
  foreach ($files as $file) {
    if (drupal_substr($file, 0, 5) == 'theme' && drupal_substr($file, -15) != 'uncompressed.js' && drupal_substr($file, -13) != 'noconflict.js') {
      $theme = drupal_substr($file, 6, drupal_strlen($file) - 9);
      $name = ucwords(str_replace('_', ' ', $theme));
      $result[$theme] = $name;
    }
  }
  return $result;
}

/**
 * Get the directory listing for the theme files of ace.
 */
function live_css_list_files($folder) {
  $results = array();
  $handler = opendir($folder);
  while ($file = readdir($handler)) {
    if ($file != '.' && $file != '..') {
      $results[] = $file;
    }
  }
  closedir($handler);
  return $results;
}

/**
 * Implements hook_page_build().
 *
 *  - This replaced hook_init()
 */
function live_css_page_build() {
  $perf = config('system.performance');

  /**
   * Verify permissions to use editor and make sure aggregation
   * is disabled.
   */
  if (user_access('edit css') && !$perf
    ->get('css.preprocess')) {
    $config = config('live_css.settings');
    $less = (bool) $config
      ->get('live_css_less');
    $theme = $config
      ->get('live_css_theme');

    // Add necessary javascript files to handle LESS files editing.
    if ($less) {
      drupal_add_js(drupal_get_path('module', 'live_css') . '/less-display.js');
      drupal_add_js(drupal_get_path('module', 'live_css') . '/less-1.3.0.min.js');
    }
    $settings = array();

    // Prepare php variables for transfer to javascript
    $settings['theme'] = $theme;
    $settings['autoload'] = FALSE;
    $settings['hideadmin'] = (bool) $config
      ->get('live_css_hideadmin');
    $settings['hidemodules'] = (bool) $config
      ->get('live_css_hidemodules');
    $settings['fontsize'] = $config
      ->get('live_css_fontsize');
    $settings['tabsize'] = (int) $config
      ->get('live_css_tabsize');
    $settings['softtabs'] = (bool) $config
      ->get('live_css_softtabs');
    $settings['storage'] = (bool) $config
      ->get('live_css_storage');
    $settings['less'] = $less;
    $settings['savepath'] = url('css/save');

    // Leaving this one out until the Admin Menu team has a working D8 release
    // $settings['menumargin'] = (boolean) config('admin_menu.settings')->get('admin_menu_top_margin');
    // Load settings from PHP to JS
    drupal_add_js(array(
      'live_css' => $settings,
    ), 'setting');

    // Load the editor itself
    drupal_add_js(drupal_get_path('module', 'live_css') . '/ace/src/ace.js');
    drupal_add_js(drupal_get_path('module', 'live_css') . '/ace/src/mode-css.js');
    drupal_add_js(drupal_get_path('module', 'live_css') . '/ace/src/theme-' . $theme . '.js');

    // Load any plugins there may be (there are none right now)
    // drupal_add_js(drupal_get_path('module', 'live_css') . '/plugins.js');
    // Load the list of stylesheets
    drupal_add_css(drupal_get_path('module', 'live_css') . '/css.css', array(
      'group' => 'CSS_THEME',
      'every_page' => TRUE,
      'weight' => 100,
      'preprocess' => FALSE,
    ));
    drupal_add_js(drupal_get_path('module', 'live_css') . '/css.js');
  }
}

Functions

Namesort descending Description
live_css_element_info_alter Implements hook_element_info_alter().
live_css_list_files Get the directory listing for the theme files of ace.
live_css_list_themes Lists all the live editor's themes available.
live_css_menu Implements hook_menu().
live_css_page_build Implements hook_page_build().
live_css_permission Implements hook_permission().
live_css_pre_render #pre_render callback that alters the LESS files prior to rendering.