Update code to work with latest html5sortable (#62)

* Update code to work with latest html5sortable (0.9.16)

* Move forEach function callback to constant variable

* Extract inline function into method
This commit is contained in:
David Sevilla Martín 2019-06-03 05:46:48 -04:00 committed by Franz Liedke
parent 6d0de4b29c
commit c65aa52726
3 changed files with 67 additions and 63 deletions

View File

@ -2779,9 +2779,9 @@
} }
}, },
"html5sortable": { "html5sortable": {
"version": "0.9.8", "version": "0.9.16",
"resolved": "https://registry.npmjs.org/html5sortable/-/html5sortable-0.9.8.tgz", "resolved": "https://registry.npmjs.org/html5sortable/-/html5sortable-0.9.16.tgz",
"integrity": "sha512-QodBiv8LdTZHot3EMzNHTeKn/nHm66BlvmcFvMFMPVm4mYN5S0uKvDxFnzRltFvrIjvps7OVRrxBJQX8aLnxWg==" "integrity": "sha512-GjCFl7w+Z6bQNs6lU5fhPqLoC5aRbFbQtL9rgqGR3Fs0GCRCrGjoRusZkhWaQpMr4eN25BlkviYA2nH+hFnDoA=="
}, },
"https-browserify": { "https-browserify": {
"version": "1.0.0", "version": "1.0.0",

View File

@ -3,7 +3,7 @@
"name": "@flarum/tags", "name": "@flarum/tags",
"dependencies": { "dependencies": {
"flarum-webpack-config": "0.1.0-beta.10", "flarum-webpack-config": "0.1.0-beta.10",
"html5sortable": "^0.9.8", "html5sortable": "^0.9.16",
"webpack": "^4.26.0", "webpack": "^4.26.0",
"webpack-cli": "^3.1.2" "webpack-cli": "^3.1.2"
}, },

View File

@ -1,4 +1,4 @@
import 'html5sortable'; import sortable from 'html5sortable/dist/html5sortable.es.js';
import Page from 'flarum/components/Page'; import Page from 'flarum/components/Page';
import Button from 'flarum/components/Button'; import Button from 'flarum/components/Button';
@ -80,70 +80,74 @@ export default class TagsPage extends Page {
} }
config() { config() {
this.$('ol, ul') sortable(this.$('ol, ul'), {
.sortable({connectWith: 'primary'}) acceptFrom: 'ol,ul'
.on('sortupdate', (e, ui) => { }).forEach(this.onSortUpdate.bind(this));
// If we've moved a tag from 'primary' to 'secondary', then we'll update }
// its attributes in our local store so that when we redraw the change
// will be made.
if (ui.startparent.is('ol') && ui.endparent.is('ul')) {
app.store.getById('tags', ui.item.data('id')).pushData({
attributes: {
position: null,
isChild: false
},
relationships: {parent: null}
});
}
// Construct an array of primary tag IDs and their children, in the same onSortUpdate(el) {
// order that they have been arranged in. el.addEventListener('sortupdate', (e) => {
const order = this.$('.TagList--primary > li') // If we've moved a tag from 'primary' to 'secondary', then we'll update
.map(function() { // its attributes in our local store so that when we redraw the change
return { // will be made.
id: $(this).data('id'), if (e.detail.origin.container instanceof HTMLOListElement && e.detail.destination.container instanceof HTMLUListElement) {
children: $(this).find('li') app.store.getById('tags', e.detail.item.getAttribute('data-id')).pushData({
.map(function() { attributes: {
return $(this).data('id'); position: null,
}).get() isChild: false
}; },
}).get(); relationships: {parent: null}
});
}
// Now that we have an accurate representation of the order which the // Construct an array of primary tag IDs and their children, in the same
// primary tags are in, we will update the tag attributes in our local // order that they have been arranged in.
// store to reflect this order. const order = this.$('.TagList--primary > li')
order.forEach((tag, i) => { .map(function() {
const parent = app.store.getById('tags', tag.id); return {
parent.pushData({ id: $(this).data('id'),
attributes: { children: $(this).find('li')
position: i, .map(function() {
isChild: false return $(this).data('id');
}, }).get()
relationships: {parent: null} };
}); }).get();
tag.children.forEach((child, j) => { // Now that we have an accurate representation of the order which the
app.store.getById('tags', child).pushData({ // primary tags are in, we will update the tag attributes in our local
attributes: { // store to reflect this order.
position: j, order.forEach((tag, i) => {
isChild: true const parent = app.store.getById('tags', tag.id);
}, parent.pushData({
relationships: {parent} attributes: {
}); position: i,
}); isChild: false
},
relationships: {parent: null}
}); });
app.request({ tag.children.forEach((child, j) => {
url: app.forum.attribute('apiUrl') + '/tags/order', app.store.getById('tags', child).pushData({
method: 'POST', attributes: {
data: {order} position: j,
isChild: true
},
relationships: {parent}
});
}); });
// A diff redraw won't work here, because sortable has mucked around
// with the DOM which will confuse Mithril's diffing algorithm. Instead
// we force a full reconstruction of the DOM.
m.redraw.strategy('all');
m.redraw();
}); });
app.request({
url: app.forum.attribute('apiUrl') + '/tags/order',
method: 'POST',
data: {order}
});
// A diff redraw won't work here, because sortable has mucked around
// with the DOM which will confuse Mithril's diffing algorithm. Instead
// we force a full reconstruction of the DOM.
m.redraw.strategy('all');
m.redraw();
});
} }
} }