2017-11-21 05:53:09 -05:00
|
|
|
|
export default Ember.Mixin.create({
|
|
|
|
|
init() {
|
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
|
|
this.keys = {
|
|
|
|
|
TAB: 9,
|
|
|
|
|
ENTER: 13,
|
|
|
|
|
ESC: 27,
|
|
|
|
|
UP: 38,
|
|
|
|
|
DOWN: 40,
|
|
|
|
|
BACKSPACE: 8,
|
2018-03-22 06:29:55 -04:00
|
|
|
|
LEFT: 37,
|
|
|
|
|
RIGHT: 39,
|
|
|
|
|
A: 65
|
2017-11-21 05:53:09 -05:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
|
this._super();
|
|
|
|
|
|
2017-11-23 09:34:52 -05:00
|
|
|
|
$(document).off("mousedown.select-kit");
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
2017-11-21 13:08:59 -05:00
|
|
|
|
if (this.$header()) {
|
|
|
|
|
this.$header()
|
|
|
|
|
.off("blur.select-kit")
|
2017-11-22 05:29:30 -05:00
|
|
|
|
.off("focus.select-kit")
|
2017-11-21 13:08:59 -05:00
|
|
|
|
.off("keypress.select-kit")
|
|
|
|
|
.off("keydown.select-kit");
|
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
2017-11-21 13:08:59 -05:00
|
|
|
|
if (this.$filterInput()) {
|
|
|
|
|
this.$filterInput()
|
|
|
|
|
.off("change.select-kit")
|
|
|
|
|
.off("keydown.select-kit")
|
2017-11-22 05:29:30 -05:00
|
|
|
|
.off("keypress.select-kit");
|
2017-11-21 13:08:59 -05:00
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
|
|
$(document)
|
2017-11-23 09:34:52 -05:00
|
|
|
|
.on("mousedown.select-kit", event => {
|
2018-01-19 09:19:07 -05:00
|
|
|
|
if (!this.element || this.isDestroying || this.isDestroyed) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-12-28 10:12:45 -05:00
|
|
|
|
|
2018-01-19 09:19:07 -05:00
|
|
|
|
if (Ember.$.contains(this.element, event.target)) {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
if (!this.get("renderedBodyOnce")) return;
|
|
|
|
|
if (!this.get("isFocused")) return;
|
|
|
|
|
} else {
|
|
|
|
|
this.didClickOutside(event);
|
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
2018-01-19 09:19:07 -05:00
|
|
|
|
return true;
|
2017-11-21 05:53:09 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.$header()
|
|
|
|
|
.on("blur.select-kit", () => {
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (!this.get("isExpanded") && this.get("isFocused")) {
|
2017-11-21 05:53:09 -05:00
|
|
|
|
this.close();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.on("focus.select-kit", (event) => {
|
|
|
|
|
this.set("isFocused", true);
|
|
|
|
|
this._destroyEvent(event);
|
|
|
|
|
})
|
|
|
|
|
.on("keydown.select-kit", (event) => {
|
|
|
|
|
const keyCode = event.keyCode || event.which;
|
|
|
|
|
|
|
|
|
|
if (document.activeElement !== this.$header()[0]) return event;
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (keyCode === this.keys.TAB && event.shiftKey) { this.unfocus(event); }
|
|
|
|
|
if (keyCode === this.keys.TAB && !event.shiftKey) this.tabFromHeader(event);
|
|
|
|
|
if (Ember.isEmpty(this.get("filter")) && keyCode === this.keys.BACKSPACE) this.backspaceFromHeader(event);
|
2017-11-21 05:53:09 -05:00
|
|
|
|
if (keyCode === this.keys.ESC) this.escapeFromHeader(event);
|
|
|
|
|
if (keyCode === this.keys.ENTER) this.enterFromHeader(event);
|
|
|
|
|
if ([this.keys.UP, this.keys.DOWN].includes(keyCode)) this.upAndDownFromHeader(event);
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (Ember.isEmpty(this.get("filter")) &&
|
|
|
|
|
[this.keys.LEFT, this.keys.RIGHT].includes(keyCode)) {
|
|
|
|
|
this.leftAndRightFromHeader(event);
|
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
return event;
|
|
|
|
|
})
|
|
|
|
|
.on("keypress.select-kit", (event) => {
|
|
|
|
|
const keyCode = event.keyCode || event.which;
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (keyCode === this.keys.ENTER) return true;
|
|
|
|
|
if (keyCode === this.keys.TAB) return true;
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
|
|
this.expand(event);
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (this.get("filterable") || this.get("autoFilterable")) {
|
2017-11-21 05:53:09 -05:00
|
|
|
|
this.set("renderedFilterOnce", true);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 05:29:30 -05:00
|
|
|
|
if (keyCode >= 65 && keyCode <= 122) {
|
|
|
|
|
Ember.run.schedule("afterRender", () => {
|
|
|
|
|
this.$filterInput()
|
|
|
|
|
.focus()
|
|
|
|
|
.val(this.$filterInput().val() + String.fromCharCode(keyCode));
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.$filterInput()
|
|
|
|
|
.on("change.select-kit", (event) => {
|
2018-03-22 06:29:55 -04:00
|
|
|
|
this.send("onFilterComputedContent", $(event.target).val());
|
2017-11-21 05:53:09 -05:00
|
|
|
|
})
|
2017-11-21 10:59:03 -05:00
|
|
|
|
.on("keypress.select-kit", (event) => {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
})
|
2017-11-21 05:53:09 -05:00
|
|
|
|
.on("keydown.select-kit", (event) => {
|
|
|
|
|
const keyCode = event.keyCode || event.which;
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (Ember.isEmpty(this.get("filter")) &&
|
|
|
|
|
keyCode === this.keys.BACKSPACE &&
|
|
|
|
|
typeof this.didPressBackspaceFromFilter === "function") {
|
|
|
|
|
this.didPressBackspaceFromFilter(event);
|
2018-02-13 11:48:00 -05:00
|
|
|
|
};
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
|
|
|
|
if (keyCode === this.keys.TAB && event.shiftKey) { this.unfocus(event); }
|
|
|
|
|
if (keyCode === this.keys.TAB && !event.shiftKey) this.tabFromFilter(event);
|
2017-11-21 05:53:09 -05:00
|
|
|
|
if (keyCode === this.keys.ESC) this.escapeFromFilter(event);
|
|
|
|
|
if (keyCode === this.keys.ENTER) this.enterFromFilter(event);
|
|
|
|
|
if ([this.keys.UP, this.keys.DOWN].includes(keyCode)) this.upAndDownFromFilter(event);
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
|
|
|
|
if (Ember.isEmpty(this.get("filter")) &&
|
|
|
|
|
[this.keys.LEFT, this.keys.RIGHT].includes(keyCode)) {
|
|
|
|
|
this.leftAndRightFromFilter(event);
|
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
didPressTab(event) {
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (this.$highlightedRow().length && this.get("isExpanded")) {
|
|
|
|
|
this.close(event);
|
|
|
|
|
this.$header().focus();
|
|
|
|
|
const guid = this.$highlightedRow().attr("data-guid");
|
|
|
|
|
this.select(this._findComputedContentItemByGuid(guid));
|
2017-11-21 17:28:48 -05:00
|
|
|
|
return true;
|
2018-03-22 06:29:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Ember.isEmpty(this.get("filter"))) {
|
|
|
|
|
this.close(event);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
didPressEnter(event) {
|
|
|
|
|
if (!this.get("isExpanded")) {
|
|
|
|
|
this.expand(event);
|
|
|
|
|
} else if (this.$highlightedRow().length) {
|
|
|
|
|
this.close(event);
|
|
|
|
|
this.$header().focus();
|
|
|
|
|
const guid = this.$highlightedRow().attr("data-guid");
|
|
|
|
|
this.select(this._findComputedContentItemByGuid(guid));
|
2017-11-21 05:53:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
didClickSelectionItem(computedContentItem) {
|
|
|
|
|
this.focus();
|
|
|
|
|
this.deselect(computedContentItem);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
didClickRow(computedContentItem) {
|
2018-03-22 11:17:23 -04:00
|
|
|
|
this.close();
|
2018-03-22 06:29:55 -04:00
|
|
|
|
this.focus();
|
|
|
|
|
this.select(computedContentItem);
|
|
|
|
|
},
|
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
|
didPressEscape(event) {
|
|
|
|
|
this._destroyEvent(event);
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
|
|
|
|
if (this.get("highlightedSelection").length && this.get("isExpanded")) {
|
|
|
|
|
this.clearHighlightSelection();
|
|
|
|
|
} else {
|
|
|
|
|
this.unfocus(event);
|
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
didPressUpAndDownArrows(event) {
|
|
|
|
|
this._destroyEvent(event);
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
this.clearHighlightSelection();
|
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
|
const keyCode = event.keyCode || event.which;
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
2018-03-22 13:35:46 -04:00
|
|
|
|
if (!this.get("isExpanded")) {
|
2017-11-21 05:53:09 -05:00
|
|
|
|
this.expand(event);
|
|
|
|
|
|
|
|
|
|
if (this.$selectedRow().length === 1) {
|
|
|
|
|
this._highlightRow(this.$selectedRow());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
|
|
|
|
return;
|
2017-11-21 05:53:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 13:35:46 -04:00
|
|
|
|
const $rows = this.$rows();
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (!$rows.length) { return; }
|
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
|
if ($rows.length === 1) {
|
|
|
|
|
this._rowSelection($rows, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const direction = keyCode === 38 ? -1 : 1;
|
|
|
|
|
|
|
|
|
|
Ember.run.throttle(this, this._moveHighlight, direction, $rows, 32);
|
|
|
|
|
},
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
didPressBackspaceFromFilter(event) { this.didPressBackspace(event); },
|
2017-11-21 05:53:09 -05:00
|
|
|
|
didPressBackspace(event) {
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (!this.get("isExpanded")) {
|
|
|
|
|
this.expand();
|
|
|
|
|
if (event) event.stopImmediatePropagation();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (!this.get("selection").length) return;
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (!Ember.isEmpty(this.get("filter"))) {
|
|
|
|
|
this.clearHighlightSelection();
|
|
|
|
|
return;
|
2017-11-21 05:53:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (!this.get("highlightedSelection").length) {
|
|
|
|
|
// try to highlight the last non locked item from the current selection
|
|
|
|
|
Ember.makeArray(this.get("selection")).slice().reverse().some(selection => {
|
|
|
|
|
if (!Ember.get(selection, "locked")) {
|
|
|
|
|
this.highlightSelection(selection);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (event) event.stopImmediatePropagation();
|
|
|
|
|
} else {
|
|
|
|
|
this.deselect(this.get("highlightedSelection"));
|
|
|
|
|
if (event) event.stopImmediatePropagation();
|
2017-11-21 05:53:09 -05:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
didPressSelectAll() {
|
|
|
|
|
this.highlightSelection(Ember.makeArray(this.get("selection")));
|
|
|
|
|
},
|
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
|
didClickOutside(event) {
|
2018-03-22 06:29:55 -04:00
|
|
|
|
if (this.get("isExpanded") && $(event.target).parents(".select-kit").length) {
|
2017-11-21 05:53:09 -05:00
|
|
|
|
this.close(event);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-21 11:37:13 -05:00
|
|
|
|
this.close(event);
|
2017-11-21 05:53:09 -05:00
|
|
|
|
return;
|
|
|
|
|
},
|
|
|
|
|
|
2017-11-21 12:07:10 -05:00
|
|
|
|
// make sure we don’t propagate a click outside component
|
|
|
|
|
// to avoid closing a modal containing the component for example
|
|
|
|
|
click(event) {
|
|
|
|
|
this._destroyEvent(event);
|
|
|
|
|
},
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
didPressLeftAndRightArrows(event) {
|
|
|
|
|
if (!this.get("isExpanded")) {
|
|
|
|
|
this.expand();
|
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Ember.isEmpty(this.get("selection"))) return;
|
|
|
|
|
|
|
|
|
|
const keyCode = event.keyCode || event.which;
|
|
|
|
|
|
|
|
|
|
if (keyCode === this.keys.LEFT) {
|
|
|
|
|
const prev = this.get("highlightedSelection.lastObject");
|
|
|
|
|
const indexOfPrev = this.get("selection").indexOf(prev);
|
|
|
|
|
|
|
|
|
|
if (this.get("selection")[indexOfPrev - 1]) {
|
|
|
|
|
this.highlightSelection(this.get("selection")[indexOfPrev - 1]);
|
|
|
|
|
} else {
|
|
|
|
|
this.highlightSelection(this.get("selection.lastObject"));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const prev = this.get("highlightedSelection.firstObject");
|
|
|
|
|
const indexOfNext= this.get("selection").indexOf(prev);
|
|
|
|
|
|
|
|
|
|
if (this.get("selection")[indexOfNext + 1]) {
|
|
|
|
|
this.highlightSelection(this.get("selection")[indexOfNext + 1]);
|
|
|
|
|
} else {
|
|
|
|
|
this.highlightSelection(this.get("selection.firstObject"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
|
tabFromHeader(event) { this.didPressTab(event); },
|
|
|
|
|
tabFromFilter(event) { this.didPressTab(event); },
|
|
|
|
|
|
|
|
|
|
escapeFromHeader(event) { this.didPressEscape(event); },
|
|
|
|
|
escapeFromFilter(event) { this.didPressEscape(event); },
|
|
|
|
|
|
|
|
|
|
upAndDownFromHeader(event) { this.didPressUpAndDownArrows(event); },
|
|
|
|
|
upAndDownFromFilter(event) { this.didPressUpAndDownArrows(event); },
|
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
|
leftAndRightFromHeader(event) { this.didPressLeftAndRightArrows(event); },
|
|
|
|
|
leftAndRightFromFilter(event) { this.didPressLeftAndRightArrows(event); },
|
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
|
backspaceFromHeader(event) { this.didPressBackspace(event); },
|
|
|
|
|
|
|
|
|
|
enterFromHeader(event) { this.didPressEnter(event); },
|
|
|
|
|
enterFromFilter(event) { this.didPressEnter(event); },
|
|
|
|
|
|
|
|
|
|
_moveHighlight(direction, $rows) {
|
|
|
|
|
const currentIndex = $rows.index(this.$highlightedRow());
|
|
|
|
|
let nextIndex = currentIndex + direction;
|
|
|
|
|
|
|
|
|
|
if (nextIndex < 0) {
|
|
|
|
|
nextIndex = $rows.length - 1;
|
|
|
|
|
} else if (nextIndex >= $rows.length) {
|
|
|
|
|
nextIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._rowSelection($rows, nextIndex);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_rowSelection($rows, nextIndex) {
|
|
|
|
|
const highlightableValue = $rows.eq(nextIndex).attr("data-value");
|
|
|
|
|
const $highlightableRow = this.$findRowByValue(highlightableValue);
|
|
|
|
|
this._highlightRow($highlightableRow);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
_highlightRow($row) {
|
|
|
|
|
Ember.run.schedule("afterRender", () => {
|
|
|
|
|
$row.trigger("mouseover").focus();
|
|
|
|
|
this.focus();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|