forked from e-frank/yii2-knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnockoutForm.php
More file actions
140 lines (109 loc) · 4.39 KB
/
Copy pathKnockoutForm.php
File metadata and controls
140 lines (109 loc) · 4.39 KB
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
<?
namespace x1\knockout;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\ArrayHelper;
use yii\helpers\StringHelper;
use yii\web\JsExpression;
use yii\web\View;
use yii\helpers\Url;
class KnockoutForm extends \yii\base\Widget {
public static $autoIdPrefix = 'x1Knockout';
public $name = '';
public $model = null;
public $class = null;
public $scenario = 'default';
public $base = [];
public $viewmodel = null;
public static function begin($config = [])
{
$w = parent::begin($config);
$view = $w->getView();
echo $view->render('@x1/knockout/views/knockout-form/begin', [
'id' => $w->id,
]);
$view->registerJs('// knockoutForm');
return $w;
}
private function makePath($attribute, $hasMany, &$model, &$base) {
if (!isset($base[$attribute])) {
$base[$attribute] = [$attribute => 1];
}
return $base[$attribute];
}
public function getPath($attribute, $hasMany = null) {
$splits = explode('.', $attribute);
$base =& $this->base;
$model = $this->model;
foreach ($splits as $split) {
var_dump($split);
if (!isset($base[$split])) {
// $base['_model'] = $model;
$base['_class'] = get_class($model);
$base[$split] = [];
}
$base =& $base[$split];
}
var_dump($splits);
var_dump($this->base);
}
public static function end() {
$w = self::$stack[count(self::$stack) - 1];
$view = $w->getView();
echo $view->render('@x1/knockout/views/knockout-form/end');
parent::end();
}
public function field($model, $attribute, $options = []) {
$splits = explode('.', $attribute);
$c = 0;
$splits_r = array_reverse($splits);
$idx = 0;
foreach ($splits_r as $key => $value) {
$idx++;
if (StringHelper::endsWith($value, '[]')) {
$c++;
$s = (($c > 1) ? str_repeat('$parentContext.', $c - 1) : '') . '$index()';
$value = '[' . str_replace('[]', '', $value) . ']' . '[\' + '.$s. ' + \']';
$splits_r[$key] = $value;
} else {
if ($idx < count($splits_r))
$splits_r[$key] = '[' . $value . ']';
}
}
$name = '\'' . implode('', array_reverse($splits_r)) . '\'';
$field = array_pop($splits);
echo sprintf('
<div class="input-group" data-bind="css: {\'has-error\': %1$s.errors && %1$s.errors().length > 0}">
<input class="form-control" data-bind="value: (%1$s.display) ? %1$s.display : %1$s " />
<input type="hidden" data-bind="value: %1$s, attr: {name:%2$s}">
<!-- ko if: %1$s.errors && %1$s.errors().length > 0 -->
<ol class="has-error clearfix list-unstyled" data-bind="foreach: %1$s.errors"><li data-bind="text: $data"></li></ol>
<!-- /ko -->
</div>
', $field, $name);
}
public function init() {
if (empty($this->model) && !empty($this->class)) {
$this->model = new $this->class(['scenario' => $this->scenario]);
} else if (empty($this->class) && !empty($this->model)) {
$this->class = get_class($this->model);
$this->scenario = $this->model->getScenario();
}
if (empty($this->name) && !empty($this->model))
$this->name = $this->model->formName();
// if (!empty($this->createContent) && empty($this->createButton))
// throw new \yii\base\InvalidConfigException("Expecting 'createButton' property of " . get_class($this));
if (!empty($this->viewmodel)) {
$view = $this->view;
if (empty($this->model))
$data = [];
else
$data = $this->model->toArray();
$view->registerJs(sprintf('//ko.applyBindings(ko.mapping.fromJS(%s, %s), document.getElementById("%s"));', Json::encode($data, JSON_FORCE_OBJECT), $this->viewmodel, $this->id));
$view->registerJs(sprintf('//ko.applyBindings(ko.mapping.fromJS(%s, %s));', Json::encode($data, JSON_FORCE_OBJECT), $this->viewmodel, $this->id));
$view->registerJs(sprintf('console.log(ko.dataFor(document.getElementById("%s")));', $this->id));
}
}
}
?>