jquery_colorpicker.module

  1. cis7 sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module
  2. cle7 sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module
  3. ecd7 sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module
  4. elmsmedia7 sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module
  5. harmony7 sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module
  6. icor7 sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module
  7. meedjum_blog7 sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module
  8. mooc7 sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module

JQuery Colorpicker primary module file.

Functions

Namesort descending Description
jquery_colorpicker_element_info Implements hook_elements().
jquery_colorpicker_field_formatter_info Implements hook_field_formatter_info().
jquery_colorpicker_field_formatter_view Implements hook_field_formatter_view().
jquery_colorpicker_field_info Implements hook_field_info().
jquery_colorpicker_field_is_empty Implements hook_field_is_empty().
jquery_colorpicker_field_validate Implements hook_field_validate().
jquery_colorpicker_field_widget_form Implements hook_widget_form().
jquery_colorpicker_field_widget_info Implements hook_field_widget_info().
jquery_colorpicker_menu Implements hook_menu().
jquery_colorpicker_permission Implements of hook_permission().
jquery_colorpicker_settings Callback function for the URL admin/settings/jquery_colorpicker This function creates the settings page for the module
jquery_colorpicker_theme Implements hook_theme().
jquery_colorpicker_validate Validation function for the new form element
theme_jquery_colorpicker callback theme for the new form element
theme_jquery_colorpicker_color_display Function that implements theme_jquery_colorpicker_color_display
theme_jquery_colorpicker_text_display Function that implements theme_jquery_colorpicker_text_display

File

