| 1 |
<?php |
| 2 |
|
| 3 |
class VP_Control_Field_CodeEditor extends VP_Control_Field |
| 4 |
{ |
| 5 |
|
| 6 |
/** |
| 7 |
* Editor's language mode |
| 8 |
* (javascript, css, html, php, json, xml, markdown) |
| 9 |
* @var String |
| 10 |
*/ |
| 11 |
protected $_mode; |
| 12 |
|
| 13 |
/** |
| 14 |
* Editor's theme |
| 15 |
* (chaos, chrome, clouds, clouds_midnight, cobalt, crimson_editor, dawn, dreamweaver, eclipse, |
| 16 |
* github, mono_industrial, monokai, solarized_dark, solarized_light, textmate, twilight) |
| 17 |
* @var String |
| 18 |
*/ |
| 19 |
protected $_theme; |
| 20 |
|
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
parent::__construct(); |
| 24 |
} |
| 25 |
|
| 26 |
public static function withArray($arr = array(), $class_name = null) |
| 27 |
{ |
| 28 |
if(is_null($class_name)) |
| 29 |
$instance = new self(); |
| 30 |
else |
| 31 |
$instance = new $class_name; |
| 32 |
$instance->_basic_make($arr); |
| 33 |
|
| 34 |
$instance->set_editor_mode( isset($arr['mode']) ? $arr['mode'] : ''); |
| 35 |
$instance->set_editor_theme(isset($arr['theme']) ? $arr['theme'] : 'textmate'); |
| 36 |
|
| 37 |
return $instance; |
| 38 |
} |
| 39 |
|
| 40 |
protected function _setup_data() |
| 41 |
{ |
| 42 |
$opt = array( |
| 43 |
'mode' => $this->get_editor_mode(), |
| 44 |
'theme' => $this->get_editor_theme(), |
| 45 |
); |
| 46 |
$this->add_data('opt', VP_Util_Text::make_opt($opt)); |
| 47 |
parent::_setup_data(); |
| 48 |
} |
| 49 |
|
| 50 |
public function render($is_compact = false) |
| 51 |
{ |
| 52 |
$this->_setup_data(); |
| 53 |
$this->add_data('is_compact', $is_compact); |
| 54 |
return VP_View::instance()->load('control/codeeditor', $this->get_data()); |
| 55 |
} |
| 56 |
|
| 57 |
public function set_value($_value) |
| 58 |
{ |
| 59 |
// normalize linebreak to \n for all saved data |
| 60 |
if( is_string($_value) ) |
| 61 |
{ |
| 62 |
$_value = str_replace(array("\r\n", "\r"), "\n", $_value); |
| 63 |
} |
| 64 |
$this->_value = $_value; |
| 65 |
return $this; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get editor's language mode |
| 70 |
* |
| 71 |
* @return String Language mode |
| 72 |
*/ |
| 73 |
public function get_editor_mode() { |
| 74 |
return $this->_mode; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Set editor's language mode |
| 79 |
* |
| 80 |
* @param String $_mode Language mode |
| 81 |
*/ |
| 82 |
public function set_editor_mode($_mode) { |
| 83 |
$this->_mode = $_mode; |
| 84 |
return $this; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Get editor's theme |
| 90 |
* |
| 91 |
* @return String Editor's theme |
| 92 |
*/ |
| 93 |
public function get_editor_theme() { |
| 94 |
return $this->_theme; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set editor's theme |
| 99 |
* |
| 100 |
* @param String $_theme Editor's theme |
| 101 |
*/ |
| 102 |
public function set_editor_theme($_theme) { |
| 103 |
$this->_theme = $_theme; |
| 104 |
return $this; |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* EOF |
| 111 |
*/ |