discourse/app/assets/javascripts/admin/components/value-list.js.es6

114 lines
3.0 KiB
Plaintext
Raw Normal View History

export default Ember.Component.extend({
2018-06-15 23:03:24 +08:00
classNameBindings: [":value-list"],
2015-07-29 03:58:49 +08:00
_enableSorting: function() {
const self = this;
const placeholder = document.createElement("div");
placeholder.className = "placeholder";
let dragging = null;
let over = null;
let nodePlacement;
2018-06-15 23:03:24 +08:00
this.$().on("dragstart.discourse", ".values .value", function(e) {
2015-07-29 03:58:49 +08:00
dragging = e.currentTarget;
2018-06-15 23:03:24 +08:00
e.dataTransfer.effectAllowed = "move";
2015-07-29 03:58:49 +08:00
e.dataTransfer.setData("text/html", e.currentTarget);
});
2018-06-15 23:03:24 +08:00
this.$().on("dragend.discourse", ".values .value", function() {
2015-07-29 03:58:49 +08:00
Ember.run(function() {
dragging.parentNode.removeChild(placeholder);
2018-06-15 23:03:24 +08:00
dragging.style.display = "block";
2015-07-29 03:58:49 +08:00
// Update data
const from = Number(dragging.dataset.index);
let to = Number(over.dataset.index);
if (from < to) to--;
if (nodePlacement === "after") to++;
2018-06-15 23:03:24 +08:00
const collection = self.get("collection");
2015-07-29 03:58:49 +08:00
const fromObj = collection.objectAt(from);
collection.replace(from, 1);
collection.replace(to, 0, [fromObj]);
self._saveValues();
});
return false;
});
2018-06-15 23:03:24 +08:00
this.$().on("dragover.discourse", ".values", function(e) {
2015-07-29 03:58:49 +08:00
e.preventDefault();
2018-06-15 23:03:24 +08:00
dragging.style.display = "none";
if (e.target.className === "placeholder") {
return;
}
2015-07-29 03:58:49 +08:00
over = e.target;
const relY = e.originalEvent.clientY - over.offsetTop;
const height = over.offsetHeight / 2;
const parent = e.target.parentNode;
if (relY > height) {
nodePlacement = "after";
parent.insertBefore(placeholder, e.target.nextElementSibling);
2018-06-15 23:03:24 +08:00
} else if (relY < height) {
2015-07-29 03:58:49 +08:00
nodePlacement = "before";
parent.insertBefore(placeholder, e.target);
}
});
2018-06-15 23:03:24 +08:00
}.on("didInsertElement"),
2015-07-29 03:58:49 +08:00
_removeSorting: function() {
2018-06-15 23:03:24 +08:00
this.$()
.off("dragover.discourse")
.off("dragend.discourse")
.off("dragstart.discourse");
}.on("willDestroyElement"),
2015-07-29 03:58:49 +08:00
_setupCollection: function() {
2018-06-15 23:03:24 +08:00
const values = this.get("values");
if (this.get("inputType") === "array") {
this.set("collection", values || []);
} else {
2018-06-15 23:03:24 +08:00
this.set("collection", values && values.length ? values.split("\n") : []);
}
2018-06-15 23:03:24 +08:00
}
.on("init")
.observes("values"),
2015-07-29 03:58:49 +08:00
_saveValues: function() {
2018-06-15 23:03:24 +08:00
if (this.get("inputType") === "array") {
this.set("values", this.get("collection"));
} else {
2018-06-15 23:03:24 +08:00
this.set("values", this.get("collection").join("\n"));
}
2015-07-29 03:58:49 +08:00
},
2018-06-15 23:03:24 +08:00
inputInvalid: Ember.computed.empty("newValue"),
keyDown(e) {
if (e.keyCode === 13) {
2018-06-15 23:03:24 +08:00
this.send("addValue");
}
},
actions: {
addValue() {
2018-06-15 23:03:24 +08:00
if (this.get("inputInvalid")) {
return;
}
2018-06-15 23:03:24 +08:00
this.get("collection").addObject(this.get("newValue"));
this.set("newValue", "");
2015-07-29 03:58:49 +08:00
this._saveValues();
},
removeValue(value) {
2018-06-15 23:03:24 +08:00
const collection = this.get("collection");
collection.removeObject(value);
2015-07-29 03:58:49 +08:00
this._saveValues();
}
}
});