2018-02-13 21:00:54 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import layout from '../templates/components/koenig-card';
|
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {htmlSafe} from '@ember/string';
|
|
|
|
import {run} from '@ember/runloop';
|
|
|
|
|
|
|
|
const TICK_HEIGHT = 8;
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
layout,
|
2018-02-20 18:02:47 +03:00
|
|
|
attributeBindings: ['style'],
|
|
|
|
classNameBindings: ['isSelected:kg-card-selected'],
|
2018-02-13 21:00:54 +03:00
|
|
|
|
|
|
|
// attrs
|
|
|
|
icon: null,
|
2018-04-25 15:48:20 +03:00
|
|
|
iconClass: 'ih5 absolute stroke-midgrey-l2 mt1 nl16 kg-icon',
|
2018-02-13 21:00:54 +03:00
|
|
|
toolbar: null,
|
|
|
|
isSelected: false,
|
|
|
|
isEditing: false,
|
2018-02-22 16:46:24 +03:00
|
|
|
hasEditMode: true,
|
2018-02-13 21:00:54 +03:00
|
|
|
|
|
|
|
// properties
|
|
|
|
showToolbar: false,
|
|
|
|
toolbarWidth: 0,
|
|
|
|
toolbarHeight: 0,
|
|
|
|
|
2018-03-15 20:54:15 +03:00
|
|
|
// internal properties
|
|
|
|
_lastIsEditing: false,
|
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
// closure actions
|
|
|
|
selectCard() {},
|
|
|
|
editCard() {},
|
|
|
|
// hooks - when attached these will be fired on the individual card components
|
|
|
|
onSelect() {},
|
|
|
|
onDeselect() {},
|
|
|
|
onEnterEdit() {},
|
|
|
|
onLeaveEdit() {},
|
|
|
|
|
2018-02-20 18:02:47 +03:00
|
|
|
// TODO: replace with Spirit classes
|
|
|
|
style: computed(function () {
|
2018-02-20 20:34:31 +03:00
|
|
|
let baseStyles = 'cursor: default; caret-color: auto;';
|
2018-02-20 18:02:47 +03:00
|
|
|
|
|
|
|
return htmlSafe(baseStyles);
|
|
|
|
}),
|
|
|
|
|
2018-04-10 18:46:25 +03:00
|
|
|
toolbarStyle: computed('showToolbar', 'toolbarWidth', 'toolbarHeight', function () {
|
|
|
|
let showToolbar = this.get('showToolbar');
|
2018-02-13 21:00:54 +03:00
|
|
|
let width = this.get('toolbarWidth');
|
|
|
|
let height = this.get('toolbarHeight');
|
2018-04-10 18:46:25 +03:00
|
|
|
let styles = [];
|
2018-02-13 21:00:54 +03:00
|
|
|
|
2018-04-10 18:46:25 +03:00
|
|
|
styles.push(`top: -${height}px`);
|
|
|
|
styles.push(`left: calc(50% - ${width / 2}px)`);
|
|
|
|
|
|
|
|
if (!showToolbar) {
|
|
|
|
styles.push('pointer-events: none !important');
|
|
|
|
}
|
|
|
|
|
|
|
|
return htmlSafe(styles.join('; '));
|
2018-02-13 21:00:54 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
let isSelected = this.get('isSelected');
|
|
|
|
let isEditing = this.get('isEditing');
|
2018-02-22 16:46:24 +03:00
|
|
|
let hasEditMode = this.get('hasEditMode');
|
2018-02-13 21:00:54 +03:00
|
|
|
|
|
|
|
if (isSelected !== this._lastIsSelected) {
|
|
|
|
if (isSelected) {
|
|
|
|
this._fireWhenRendered(this._onSelect);
|
|
|
|
} else {
|
|
|
|
this._fireWhenRendered(this._onDeselect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEditing !== this._lastIsEditing) {
|
2018-02-22 16:46:24 +03:00
|
|
|
if (!hasEditMode) {
|
|
|
|
isEditing = false;
|
|
|
|
} else if (isEditing) {
|
2018-02-13 21:00:54 +03:00
|
|
|
this._onEnterEdit();
|
|
|
|
} else {
|
|
|
|
this._onLeaveEdit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._lastIsSelected = isSelected;
|
|
|
|
this._lastIsEditing = isEditing;
|
|
|
|
},
|
|
|
|
|
2018-04-03 20:34:01 +03:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._setToolbarProperties();
|
|
|
|
},
|
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
window.removeEventListener('keydown', this._onKeydownHandler);
|
|
|
|
this._removeMousemoveHandler();
|
|
|
|
},
|
|
|
|
|
|
|
|
mouseDown(event) {
|
|
|
|
let isSelected = this.get('isSelected');
|
|
|
|
let isEditing = this.get('isEditing');
|
2018-02-22 16:46:24 +03:00
|
|
|
let hasEditMode = this.get('hasEditMode');
|
2018-02-13 21:00:54 +03:00
|
|
|
|
|
|
|
// if we perform an action we want to prevent the mousedown from
|
|
|
|
// triggering a cursor position change which can result in multiple
|
|
|
|
// card select calls getting the component into an odd state. We also
|
|
|
|
// manually show the toolbar so that we're not relying on mousemove
|
|
|
|
if (!isSelected && !isEditing) {
|
|
|
|
this.selectCard();
|
|
|
|
this.set('showToolbar', true);
|
2018-02-22 16:46:24 +03:00
|
|
|
|
|
|
|
// in most situations we want to prevent default behaviour which
|
|
|
|
// can cause an underlying cursor position change but inputs and
|
|
|
|
// textareas are different and we want the focus to move to them
|
|
|
|
// immediately when clicked
|
|
|
|
let targetTagName = event.target.tagName;
|
|
|
|
let allowedTagNames = ['INPUT', 'TEXTAREA'];
|
|
|
|
if (!allowedTagNames.includes(targetTagName)) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
} else if (hasEditMode && isSelected && !isEditing) {
|
2018-02-13 21:00:54 +03:00
|
|
|
this.editCard();
|
|
|
|
this.set('showToolbar', true);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
doubleClick() {
|
2018-02-22 16:46:24 +03:00
|
|
|
if (this.get('hasEditMode') && !this.get('isEditing')) {
|
2018-02-13 21:00:54 +03:00
|
|
|
this.editCard();
|
|
|
|
this.set('showToolbar', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_onSelect() {
|
|
|
|
this._fireWhenRendered(this._showToolbar);
|
|
|
|
this._showToolbar();
|
|
|
|
this.onSelect();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onDeselect() {
|
|
|
|
this._hideToolbar();
|
|
|
|
this.onDeselect();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onEnterEdit() {
|
|
|
|
this._onKeydownHandler = run.bind(this, this._handleKeydown);
|
|
|
|
window.addEventListener('keydown', this._onKeydownHandler);
|
|
|
|
this.onEnterEdit();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onLeaveEdit() {
|
|
|
|
window.removeEventListener('keydown', this._onKeydownHandler);
|
|
|
|
this.onLeaveEdit();
|
|
|
|
},
|
|
|
|
|
2018-04-03 20:34:01 +03:00
|
|
|
_setToolbarProperties() {
|
2018-02-13 21:00:54 +03:00
|
|
|
if (this.get('toolbar')) {
|
2018-04-03 20:34:01 +03:00
|
|
|
let toolbar = this.element.querySelector('[data-toolbar="true"]');
|
2018-02-13 21:00:54 +03:00
|
|
|
let {width, height} = toolbar.getBoundingClientRect();
|
|
|
|
|
|
|
|
this.setProperties({
|
|
|
|
toolbarWidth: width,
|
|
|
|
toolbarHeight: height + TICK_HEIGHT
|
|
|
|
});
|
2018-04-03 20:34:01 +03:00
|
|
|
}
|
|
|
|
},
|
2018-02-13 21:00:54 +03:00
|
|
|
|
2018-04-03 20:34:01 +03:00
|
|
|
_showToolbar() {
|
|
|
|
// only show a toolbar if we have one
|
|
|
|
if (this.get('toolbar')) {
|
2018-04-25 17:11:45 +03:00
|
|
|
this._setToolbarProperties();
|
|
|
|
|
2018-02-13 21:00:54 +03:00
|
|
|
if (!this.get('showToolbar') && !this._onMousemoveHandler) {
|
|
|
|
this._onMousemoveHandler = run.bind(this, this._handleMousemove);
|
|
|
|
window.addEventListener('mousemove', this._onMousemoveHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_hideToolbar() {
|
|
|
|
this.set('showToolbar', false);
|
|
|
|
this._removeMousemoveHandler();
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleKeydown(event) {
|
|
|
|
if (event.code === 'Escape' && this.get('isEditing')) {
|
|
|
|
// run the select card routine with isEditing=false to exit edit mode
|
|
|
|
this.selectCard(false);
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_handleMousemove() {
|
|
|
|
if (!this.get('showToolbar')) {
|
|
|
|
this.set('showToolbar', true);
|
|
|
|
this._removeMousemoveHandler();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_removeMousemoveHandler() {
|
|
|
|
window.removeEventListener('mousemove', this._onMousemoveHandler);
|
|
|
|
this._onMousemoveHandler = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// convenience method for when we only want to run a method when our
|
|
|
|
// elements have been rendered
|
|
|
|
_fireWhenRendered(method) {
|
|
|
|
if (this.element) {
|
|
|
|
run.bind(this, method)();
|
|
|
|
} else {
|
|
|
|
run.scheduleOnce('afterRender', this, method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|