-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathXEditable.php
628 lines (549 loc) · 15.1 KB
/
XEditable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<?php
/**
* XEditable class file.
*
* @author Marcio Camello <[email protected]>
* @link http://
* @copyright Copyright © Marcio Camello 2014
* @version 1.5.1
*/
namespace mcms\xeditable;
use backend\modules\cms\models\Content;
use yii\bootstrap\BootstrapAsset;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
use yii\web\NotFoundHttpException;
use yii\web\View;
class XEditable extends \yii\base\Widget
{
/**
* @see Xeditable
* @var object
* Additional options for submit ajax request. List of values: http://api.jquery.com/jQuery.ajax
*
* ajaxOptions: {
* type: 'put',
* dataType: 'json'
* }
*/
public $ajaxOptions = null;
/**
* @see Xeditable
* @var string
* Animation speed (inline mode only)
*/
public $anim = false;
/**
* @see Xeditable
* @var string
* Allows to automatically set element's text based on it's value. Can be auto|always|never. Useful for select and date.
* For example, if dropdown list is {1: 'a', 2: 'b'} and element's value set to 1, it's html will be automatically set to 'a'.
* auto - text will be automatically set only if element is empty.
* always|never - always(never) try to set element's text.
*/
public $autotext = 'auto';
/**
* @see Xeditable
* @var string|object
* Value that will be displayed in input if original field value is empty (null|undefined|'').
*/
public $defaultValue = null;
/**
* @see Xeditable
* @var boolean
* Sets disabled state of editable
*/
public $disabled = false;
/**
* @see Xeditable
* @var function|boolean
* Callback to perform custom displaying of value in element's text.
* If null, default input's display used.
* If false, no displaying methods will be called, element's text will never change.
* Runs under element's scope.
* Parameters:
*
* value current value to be displayed
* response server response (if display called after ajax submit), since 1.4.0
* For inputs with source (select, checklist) parameters are different:
*
* value current value to be displayed
* sourceData array of items for current input (e.g. dropdown items)
* response server response (if display called after ajax submit), since 1.4.0
* To get currently selected items use $.fn.editableutils.itemsByValue(value, sourceData).
*
* display: function(value, sourceData) {
* //display checklist as comma-separated values
* var html = [],
* checked = $.fn.editableutils.itemsByValue(value, sourceData);
*
* if(checked.length) {
* $.each(checked, function(i, v) { html.push($.fn.editableutils.escape(v.text)); });
* $(this).html(html.join(', '));
* } else {
* $(this).empty();
* }
* }
*/
public $display = null;
/**
* @see Xeditable
* @var string
* Css class applied when editable text is empty.
*/
public $emptyclass = 'editable-empty';
/**
* @see Xeditable
* @var string
* Text shown when element is empty.
*/
public $emptytext = 'Empty';
/**
* @see Xeditable
* @var function
* Error callback. Called when request failed (response status != 200).
* Usefull when you want to parse error response and display a custom message. Must return string - the message to be displayed in the error block.
*
* error: function(response, newValue) {
* if(response.status === 500) {
* return 'Service unavailable. Please try later.';
* } else {
* return response.responseText;
* }
* }
*/
public $error = null;
/**
* @see Xeditable
* @var string|boolean
* Color used to highlight element after update. Implemented via CSS3 transition, works in modern browsers.
*/
public $highlight = '#FFFF80';
/**
* @see Xeditable
* @var string
* Name of field. Will be submitted on server. Can be taken from id attribute
*/
public $name = null;
/**
* @see Xeditable
* @var string
* Action when user clicks outside the container. Can be cancel|submit|ignore.
* Setting ignore allows to have several containers open.
*/
public $onblur = 'cancel';
/**
* @see Xeditable
* @var object|function
* Additional params for submit. If defined as object - it is appended to original ajax data (pk, name and value).
* If defined as function - returned object overwrites original ajax data.
*
* params: function(params) {
* //originally params contain pk, name and value
* params.a = 1;
* return params;
* }
*/
public $params = null;
/**
* @see Xeditable
* @var string|object|function
* Primary key of editable object (e.g. record id in database).
* For composite keys use object, e.g. {id: 1, lang: 'en'}. Can be calculated dynamically via function.
*/
public $pk = null;
/**
* @see Xeditable
* @var string
* Placement of container relative to element. Can be top|right|bottom|left. Not used for inline container.
*/
public $placement = 'top';
/**
* @see Xeditable
* @var boolean
* Whether to save or cancel value when it was not changed but form was submitted
*/
public $savenochange = false;
/**
* @see Xeditable
* @var string
* If selector is provided, editable will be delegated to the specified targets.
* Usefull for dynamically generated DOM elements.
* Please note, that delegated targets can't be initialized with emptytext and autotext options, as they actually become editable only after first click.
* You should manually set class editable-click to these elements.
* Also, if element originally empty you should add class editable-empty, set data-value="" and write emptytext into element:
*
* <div id="user">
* <!-- empty -->
* <a href="#" data-name="username" data-type="text" class="editable-click editable-empty" data-value="" title="Username">Empty</a>
* <!-- non-empty -->
* <a href="#" data-name="group" data-type="select" data-source="/groups" data-value="1" class="editable-click" title="Group">Operator</a>
* </div>
*
* <script>
* $('#user').editable({
* selector: 'a',
* url: '/post',
* pk: 1
* });
* </script>
*/
public $selector = null;
/**
* @see Xeditable
* @var string
* Strategy for sending data on server. Can be auto|always|never. When 'auto' data will be sent on server only
* if pk and url defined, otherwise new value will be stored locally.
*/
public $send = 'auto';
/**
* @see Xeditable
* @var boolean|string
* Where to show buttons: left(true)|bottom|false
* Form without buttons is auto-submitted.
*/
public $showbuttons = 'left';
/**
* @see Xeditable
* @var function
* Success callback. Called when value successfully sent on server and response status = 200.
* Usefull to work with json response. For example, if your backend response can be {success: true} or {success: false, msg: "server error"} you can check it inside this callback.
* If it returns string - means error occured and string is shown as error message.
* If it returns object like {newValue: <something>} - it overwrites value, submitted by user.
* Otherwise newValue simply rendered into element.
*
* success: function(response, newValue) {
* if(!response.success)
* return response.msg;
* }
*/
public $success = null;
/**
* @see Xeditable
* @var string
* How to toggle editable. Can be click|dblclick|mouseenter|manual.
* When set to manual you should manually call show/hide methods of editable.
* Note: if you call show or toggle inside click handler of some DOM element, you need to apply e.stopPropagation() because containers are being closed on any click on document.
*
* $('#edit-button').click(function(e) {
* e.stopPropagation();
* $('#username').editable('toggle');
* });
*/
public $toggle = 'click';
/**
* @see Xeditable
* @var string
* Type of input. Can be text|textarea|select|date|checklist and more
*/
public $type = 'text';
/**
* @see Xeditable
* @var string
* Css class applied when value was stored but not sent to server (pk is empty or send = 'never').
* You may set it to null if you work with editables locally and submit them together.
*/
public $unsavedclass = 'editable-unsaved';
/**
* @see Xeditable
* @var string|function
* Url for submit, e.g. '/post'
* If function - it will be called instead of ajax. Function should return deferred object to run fail/done callbacks.
*
* url: function(params) {
* var d = new $.Deferred;
* if(params.value === 'abc') {
* return d.reject('error message'); //returning error via deferred object
* } else {
* //async saving data in js model
* someModel.asyncSaveMethod({
* ...,
* success: function(){
* d.resolve();
* }
* });
* return d.promise();
* }
* }
*/
public $url = null;
/**
* @see Xeditable
* @var function
* Function for client-side validation. If returns string - means validation not passed and string showed as error.
* Since 1.5.1 you can modify submitted value by returning object from validate: {newValue: '...'} or {newValue: '...', msg: '...'}
*
* validate: function(value) {
* if($.trim(value) == '') {
* return 'This field is required';
* }
* }
*/
public $validate = null;
/**
* @see Xeditable
* @var mixed
* Initial value of input. If not set, taken from element's text.
* Note, that if element's text is empty - text is automatically generated from value and can be customized (see autotext option).
* For example, to display currency sign:
*
* <a id="price" data-type="text" data-value="100"></a>
* <script>
* $('#price').editable({
* ...
* display: function(value) {
* $(this).text(value + '$');
* }
* })
* </script>
*/
public $value = null;
/**
* @see Xeditable
* @var array
*/
public $paramsOptions = [];
/**
* @see Xeditable
* @var array
*/
public $options = [];
/**
* @see Xeditable
* @var array
*/
public $htmlOptions = [];
/**
* @see Xeditable
* @var array
*/
public $pluginOptions = [];
/**
* @see Xeditable
* @var function
*/
public $method = null;
/**
* @see Xeditable
* @var string
*/
public $title = null;
/**
* @see Xeditable
* @var object
*/
public $model = null;
/**
* @see Xeditable
* @var array
*/
public $source = null;
/**
* DOM id of target where afterAjaxUpdate handler will call
* live update of editable element
*
* @var string
*/
public $liveTarget = null;
/**
* jQuery selector of elements to wich will be applied editable.
* Usefull in combination of `liveTarget` when you want to keep field(s) editble
* after ajaxUpdate
*
* @var string
*/
public $liveSelector = null;
protected $_prepareToAutotext = false;
/**
* @see Xeditable
* @var object
*/
public $callbacks = [];
/**
* @see Xeditable
* @var string
*/
public $button = null;
/**
* @see Xeditable
* @var string
*/
public $var = null;
/**
* @see Xeditable
* @see Init extension default
*/
public function init()
{
parent::init();
$this->registerAssets();
}
/**
* @see Xeditable
* @see Load extension with all settings
*/
public function run()
{
$this->jsOptions();
$this->registerScript();
return $this->htmlOptions();
}
public function registerScript()
{
$options = false;
foreach($this->options as $name => $value)
{
$options .= $name.":".Json::encode($value).",";
}
foreach($this->callbacks as $name => $value)
{
$options .= $name.":".$value.",";
}
$this->view->registerJs("$('#$this->id').editable({".$options."});");
}
public function htmlOptions()
{
if(ArrayHelper::getValue($this->pluginOptions,'toggle')=='manual')
{
$this->view->registerJs("
$('#edit-".$this->id."').click(function(e) {
e.stopPropagation();
e.preventDefault();
$('#".$this->id."').editable('toggle');
});",View::POS_READY);
return Html::a('<i class="glyphicon glyphicon-pencil" style="padding-right: 5px"></i>[edit]', '#', [
'id' => "edit-".$this->id
]).'<br>'.Html::tag('div',$this->value,ArrayHelper::merge($this->dataHtmlOptions(),[
'style' => 'display:inline;'
]));
}else{
return Html::a($this->value, '#', $this->dataHtmlOptions());
}
}
public function dataHtmlOptions()
{
foreach($this->paramsOptions['default'] as $name => $value)
{
if($name!='id')
{
$dataOptions['data-'.$name] = $value;
}else{
$dataOptions[$name] = $value;
}
}
return ($this->htmlOptions) ? ArrayHelper::merge($dataOptions,$this->htmlOptions) : $dataOptions;;
}
public function jsOptions()
{
/**
* @see Xeditable
* @see set default url
*/
$this->url = $this->url ? $this->url : 'editable';
/**
* @see Xeditable
* @see set default params
*/
$this->paramsOptions['default'] = [
'id' => $this->id,
'type' => $this->type,
'url' => $this->url,
'placement' => $this->placement,
'emptytext' => $this->emptytext,
'showbuttons' => $this->showbuttons,
'send' => $this->send,
];
/**
* @see Xeditable
* @see get data with model
*/
if($this->model==true)
{
$name = $this->pluginOptions['name'];
$this->paramsOptions['model'] = [
'url' => $this->url,
'value' => $this->model->$name,
'pk' => $this->model->id,
];
$this->options = ArrayHelper::merge(
$this->paramsOptions['default'],
$this->paramsOptions['model'],
$this->pluginOptions
);
}else{
$this->options = ArrayHelper::merge(
$this->paramsOptions['default'],
$this->pluginOptions
);
}
/**
* @see Xeditable
* @see i18n for `clear` in date and datetime
*/
if($this->type == 'date' || $this->type == 'datetime') {
if(!isset($this->options['clear'])) {
$this->options['clear'] = \Yii::t('app', 'x clear');
}
}
}
public static function saveAction($data)
{
$model = ArrayHelper::getValue($data,'model');
$name = ArrayHelper::getValue($data,'name');
$value = ArrayHelper::getValue($data,'value');
if($model===null)
throw new NotFoundHttpException();
if(!is_array($value)){
if (strtotime($value))
{
$model->$name = strtotime($value);
}else{
$model->$name = $value;
}
}else{
$model->$name = implode(',', $value);
}
if ($model->validate()){
$model->update();
}else{
VarDumper::dump($model->getErrors(),10);
}
}
/**
* @see Xeditable
* @see Register assets from this extension and yours types
*/
public function registerAssets()
{
$config = new XEditableConfig();
if(isset($this->pluginOptions['mode']) && is_array($this->pluginOptions)){
$config->mode = $this->pluginOptions['mode'];
}
if(isset($this->pluginOptions['form']) && is_array($this->pluginOptions)){
$config->form = $this->pluginOptions['form'];
}
$config->registerDefaultAssets();
if($this->type == 'select2') {
Select2Asset::register($this->view);
}
if($this->type == 'datetime') {
//DateTimePickerAsset::register($this->view);
}
if($this->type == 'date') {
//DatePickerAsset::register($this->view);
}
if($this->type == 'typeaheadjs') {
TypeaheadAsset::register($this->view);
}
if($this->type == 'combodate') {
//ComboDateAsset::register($this->view);
}
if($this->type == 'wysihtml5') {
//WysiHtml5Asset::register($this->view);
}
$this->view = \Yii::$app->getView();
XEditableAsset::register($this->view);
}
}