sites/all/modules/ulmus/jquery_colorpicker/jquery_colorpicker.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * JQuery Colorpicker primary module file.
  5. */
  6. /**
  7. * Implements of hook_permission().
  8. */
  9. function jquery_colorpicker_permission() {
  10. return array(
  11. 'administer jquery colorpicker' => array(
  12. 'title' => t('Administer jQuery colorpicker'),
  13. 'description' => t('Allows users to administer the settings for the jQuery colorpicker'),
  14. ),
  15. );
  16. }
  17. /**
  18. * Implements hook_menu().
  19. */
  20. function jquery_colorpicker_menu() {
  21. // This creates the URL for the settings page.
  22. $menu['admin/config/development/jquery_colorpicker'] = array(
  23. 'title' => 'Jquery Colorpicker',
  24. 'description' => 'Settings page for jquery colorpicker',
  25. 'page callback' => 'drupal_get_form',
  26. 'page arguments' => array('jquery_colorpicker_settings'),
  27. 'access arguments' => array('administer jquery colorpicker'),
  28. );
  29. return $menu;
  30. }
  31. /**
  32. * Implements hook_elements().
  33. */
  34. function jquery_colorpicker_element_info() {
  35. // This is the definition for the new form API element.
  36. return array(
  37. 'jquery_colorpicker' => array(
  38. '#input' => TRUE,
  39. '#element_validate' => array('jquery_colorpicker_validate'),
  40. '#jquery_colorpicker_background' => 'select.png',
  41. '#theme' => 'jquery_colorpicker',
  42. ),
  43. );
  44. }
  45. /**
  46. * Implements hook_theme().
  47. */
  48. function jquery_colorpicker_theme() {
  49. return array(
  50. 'jquery_colorpicker' => array(
  51. 'render element' => 'element',
  52. ),
  53. 'jquery_colorpicker_color_display' => array(
  54. 'variables' => array(
  55. 'entity_id' => NULL,
  56. 'instance_id' => NULL,
  57. 'entity_delta' => NULL,
  58. 'item' => NULL,
  59. ),
  60. ),
  61. 'jquery_colorpicker_text_display' => array(
  62. 'variables' => array('item' => NULL),
  63. ),
  64. );
  65. }
  66. /**
  67. * callback theme for the new form element
  68. */
  69. function theme_jquery_colorpicker($variables) {
  70. $element = $variables['element'];
  71. $element['#id'] = isset($element['#id']) ? $element['#id'] : 'edit-' . rand(0, 9999);
  72. $class = array('form-colorpicker');
  73. $output = '';
  74. $path = libraries_get_path('colorpicker');
  75. // Decide what background to use to render the element. In order to ensure the background exists, we create an array of
  76. // the two possibilities, that we will use to compare the value submitted in the Form API definition.
  77. $backgrounds = array('select.png', 'select2.png');
  78. // Now we check to see if the value in the Form API definition is valid. If it is, we use it, if it's not, we use a default value.
  79. $background = (in_array($element['#jquery_colorpicker_background'], $backgrounds)) ? $element['#jquery_colorpicker_background'] : 'select.png';
  80. // Since we know the background, we can then get the URL of it to pass to the javascript function.
  81. $background_url = base_path() . $path . '/images/' . $background;
  82. // Next we determine what the default value for the form element is. This will also be passed to the javascript function.
  83. if (isset($element['#value']) && strlen($element['#value'])) {
  84. $default_color = '#' . $element['#value'];
  85. }
  86. elseif (isset($element['#default_value']) && strlen($element['#default_value']) == 6 && preg_match('/^[0-9a-f]{6}$/i', $element['#default_value'])) {
  87. $default_color = '#' . strtolower($element['#default_value']);
  88. }
  89. else {
  90. $default_color = "#ffffff";
  91. }
  92. // Build an array of all the settings to be used by the javascript function.
  93. $settings = array(
  94. 'ids' => array($element['#id']),
  95. 'backgrounds' => array($background_url),
  96. 'backgroundColors' => array($default_color),
  97. $element['#id'] . '-defaultColor' => $default_color,
  98. );
  99. // Attach the 3rd party CSS and JS files, and attach the module's JS files.
  100. $element['#attached'] = array(
  101. 'css' => array(
  102. // Add the 3rd party CSS files required for the form elmeent.
  103. $path . '/css/colorpicker.css',
  104. ),
  105. 'js' => array(
  106. // Add the 3rd party JS files required for the form element.
  107. $path . '/js/colorpicker.js',
  108. // Add the module js files.
  109. drupal_get_path('module', 'jquery_colorpicker') . '/js/jquery_colorpicker.js',
  110. // Add the settings.
  111. array(
  112. 'data' => $settings,
  113. 'type' => 'setting',
  114. ),
  115. ),
  116. );
  117. // If IE6 support is enabled, attach the inline CSS and JS that will reside in conditional comments.
  118. if (variable_get('jquery_colorpicker_ie6_support', 0)) {
  119. $ie6_path = base_path() . drupal_get_path('module', 'jquery_colorpicker') . '/twin_helix/';
  120. $path_to_pngfix = $ie6_path . 'iepngfix.htc';
  121. $path_to_tilebg = $ie6_path . 'iepngfix_tilebg.js';
  122. $css = '<!--[if lt IE 7]>' .
  123. '.inner_wrapper, .color_picker, .colorpicker_color div, .colorpicker_color div div { behavior: url("' . $path_to_pngfix . '") } ' .
  124. '<![endif]-->';
  125. $element['#attached']['css'][] = array(
  126. 'data' => $css,
  127. 'type' => 'inline',
  128. );
  129. $js = 'var script = document.createElement("script"); script.type = "text/javascript";';
  130. $js .= 'script.src = "' . $path_to_tilebg . '";';
  131. $js .= 'document.getElementsByTagName("head")[0].appendChild(script);';
  132. $element['#attached']['js'][] = array(
  133. 'data' => $js,
  134. 'type' => 'inline',
  135. );
  136. }
  137. // And we pass the settings in a namespace to the Javascript.
  138. drupal_add_js(array('jqueryColorpicker' => $settings), 'setting');
  139. // Over the next few lines we build the output of the element in HTML and to send to the browser.
  140. _form_set_class($element, $class);
  141. if (isset($element['#field_prefix'])) {
  142. $output .= '<span class="field-prefix">' . $element['#field_prefix'] . '</span> ';
  143. }
  144. $output .= '<div id="' . $element['#id'] . '" class="jquery_colorpicker">';
  145. $output .= '<div id="' . $element['#id'] . '-inner_wrapper" class="inner_wrapper">';
  146. $output .= '<div class="color_picker" style="background-color:' . $default_color . '">';
  147. $output .= '<span class="hash">#</span>';
  148. $name = isset($element['#name']) ? $element['#name'] : $element['#id'];
  149. $value = isset($element['#value']) ? check_plain($element['#value']) : '';
  150. $output .= '<input type="text"' . ' maxlength="6"' . ' name="' . $name . '" id="' . $element['#id'] . '"' . ' size="7"' . ' value="' . $value . '"' . drupal_attributes($element['#attributes']) . ' />';
  151. $output .= '<div class="description">' . t('Enter a hexidecimal color value. Enabling javascript will replace this input with a graphical color selector.') . '</div>';
  152. $output .= '</div>';
  153. $output .= '</div>';
  154. if (isset($element['#cardinality'])) {
  155. if ($element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
  156. $output .= '<div>' . l(t('Remove'), '#', array('html' => TRUE, 'attributes' => array('onclick' => 'jQuery(\'#' . $element['#id'] . '\').find(\'input\').val(\'\'); jQuery(\'#' . $element['#id'] . '\').parent().parent().parent().hide(); return false;'))) . '</div>';
  157. }
  158. else {
  159. $output .= '<div>' . l(t('Clear'), '#', array('html' => TRUE, 'attributes' => array('onclick' => 'jQuery(\'#' . $element['#id'] . '\').find(\'input\').val(\'\'); jQuery(\'#' . $element['#id'] . ' .color_picker\').css("backgroundColor", "#FFF"); return false'))) . '</div>';
  160. }
  161. }
  162. $output .= '</div>';
  163. if (isset($element['#field_suffix'])) {
  164. $output .= '<span class="field-suffix">' . $element['#field_suffix'] . '</span>';
  165. }
  166. $element['#children'] = $output;
  167. $element['#theme'] = 'form_element';
  168. return drupal_render($element);
  169. }
  170. /**
  171. * Validation function for the new form element
  172. */
  173. function jquery_colorpicker_validate($element, &$form_state) {
  174. // All values must be 6 characters in length (will probably add support for 3 character definitions and predifined colors in version 2
  175. if ($element['#value'] != '' && strlen($element['#value']) != 6) {
  176. form_error($element, t('Color values must be exactly six characters in length'));
  177. }
  178. // All values must not include the hash symbol.
  179. elseif ($element['#value'] != '' && preg_match('/^#/', $element['#value'])) {
  180. form_error($element, t('Do not include the hash symbol (#) when entering colors'));
  181. }
  182. // All values must be hexadecimal values.
  183. elseif ($element['#value'] != '' && !preg_match('/^[0-9a-f]{6}$/i', $element['#value'])) {
  184. form_error($element, t("You entered an invalid value for the color. Colors must be hexadecimal, and can only contain the characters '0' to '9' and 'a' to 'f'."));
  185. }
  186. }
  187. /**
  188. * Callback function for the URL admin/settings/jquery_colorpicker
  189. * This function creates the settings page for the module
  190. */
  191. function jquery_colorpicker_settings() {
  192. // First we create a fieldset to make it all look nice. If ever other settings are enabled, this will keep the page looking nice.
  193. $form['ie6'] = array(
  194. '#type' => 'fieldset',
  195. '#title' => t('Internet Explorer 6'),
  196. );
  197. // Next we add a description of the necessary steps to enable IE6 support.
  198. $form['ie6']['description'] = array(
  199. '#value' => '<p>' . t('Internet Explorer 6 does not support transparencies in .png files. They are rendered with a non-transparent blue glow anywhere where there should be transparency. Since jquery colorpicker relies on transparent .png files to work, this means that out of the box, the jquery colorpicker will not work with IE6. You can solve this using the following steps:') .
  200. '</p><ol><li>' . t('Inside the jquery colorpicker module folder (generally located at sites/all/modules), create a new folder named "twin_helix". If the jquery colorpicker module is in the standard location, this means you will have the folder sites/all/modules/jquery_colorpicker/twin_helix') .
  201. '</li><li>' . t('Download the iepngfix.zip from !twin_helix', array(
  202. '!twin_helix' => l(t('Twin Helix'),
  203. 'http://www.twinhelix.com/css/iepngfix/',
  204. array('absolute' => TRUE)))) .
  205. '</li><li>' . t('Unpack the zip file, and place the following three files into the twin_helix folder you created in step 1:') . '<ul><li>iepngfix.htc</li><li>blank.gif</li><li>iepngfix_tilebg.js</li></ul></li><li>' . t('Check the box below, and save this page') .
  206. '</li></ol>',
  207. );
  208. // And finally we add a checkbox so the user can indicate that IE6 should be supported.
  209. $form['ie6']['jquery_colorpicker_ie6_support'] = array(
  210. '#type' => 'checkbox',
  211. '#title' => t('Support IE6'),
  212. '#default_value' => variable_get('jquery_colorpicker_ie6_support', 0),
  213. );
  214. return system_settings_form($form);
  215. }
  216. /**
  217. * Implements hook_field_info().
  218. */
  219. function jquery_colorpicker_field_info() {
  220. return array(
  221. 'jquery_colorpicker' => array(
  222. 'label' => t('jQuery Colorpicker'),
  223. 'description' => t('A colorpicker pop that uses the jQuery Colorpicker'),
  224. 'default_widget' => 'jquery_colorpicker',
  225. 'default_formatter' => 'jquery_colorpicker_color_display',
  226. 'property_type' => 'text',
  227. ),
  228. );
  229. }
  230. /**
  231. * Implements hook_field_validate().
  232. */
  233. function jquery_colorpicker_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  234. foreach ($items as $delta => $item) {
  235. if (!empty($item['jquery_colorpicker'])) {
  236. // All values must be 6 characters in length (will probably add support for 3 character definitions and predifined colors in version 2.
  237. if (strlen($item['jquery_colorpicker']) != 6) {
  238. $errors[$field['field_name']][$langcode][$delta][] = array(
  239. 'error' => 'jquery_colorpicker_color_not_long_enough',
  240. 'message' => t('Color values must be exactly six characters in length'),
  241. );
  242. }
  243. // All values must not include the hash symbol.
  244. elseif (preg_match('/^#/', $item['jquery_colorpicker'])) {
  245. $errors[$field['field_name']][$langcode][$delta][] = array(
  246. 'error' => 'jquery_colorpicker_starts_with_hash',
  247. 'message' => t('Do not include the hash symbol (#) when entering colors'),
  248. );
  249. }
  250. // All values must be hexadecimal values.
  251. elseif (!preg_match('/^[0-9a-fA-F]{6}$/i', $item['jquery_colorpicker'])) {
  252. $errors[$field['field_name']][$langcode][$delta][] = array(
  253. 'error' => 'jquery_colorpicker_not_jquery_colorpicker',
  254. 'message' => t("You entered an invalid value for the color. Colors must be hexadecimal, and can only contain the characters '0-9', 'a-f' and/or 'A-F'."),
  255. );
  256. }
  257. }
  258. }
  259. }
  260. /**
  261. * Implements hook_field_is_empty().
  262. */
  263. function jquery_colorpicker_field_is_empty($item, $field) {
  264. return empty($item['jquery_colorpicker']);
  265. }
  266. /**
  267. * Implements hook_field_formatter_info().
  268. */
  269. function jquery_colorpicker_field_formatter_info() {
  270. return array(
  271. 'jquery_colorpicker_color_display' => array(
  272. 'label' => t('Colored block'),
  273. 'field types' => array('jquery_colorpicker'),
  274. ),
  275. 'jquery_colorpicker_text_display' => array(
  276. 'label' => t('Text'),
  277. 'field types' => array('jquery_colorpicker'),
  278. ),
  279. 'jquery_colorpicker_raw_hex_display' => array(
  280. 'label' => t('Raw hex'),
  281. 'field types' => array('jquery_colorpicker'),
  282. ),
  283. );
  284. }
  285. /**
  286. * Implements hook_field_formatter_view().
  287. */
  288. function jquery_colorpicker_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  289. $css_attachment_cache = &drupal_static(__FUNCTION__ . '_css');
  290. if (!isset($css_attachment_cache)) {
  291. $css_attachment_cache = array();
  292. }
  293. $element = array();
  294. foreach ($items as $delta => $item) {
  295. switch ($display['type']) {
  296. case 'jquery_colorpicker_color_display':
  297. $entity_ids = entity_extract_ids($entity_type, $entity);
  298. $element[$delta] = array(
  299. '#theme' => 'jquery_colorpicker_color_display',
  300. '#entity_id' => $entity_ids[0],
  301. '#instance_id' => $instance['id'],
  302. '#entity_delta' => $delta,
  303. '#item' => $item['jquery_colorpicker'],
  304. );
  305. $css_attachment = array(
  306. 'type' => 'inline',
  307. 'data' => '.jquery_colorpicker_color_display_' . $instance['id'] . '_' . $delta . '_' . $item['jquery_colorpicker'] . '{background-color:#' . $item['jquery_colorpicker'] . ';}',
  308. );
  309. if (!in_array($css_attachment, $css_attachment_cache)) {
  310. $css_attachment_cache[] = $css_attachment;
  311. $element[$delta]['#attached']['css'] = $css_attachment_cache;
  312. }
  313. break;
  314. case 'jquery_colorpicker_text_display':
  315. $element[$delta] = array(
  316. '#theme' => 'jquery_colorpicker_text_display',
  317. '#item' => $item['jquery_colorpicker'],
  318. );
  319. break;
  320. case 'jquery_colorpicker_raw_hex_display':
  321. $element[$delta]['#markup'] = $item['jquery_colorpicker'];
  322. break;
  323. }
  324. }
  325. return $element;
  326. }
  327. /**
  328. * Implements hook_field_widget_info().
  329. */
  330. function jquery_colorpicker_field_widget_info() {
  331. return array(
  332. 'jquery_colorpicker' => array(
  333. 'label' => t('jQuery Colorpicker'),
  334. 'field types' => array('jquery_colorpicker'),
  335. ),
  336. );
  337. }
  338. /**
  339. * Implements hook_widget_form().
  340. */
  341. function jquery_colorpicker_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  342. $value = '';
  343. if (isset($instance['default_value'][$delta]['jquery_colorpicker'])) {
  344. $value = $instance['default_value'][$delta]['jquery_colorpicker'];
  345. }
  346. if (isset($items[$delta]['jquery_colorpicker'])) {
  347. $value = $items[$delta]['jquery_colorpicker'];
  348. }
  349. $element += array(
  350. '#delta' => $delta,
  351. );
  352. $element['jquery_colorpicker'] = array();
  353. if ($instance['widget']['type'] == 'jquery_colorpicker') {
  354. $element['jquery_colorpicker'] += array(
  355. '#title' => $instance['label'],
  356. '#type' => 'jquery_colorpicker',
  357. '#default_value' => $value,
  358. '#cardinality' => $field['cardinality'],
  359. '#description' => $element['#description'],
  360. );
  361. }
  362. return $element;
  363. }
  364. /**
  365. * Function that implements theme_jquery_colorpicker_color_display
  366. */
  367. function theme_jquery_colorpicker_color_display($variables) {
  368. return '<div id ="jquery_colorpicker_color_display_' . $variables['entity_id'] . '_' . $variables['instance_id'] . '_' . $variables['entity_delta'] . '" class="jquery_colorpicker_color_display jquery_colorpicker_color_display_' . $variables['instance_id'] . ' jquery_colorpicker_color_display_' . $variables['instance_id'] . '_' . $variables['entity_delta'] . '_' . $variables['item'] . '">&nbsp;</div>';
  369. }
  370. /**
  371. * Function that implements theme_jquery_colorpicker_text_display
  372. */
  373. function theme_jquery_colorpicker_text_display($variables) {
  374. return '<div class="jquery_colorpicker_text_display"><span class="jquery_colorpicker_hash_mark">#</span>' . $variables['item'] . '</div>';
  375. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.