Ghost/ghost/admin/app/components/gh-token-input/trigger.js
Kevin Ansfield 6a3cfc2ca8 Use token input to allow selection of multiple authors in PSM
requires https://github.com/TryGhost/Ghost/pull/9426
- fixed default token component display in {{gh-token-input}}
    - if no `tokenComponent` is passed to `{{gh-token-input}}` then it should default to the ember-drag-drop `draggable-object` component but instead it didn't output anything
    - put `draggable-object` in quotes because `{{component}}` needs a component name rather than an object
    - rename `option` attribute to `content` to match the default `{{draggable-object}}` interface
- add embedded `authors` attr to the Post model
    - ensure authors is populated when starting new post
    - add validation for empty authors list
- swap author dropdown for a token input in PSM
- show all post authors in posts list
- update tests for `authors`
  - always provide through an authors array
  - fix mirage serialisation for paginated responses (embedded records were not being serialised)
- unify tags and author inputs design
  - remove highlight of primary tags
  - highlight internal tags
  - remove unnecessary/redundant title attributes on tags
  - use SVG icon for "remove option" button in token inputs
2018-03-27 18:50:52 +01:00

67 lines
2.7 KiB
JavaScript

import EmberPowerSelectMultipleTrigger from 'ember-power-select/components/power-select-multiple/trigger';
import {assert} from '@ember/debug';
import {copy} from '@ember/object/internals';
import {get} from '@ember/object';
import {isBlank} from '@ember/utils';
export default EmberPowerSelectMultipleTrigger.extend({
actions: {
chooseOption(option) {
this.get('select').actions.choose(option);
},
handleOptionMouseDown(event) {
let action = this.get('extra.optionMouseDown');
if (action) {
return action(event);
}
},
handleOptionTouchStart(event) {
let action = this.get('extra.optionTouchStart');
if (action) {
return action(event);
}
},
reorderItems() {
// ember-drag-drop's sortable-objects has two-way bindings and will
// update EPS' selected value directly. We have to create a copy
// after sorting in order to force the onchange action to be triggered
this.get('select').actions.select(copy(this.get('select.selected')));
},
// copied directly from EPS, the default behaviour of stopping propagation
// of keydown events prevents our shortcuts from being triggered
onKeydown(e) {
let {onKeydown, select} = this.getProperties('onKeydown', 'select');
if (onKeydown && onKeydown(e) === false) {
e.stopPropagation();
return false;
}
if (e.keyCode === 8) {
e.stopPropagation();
if (isBlank(e.target.value)) {
let lastSelection = select.selected[select.selected.length - 1];
if (lastSelection) {
select.actions.select(this.get('buildSelection')(lastSelection, select), e);
if (typeof lastSelection === 'string') {
select.actions.search(lastSelection);
} else {
let searchField = this.get('searchField');
assert('`{{power-select-multiple}}` requires a `searchField` when the options are not strings to remove options using backspace', searchField);
select.actions.search(get(lastSelection, searchField));
}
select.actions.open(e);
}
}
}
// Disable the propagation cancellation so that our shortcuts still work
// } else if (e.keyCode >= 48 && e.keyCode <= 90 || e.keyCode === 32) { // Keys 0-9, a-z or SPACE
// e.stopPropagation();
// }
}
}
});