1 <?php
2 3 4 5 6 7 8 9 10
11 namespace uix\ui;
12
13 14 15 16 17 18
19 class modal extends panel{
20
21 22 23 24 25 26 27
28 public $type = 'modal';
29
30 31 32 33 34 35 36
37 public $footer;
38
39 40 41 42 43 44 45
46 public $templates = null;
47
48 49 50 51 52 53 54
55 public function init(){
56
57 parent::init();
58 $data = uix()->request_vars('post');
59 if (isset($data['uixNonce_' . $this->id()]) && wp_verify_nonce($data['uixNonce_' . $this->id()], $this->id())){
60 wp_send_json_success( $this->get_data() );
61 }
62 }
63 64 65 66 67 68 69 70
71 public function set_footers(){
72 if( !empty( $this->child ) ){
73 foreach ($this->child as $child_slug=>$child){
74 if ( in_array($child->type, array('footer') ) ){
75 $this->footer = $child;
76 $this->attributes['data-footer'] = '#' . $this->id() . '-footer-tmpl';
77 }
78 }
79 }
80 }
81 82 83 84 85 86
87 public function set_attributes(){
88
89 $this->attributes = array(
90 'data-modal' => $this->id(),
91 'data-content' => '#' . $this->id() . '-tmpl',
92 'data-margin' => 12,
93 'data-element' => 'form',
94 'class' => 'button',
95 );
96
97 if( !empty( $this->struct['description'] ) ){
98 $this->attributes['data-title'] = $this->struct['description'];
99 unset( $this->struct['description'] );
100 }
101 if( !empty( $this->struct['attributes'] ) )
102 $this->attributes = array_merge( $this->attributes, $this->struct['attributes'] );
103
104
105 }
106
107 108 109 110 111 112
113 protected function enqueue_active_assets(){
114 echo '<style>h3#' . $this->id() . '_uixModalLable { background: ' . $this->base_color() . '; }</style>';
115 parent::enqueue_active_assets();
116 }
117
118 119 120 121 122 123 124
125 public function set_assets() {
126
127 $this->assets['script']['modals'] = array(
128 'src' => $this->url . 'assets/js/modals' . UIX_ASSET_DEBUG . '.js',
129 'deps' => array( 'baldrick' ),
130 );
131 $this->assets['style']['modals'] = $this->url . 'assets/css/modals' . UIX_ASSET_DEBUG . '.css';
132
133 parent::set_assets();
134 }
135
136
137 138 139 140 141 142 143 144
145 public function render(){
146
147 $this->set_footers();
148
149 add_action( 'admin_footer', array( $this, 'output_templates' ) );
150 add_action( 'wp_footer', array( $this, 'output_templates' ) );
151
152 $output = '<button ' . $this->build_attributes() . '>' . $this->struct['label'] . '</button>';
153
154 $this->templates .= $this->render_modal_template();
155
156 return $output;
157 }
158
159 160 161 162 163 164 165 166
167 public function render_modal_template(){
168 unset( $this->struct['label'] );
169 $output = '<script type="text/html" id="' . esc_attr( $this->id() ) . '-tmpl">';
170 $output .= parent::render();
171 $output .= '</script>';
172 $output .= $this->render_footer_template();
173 return $output;
174 }
175
176
177 178 179 180 181 182 183 184
185 public function render_footer_template(){
186 $output = null;
187 if ( !empty( $this->footer ) ){
188 $output .= '<script type="text/html" id="' . esc_attr( $this->id() ) . '-footer-tmpl">';
189 $output .= $this->footer->render();
190 $output .= '</script>';
191 }
192
193 return $output;
194 }
195
196 197 198 199 200 201 202 203
204 public function output_templates(){
205 echo $this->templates;
206 }
207
208 }