Skip to content

Commit

Permalink
Merge 16cd4dd into c97d526
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Oct 8, 2021
2 parents c97d526 + 16cd4dd commit a30f2d2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/Form/CollectionJsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function configureOptions(OptionsResolver $resolver): void
'block_prefix' => 'collection_js_entry',
]);
},
'call_post_add_on_init' => false,
]);
}

Expand All @@ -56,6 +57,7 @@ public function buildView(FormView $view, FormInterface $form, array $options):
'allow_move_down' => $options['allow_move_down'],
'render_expanded' => $options['render_expanded'],
'prototype_name' => $options['prototype_name'],
'call_post_add_on_init' => $options['call_post_add_on_init'],
]);
}

Expand Down
76 changes: 64 additions & 12 deletions src/Resources/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,72 @@ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Re

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }

function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }

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; }

function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }

var _dispatchCollectionJsEvent = /*#__PURE__*/new WeakSet();

var _default = /*#__PURE__*/function (_Controller) {
_inherits(_default, _Controller);

var _super = _createSuper(_default);

function _default() {
var _this;

_classCallCheck(this, _default);

return _super.apply(this, arguments);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

_this = _super.call.apply(_super, [this].concat(args));

_classPrivateMethodInitSpec(_assertThisInitialized(_this), _dispatchCollectionJsEvent);

return _this;
}

_createClass(_default, [{
key: "connect",
value: function connect() {
var _self = this;

var options = {
call_post_add_on_init: false,
call_post_add_on_init: this.callPostAddOnInitValue,
post_add: function post_add(new_elem, context, index) {
_classPrivateMethodGet(_self, _dispatchCollectionJsEvent, _dispatchCollectionJsEvent2).call(_self, 'post-add', {
new_elem: new_elem,
context: context,
index: index
});
},
post_delete: function post_delete(delete_elem, context, index) {
_classPrivateMethodGet(_self, _dispatchCollectionJsEvent, _dispatchCollectionJsEvent2).call(_self, 'post-delete', {
delete_elem: delete_elem,
context: context,
index: index
});
},
post_up: function post_up(elem, switched_elem, index) {
_classPrivateMethodGet(_self, _dispatchCollectionJsEvent, _dispatchCollectionJsEvent2).call(_self, 'post-up', {
elem: elem,
switched_elem: switched_elem,
index: index
});
},
post_down: function post_down(elem, switched_elem, index) {
_classPrivateMethodGet(_self, _dispatchCollectionJsEvent, _dispatchCollectionJsEvent2).call(_self, 'post-down', {
elem: elem,
switched_elem: switched_elem,
index: index
});
},
prototype_name: this.prototypeNameValue || '__name__'
};

Expand All @@ -66,21 +114,15 @@ var _default = /*#__PURE__*/function (_Controller) {
}

if (this.allowDeleteValue) {
options = _objectSpread(_objectSpread({}, options), {}, {
btn_delete_selector: '.collection-js-elem-remove'
});
options.btn_delete_selector = '.collection-js-elem-remove';
}

if (this.allowMoveUpValue) {
options = _objectSpread(_objectSpread({}, options), {}, {
btn_up_selector: '.collection-js-elem-up'
});
options.btn_up_selector = '.collection-js-elem-up';
}

if (this.allowMoveDownValue) {
options = _objectSpread(_objectSpread({}, options), {}, {
btn_down_selector: '.collection-js-elem-down'
});
options.btn_down_selector = '.collection-js-elem-down';
}

(0, _symfonyCollectionJs["default"])(this.element.querySelector('.collection-js-root'), options);
Expand All @@ -92,10 +134,20 @@ var _default = /*#__PURE__*/function (_Controller) {

exports["default"] = _default;

function _dispatchCollectionJsEvent2(event, detail) {
// Dispatch event like it was dispatched by https://github.com/stimulus-use/stimulus-use/blob/main/docs/use-dispatch.md
this.element.dispatchEvent(new CustomEvent("".concat(this.identifier, ":").concat(event), {
bubbles: true,
cancelable: true,
detail: detail
}));
}

_defineProperty(_default, "values", {
allowAdd: Boolean,
allowDelete: Boolean,
allowMoveUp: Boolean,
allowMoveDown: Boolean,
prototypeName: String
prototypeName: String,
callPostAddOnInit: Boolean
});
42 changes: 28 additions & 14 deletions src/Resources/assets/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ export default class extends Controller {
allowDelete: Boolean,
allowMoveUp: Boolean,
allowMoveDown: Boolean,
prototypeName: String
prototypeName: String,
callPostAddOnInit: Boolean,
};

connect() {
const _self = this;
let options = {
call_post_add_on_init: false,
call_post_add_on_init: this.callPostAddOnInitValue,
post_add: function(new_elem, context, index) {
_self.#dispatchCollectionJsEvent('post-add', {new_elem, context, index});
},
post_delete: function(delete_elem, context, index) {
_self.#dispatchCollectionJsEvent('post-delete', {delete_elem, context, index});
},
post_up: function(elem, switched_elem, index) {
_self.#dispatchCollectionJsEvent('post-up', {elem, switched_elem, index});
},
post_down: function(elem, switched_elem, index) {
_self.#dispatchCollectionJsEvent('post-down', {elem, switched_elem, index});
},
prototype_name: this.prototypeNameValue || '__name__',
};
if (this.allowAddValue) {
Expand All @@ -25,24 +39,24 @@ export default class extends Controller {
};
}
if (this.allowDeleteValue) {
options = {
...options,
btn_delete_selector: '.collection-js-elem-remove',
};
options.btn_delete_selector ='.collection-js-elem-remove';
}
if (this.allowMoveUpValue) {
options = {
...options,
btn_up_selector: '.collection-js-elem-up',
};
options.btn_up_selector ='.collection-js-elem-up';
}
if (this.allowMoveDownValue) {
options = {
...options,
btn_down_selector: '.collection-js-elem-down',
};
options.btn_down_selector = '.collection-js-elem-down';
}

formCollection(this.element.querySelector('.collection-js-root'), options);
}

#dispatchCollectionJsEvent(event, detail) {
// Dispatch event like it was dispatched by https://github.com/stimulus-use/stimulus-use/blob/main/docs/use-dispatch.md
this.element.dispatchEvent(new CustomEvent(`${this.identifier}:${event}`, {
bubbles: true,
cancelable: true,
detail
}));
}
}
4 changes: 2 additions & 2 deletions src/Resources/assets/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

.collection-js-actions {
position: absolute;
right: 0px;
top: 0px;
right: 0;
top: 0;
}
1 change: 1 addition & 0 deletions src/Resources/views/bootstrap_5_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
('data-'~ controllerName ~ '-allow-move-up-value'): allow_move_up ? 'true' : 'false',
('data-'~ controllerName ~ '-allow-move-down-value'): allow_move_down ? 'true' : 'false',
('data-'~ controllerName ~ '-prototype-name-value'): prototype_name ? '__name__',
('data-'~ controllerName ~ '-call-post-add-on-init-value'): call_post_add_on_init ? 'true' : 'false',
}) %}
{% if ea is defined %}
{% set row_attr = row_attr|merge({class: (row_attr.class ~ ' field-collection')|trim}) %}
Expand Down

0 comments on commit a30f2d2

Please sign in to comment.