| 1 |
<?php |
| 2 |
class AccuaForm_Element_ColorPicker extends Element { |
| 3 |
/** |
| 4 |
* Status that changes to 1 if WordPress is ready to load javascripts, and changes to 2 when color picker scripts are loaded |
| 5 |
* |
| 6 |
* @var int |
| 7 |
*/ |
| 8 |
protected static $_cp_loaded_scripts = 0; |
| 9 |
/** |
| 10 |
* Status that changes to 1 if WordPress is ready to load styles, and changes to 2 when color picker styles are loaded |
| 11 |
* |
| 12 |
* @var int |
| 13 |
*/ |
| 14 |
protected static $_cp_loaded_styles = 0; |
| 15 |
/** |
| 16 |
* Status that changes to TRUE when a color picker field is rendered, meaning that scripts and styles are needed |
| 17 |
* |
| 18 |
* @var bool |
| 19 |
*/ |
| 20 |
protected static $_cp_required_script_styles = FALSE; |
| 21 |
|
| 22 |
public function __construct($label, $name, array $properties = null) { |
| 23 |
parent::__construct($label,$name,$properties); |
| 24 |
$this->setValidation(new AccuaForm_Validation_Color()); |
| 25 |
} |
| 26 |
|
| 27 |
public function jQueryDocumentReady() { |
| 28 |
parent::jQueryDocumentReady(); |
| 29 |
echo 'jQuery("#', $this->attributes["id"], '").colorPicker({opacity: false, renderCallback: function($elm, toggled){ |
| 30 |
if ($elm.val() != "") { |
| 31 |
$elm.val("#"+this.color.colors.HEX); |
| 32 |
} |
| 33 |
}});'; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Loads scripts and styles for color picker only once, if needed, when WordPress is ready |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
protected static function maybe_load_colorPicker_scripts_styles() { |
| 42 |
if (self::$_cp_required_script_styles) { // scripts and styles are needed |
| 43 |
if (self::$_cp_loaded_scripts == 1) { // WordPress is ready to load scripts |
| 44 |
self::$_cp_loaded_scripts = 2; // Change status to load them just once |
| 45 |
wp_enqueue_script('accua-jqColorPicker', plugins_url('/js/jqColorPicker.min.js', ACCUA_FORMS_FILE ), array( 'jquery' ), ACCUA_FORMS_JS_VERSION); |
| 46 |
} |
| 47 |
if (self::$_cp_loaded_styles == 1) { // WordPress is ready to load styles |
| 48 |
self::$_cp_loaded_styles = 2; // Change status to load them just once |
| 49 |
// wp_enqueue_style(...) here if needed |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Function called during action wp_print_scripts to change status of self::$_cp_loaded_scripts and load scripts if needed |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public static function maybe_load_colorPicker_scripts(){ |
| 60 |
if (self::$_cp_loaded_scripts == 0) { // First time |
| 61 |
self::$_cp_loaded_scripts = 1; // When we are in this status, WordPress is ready to load scripts |
| 62 |
self::maybe_load_colorPicker_scripts_styles(); // Load scripts if they were previously set as needed |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Function called during action wp_print_styles to change status of self::$_cp_loaded_styles and load styles if needed |
| 68 |
* |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public static function maybe_load_colorPicker_styles(){ |
| 72 |
if (self::$_cp_loaded_styles == 0) { // First time |
| 73 |
self::$_cp_loaded_styles = 1; // When we are in this status, WordPress is ready to load styles |
| 74 |
self::maybe_load_colorPicker_scripts_styles(); // Load styles if they were previously set as needed |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
public function render() { |
| 79 |
self::$_cp_required_script_styles = TRUE; // Changing status meaning that script and styles are needed |
| 80 |
self::maybe_load_colorPicker_scripts_styles(); // Load script and styles once if WordPress is ready |
| 81 |
parent::render(); |
| 82 |
} |
| 83 |
} |
| 84 |
|