Skip to content

Commit 25d8306

Browse files
committed
Rebase and refresh the code
1 parent ecd774a commit 25d8306

File tree

5 files changed

+128
-252
lines changed

5 files changed

+128
-252
lines changed
Lines changed: 95 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -1,215 +1,105 @@
1-
'use strict';
1+
import { Controller } from '@hotwired/stimulus';
22

3-
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4-
5-
Object.defineProperty(exports, "__esModule", {
6-
value: true
7-
});
8-
exports["default"] = void 0;
9-
10-
var _stimulus = require("stimulus");
11-
12-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13-
14-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
15-
16-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
17-
18-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
19-
20-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
21-
22-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
23-
24-
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
25-
26-
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
27-
28-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
29-
30-
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
31-
32-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
33-
34-
var _default = /*#__PURE__*/function (_Controller) {
35-
_inherits(_default, _Controller);
36-
37-
var _super = _createSuper(_default);
38-
39-
function _default() {
40-
var _this;
41-
42-
_classCallCheck(this, _default);
43-
44-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
45-
args[_key] = arguments[_key];
3+
class default_1 extends Controller {
4+
constructor() {
5+
super(...arguments);
6+
this.index = 0;
467
}
47-
48-
_this = _super.call.apply(_super, [this].concat(args));
49-
50-
_defineProperty(_assertThisInitialized(_this), "index", 0);
51-
52-
_defineProperty(_assertThisInitialized(_this), "controllerName", null);
53-
54-
return _this;
55-
}
56-
57-
_createClass(_default, [{
58-
key: "connect",
59-
value: function connect() {
60-
this.controllerName = this.context.scope.identifier;
61-
this.index = this.startIndexValue ? this.startIndexValue : this.entryTargets.length - 1;
62-
63-
if (!this.prototypeNameValue) {
64-
this.prototypeNameValue = '__name__';
65-
}
66-
67-
this._dispatchEvent('form-collection:pre-connect', {
68-
allowAdd: this.allowAddValue,
69-
allowDelete: this.allowDeleteValue
70-
});
71-
72-
if (true === this.allowAddValue) {
73-
// Add button Add
74-
var buttonAdd = this._textToNode(this.buttonAddValue);
75-
76-
this.element.prepend(buttonAdd);
77-
} // Add buttons Delete
78-
79-
80-
if (true === this.allowDeleteValue) {
81-
for (var i = 0; i < this.entryTargets.length; i++) {
82-
var entry = this.entryTargets[i];
83-
84-
this._addDeleteButton(entry, i);
8+
connect() {
9+
this.controllerName = this.context.scope.identifier;
10+
this.index = this.startIndexValue ? this.startIndexValue : this.entryTargets.length - 1;
11+
if (!this.prototypeNameValue) {
12+
this.prototypeNameValue = '__name__';
8513
}
86-
}
87-
88-
this._dispatchEvent('form-collection:connect', {
89-
allowAdd: this.allowAddValue,
90-
allowDelete: this.allowDeleteValue
91-
});
14+
this._dispatchEvent('form-collection:pre-connect', {
15+
allowAdd: this.allowAddValue,
16+
allowDelete: this.allowDeleteValue,
17+
});
18+
if (this.allowAddValue) {
19+
const buttonAdd = this._textToNode(this.buttonAddValue);
20+
this.element.prepend(buttonAdd);
21+
}
22+
if (this.allowDeleteValue) {
23+
for (let i = 0; i < this.entryTargets.length; i++) {
24+
const entry = this.entryTargets[i];
25+
this._addDeleteButton(entry, i);
26+
}
27+
}
28+
this._dispatchEvent('form-collection:connect', {
29+
allowAdd: this.allowAddValue,
30+
allowDelete: this.allowDeleteValue,
31+
});
9232
}
93-
}, {
94-
key: "add",
95-
value: function add(event) {
96-
this.index++; // Compute the new entry
97-
98-
var newEntry = this.element.dataset.prototype;
99-
100-
if (!newEntry) {
101-
newEntry = this.prototypeValue;
102-
}
103-
104-
var regExp = new RegExp(this.prototypeNameValue + 'label__', 'g');
105-
newEntry = newEntry.replace(regExp, this.index);
106-
regExp = new RegExp(this.prototypeNameValue, 'g');
107-
newEntry = newEntry.replace(regExp, this.index);
108-
newEntry = this._textToNode(newEntry);
109-
110-
this._dispatchEvent('form-collection:pre-add', {
111-
index: this.index,
112-
element: newEntry
113-
});
114-
115-
this.element.append(newEntry); // Retrieve the entry from targets to make sure that this is the one
116-
117-
var entry = this.entryTargets[this.entryTargets.length - 1];
118-
entry = this._addDeleteButton(entry, this.index);
119-
120-
this._dispatchEvent('form-collection:add', {
121-
index: this.index,
122-
element: entry
123-
});
33+
add() {
34+
this.index++;
35+
let newEntry = this.element.dataset.prototype;
36+
if (!newEntry) {
37+
newEntry = this.prototypeValue;
38+
}
39+
let regExp = new RegExp(this.prototypeNameValue + 'label__', 'g');
40+
newEntry = newEntry.replace(regExp, this.index);
41+
regExp = new RegExp(this.prototypeNameValue, 'g');
42+
newEntry = newEntry.replace(regExp, this.index);
43+
newEntry = this._textToNode(newEntry);
44+
this._dispatchEvent('form-collection:pre-add', {
45+
index: this.index,
46+
element: newEntry,
47+
});
48+
this.element.append(newEntry);
49+
let entry = this.entryTargets[this.entryTargets.length - 1];
50+
entry = this._addDeleteButton(entry, this.index);
51+
this._dispatchEvent('form-collection:add', {
52+
index: this.index,
53+
element: entry,
54+
});
12455
}
125-
}, {
126-
key: "delete",
127-
value: function _delete(event) {
128-
var entry = event.target.closest('[data-' + this.controllerName + '-target="entry"]');
129-
130-
this._dispatchEvent('form-collection:pre-delete', {
131-
index: entry.dataset.indexEntry,
132-
element: entry
133-
});
134-
135-
entry.remove();
136-
137-
this._dispatchEvent('form-collection:delete', {
138-
index: entry.dataset.indexEntry,
139-
element: entry
140-
});
56+
delete(event) {
57+
const entry = event.target.closest('[data-' + this.controllerName + '-target="entry"]');
58+
this._dispatchEvent('form-collection:pre-delete', {
59+
index: entry.dataset.indexEntry,
60+
element: entry,
61+
});
62+
entry.remove();
63+
this._dispatchEvent('form-collection:delete', {
64+
index: entry.dataset.indexEntry,
65+
element: entry,
66+
});
14167
}
142-
/**
143-
* Add the delete button to the entry
144-
* @param String entry
145-
* @param Number index
146-
* @returns {ChildNode}
147-
* @private
148-
*/
149-
150-
}, {
151-
key: "_addDeleteButton",
152-
value: function _addDeleteButton(entry, index) {
153-
// link the button and the entry by the data-index-entry attribute
154-
entry.dataset.indexEntry = index;
155-
156-
var buttonDelete = this._textToNode(this.buttonDeleteValue);
157-
158-
if (!buttonDelete) {
68+
_addDeleteButton(entry, index) {
69+
entry.dataset.indexEntry = index.toString();
70+
const buttonDelete = this._textToNode(this.buttonDeleteValue);
71+
if (!buttonDelete) {
72+
return entry;
73+
}
74+
buttonDelete.dataset.indexEntry = index;
75+
if ('TR' === entry.nodeName) {
76+
entry.lastElementChild.append(buttonDelete);
77+
}
78+
else {
79+
entry.append(buttonDelete);
80+
}
15981
return entry;
160-
}
161-
162-
buttonDelete.dataset.indexEntry = index;
163-
164-
if ('TR' === entry.nodeName) {
165-
entry.lastElementChild.append(buttonDelete);
166-
} else {
167-
entry.append(buttonDelete);
168-
}
169-
170-
return entry;
17182
}
172-
/**
173-
* Convert text to Element to insert in the DOM
174-
* @param String text
175-
* @returns {ChildNode}
176-
* @private
177-
*/
178-
179-
}, {
180-
key: "_textToNode",
181-
value: function _textToNode(text) {
182-
var template = document.createElement('template');
183-
text = text.trim(); // Never return a text node of whitespace as the result
184-
185-
template.innerHTML = text;
186-
return template.content.firstChild;
83+
_textToNode(text) {
84+
const template = document.createElement('template');
85+
text = text.trim();
86+
template.innerHTML = text;
87+
return template.content.firstChild;
18788
}
188-
}, {
189-
key: "_dispatchEvent",
190-
value: function _dispatchEvent(name) {
191-
var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
192-
var canBubble = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
193-
var cancelable = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
194-
var userEvent = document.createEvent('CustomEvent');
195-
userEvent.initCustomEvent(name, canBubble, cancelable, payload);
196-
this.element.dispatchEvent(userEvent);
89+
_dispatchEvent(name, payload) {
90+
console.log('TTTT');
91+
this.element.dispatchEvent(new CustomEvent(name, { detail: payload }));
19792
}
198-
}]);
199-
200-
return _default;
201-
}(_stimulus.Controller);
202-
203-
exports["default"] = _default;
204-
205-
_defineProperty(_default, "targets", ['entry']);
206-
207-
_defineProperty(_default, "values", {
208-
allowAdd: Boolean,
209-
allowDelete: Boolean,
210-
buttonAdd: String,
211-
buttonDelete: String,
212-
prototypeName: String,
213-
prototype: String,
214-
startIndex: Number
215-
});
93+
}
94+
default_1.targets = ['entry'];
95+
default_1.values = {
96+
allowAdd: Boolean,
97+
allowDelete: Boolean,
98+
buttonAdd: String,
99+
buttonDelete: String,
100+
prototypeName: String,
101+
prototype: String,
102+
startIndex: Number,
103+
};
104+
105+
export { default_1 as default };
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const config = require('../../../../jest.config.js');
2+
3+
config.setupFilesAfterEnv.push('./test/setup.js');
4+
5+
module.exports = config;

src/FormCollection/Resources/assets/package.json

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,10 @@
1313
}
1414
}
1515
},
16-
"scripts": {
17-
"build": "babel src -d dist",
18-
"test": "babel src -d dist && jest",
19-
"lint": "eslint src test"
20-
},
2116
"peerDependencies": {
22-
"stimulus": "^2.0.0"
17+
"@hotwired/stimulus": "^3.0.0"
2318
},
2419
"devDependencies": {
25-
"@babel/cli": "^7.12.1",
26-
"@babel/core": "^7.12.3",
27-
"@babel/plugin-proposal-class-properties": "^7.12.1",
28-
"@babel/preset-env": "^7.12.7",
29-
"@symfony/stimulus-testing": "^1.1.0",
30-
"stimulus": "^2.0.0"
31-
},
32-
"jest": {
33-
"testRegex": "test/.*\\.test.js",
34-
"setupFilesAfterEnv": [
35-
"./test/setup.js"
36-
]
20+
"@hotwired/stimulus": "^3.0.0"
3721
}
3822
}

0 commit comments

Comments
 (0)