2017-11-21 05:53:09 -05:00
|
|
|
function checkSelectKitIsNotExpanded(selector) {
|
2018-07-24 14:12:09 -04:00
|
|
|
if (find(selector).hasClass("is-expanded")) {
|
|
|
|
throw new Error(
|
|
|
|
"You expected select-kit to be collapsed but it is expanded."
|
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkSelectKitIsNotCollapsed(selector) {
|
2018-07-24 14:12:09 -04:00
|
|
|
if (!find(selector).hasClass("is-expanded")) {
|
|
|
|
throw new Error(
|
|
|
|
"You expected select-kit to be expanded but it is collapsed."
|
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
Ember.Test.registerAsyncHelper("expandSelectKit", function(app, selector) {
|
2017-11-21 05:53:09 -05:00
|
|
|
checkSelectKitIsNotExpanded(selector);
|
2018-07-24 14:12:09 -04:00
|
|
|
click(selector + " .select-kit-header");
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
Ember.Test.registerAsyncHelper("collapseSelectKit", function(app, selector) {
|
2017-11-21 05:53:09 -05:00
|
|
|
checkSelectKitIsNotCollapsed(selector);
|
2018-07-24 14:12:09 -04:00
|
|
|
click(selector + " .select-kit-header");
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
Ember.Test.registerAsyncHelper("selectKitFillInFilter", function(
|
|
|
|
app,
|
|
|
|
filter,
|
|
|
|
selector
|
|
|
|
) {
|
2017-12-22 07:08:12 -05:00
|
|
|
checkSelectKitIsNotCollapsed(selector);
|
2018-07-24 14:12:09 -04:00
|
|
|
fillIn(selector + " .filter-input", filter);
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
Ember.Test.registerAsyncHelper("selectKitSelectRowByValue", function(
|
|
|
|
app,
|
|
|
|
value,
|
|
|
|
selector
|
|
|
|
) {
|
2017-12-22 07:08:12 -05:00
|
|
|
checkSelectKitIsNotCollapsed(selector);
|
|
|
|
click(selector + " .select-kit-row[data-value='" + value + "']");
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
Ember.Test.registerAsyncHelper("selectKitSelectRowByName", function(
|
|
|
|
app,
|
|
|
|
name,
|
|
|
|
selector
|
|
|
|
) {
|
2017-12-22 07:08:12 -05:00
|
|
|
checkSelectKitIsNotCollapsed(selector);
|
|
|
|
click(selector + " .select-kit-row[data-name='" + name + "']");
|
|
|
|
});
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
Ember.Test.registerAsyncHelper("selectKitSelectNoneRow", function(
|
|
|
|
app,
|
|
|
|
selector
|
|
|
|
) {
|
2017-12-22 07:08:12 -05:00
|
|
|
checkSelectKitIsNotCollapsed(selector);
|
|
|
|
click(selector + " .select-kit-row.none");
|
|
|
|
});
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
Ember.Test.registerAsyncHelper("selectKitSelectRowByIndex", function(
|
|
|
|
app,
|
|
|
|
index,
|
|
|
|
selector
|
|
|
|
) {
|
2017-12-22 07:08:12 -05:00
|
|
|
checkSelectKitIsNotCollapsed(selector);
|
|
|
|
click(find(selector + " .select-kit-row").eq(index));
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
|
2018-07-29 16:51:32 -04:00
|
|
|
Ember.Test.registerAsyncHelper("keyboardHelper", function(
|
|
|
|
app,
|
|
|
|
value,
|
|
|
|
target,
|
|
|
|
selector
|
|
|
|
) {
|
|
|
|
function createEvent(element, keyCode, options) {
|
|
|
|
element = element || ".filter-input";
|
|
|
|
selector = find(selector).find(element);
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var type = options.type || "keydown";
|
|
|
|
var event = jQuery.Event(type);
|
|
|
|
event.keyCode = keyCode;
|
|
|
|
if (options && options.metaKey) {
|
|
|
|
event.metaKey = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
andThen(() => {
|
|
|
|
find(selector).trigger(event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
case "enter":
|
|
|
|
return createEvent(target, 13);
|
|
|
|
case "backspace":
|
|
|
|
return createEvent(target, 8);
|
|
|
|
case "selectAll":
|
|
|
|
return createEvent(target, 65, { metaKey: true });
|
|
|
|
case "escape":
|
|
|
|
return createEvent(target, 27);
|
|
|
|
case "down":
|
|
|
|
return createEvent(target, 40);
|
|
|
|
case "up":
|
|
|
|
return createEvent(target, 38);
|
|
|
|
case "tab":
|
|
|
|
return createEvent(target, 9);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
function selectKit(selector) {
|
2017-12-22 07:08:12 -05:00
|
|
|
selector = selector || ".select-kit";
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
function rowHelper(row) {
|
|
|
|
return {
|
2018-07-24 14:12:09 -04:00
|
|
|
name: function() {
|
|
|
|
return row.attr("data-name");
|
|
|
|
},
|
|
|
|
icon: function() {
|
|
|
|
return row.find(".d-icon");
|
|
|
|
},
|
|
|
|
title: function() {
|
|
|
|
return row.attr("title");
|
|
|
|
},
|
|
|
|
value: function() {
|
|
|
|
return row.attr("data-value");
|
|
|
|
},
|
|
|
|
exists: function() {
|
|
|
|
return exists(row);
|
|
|
|
},
|
|
|
|
el: function() {
|
|
|
|
return row;
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function headerHelper(header) {
|
|
|
|
return {
|
2018-07-24 14:12:09 -04:00
|
|
|
value: function() {
|
|
|
|
return header.attr("data-value");
|
|
|
|
},
|
|
|
|
name: function() {
|
|
|
|
return header.attr("data-name");
|
|
|
|
},
|
|
|
|
label: function() {
|
|
|
|
return header.text().trim();
|
|
|
|
},
|
|
|
|
icon: function() {
|
|
|
|
return header.find(".icon");
|
|
|
|
},
|
|
|
|
title: function() {
|
|
|
|
return header.attr("title");
|
|
|
|
},
|
|
|
|
el: function() {
|
|
|
|
return header;
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function filterHelper(filter) {
|
|
|
|
return {
|
2018-07-24 14:12:09 -04:00
|
|
|
icon: function() {
|
|
|
|
return filter.find(".d-icon");
|
|
|
|
},
|
|
|
|
exists: function() {
|
|
|
|
return exists(filter);
|
|
|
|
},
|
|
|
|
el: function() {
|
|
|
|
return filter;
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2017-12-22 07:08:12 -05:00
|
|
|
expand: function() {
|
2018-07-29 16:51:32 -04:00
|
|
|
return expandSelectKit(selector);
|
2018-07-24 14:12:09 -04:00
|
|
|
},
|
|
|
|
|
2017-12-22 07:08:12 -05:00
|
|
|
collapse: function() {
|
2018-07-29 16:51:32 -04:00
|
|
|
return collapseSelectKit(selector);
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2017-12-22 07:08:12 -05:00
|
|
|
selectRowByIndex: function(index) {
|
|
|
|
selectKitSelectRowByIndex(index, selector);
|
|
|
|
return selectKit(selector);
|
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2017-12-22 07:08:12 -05:00
|
|
|
selectRowByValue: function(value) {
|
2018-07-29 16:51:32 -04:00
|
|
|
return selectKitSelectRowByValue(value, selector);
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2018-08-30 05:20:42 -04:00
|
|
|
// Remove when stable is updated to Discourse 2.1
|
|
|
|
selectRowByValueAwait: function(value) {
|
|
|
|
return selectKitSelectRowByValue(value, selector);
|
|
|
|
},
|
|
|
|
|
2017-12-22 07:08:12 -05:00
|
|
|
selectRowByName: function(name) {
|
|
|
|
selectKitSelectRowByValue(name, selector);
|
|
|
|
return selectKit(selector);
|
|
|
|
},
|
|
|
|
|
|
|
|
selectNoneRow: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return selectKitSelectNoneRow(selector);
|
|
|
|
},
|
|
|
|
|
2017-12-22 07:08:12 -05:00
|
|
|
fillInFilter: function(filter) {
|
2018-07-24 14:12:09 -04:00
|
|
|
return selectKitFillInFilter(filter, selector);
|
|
|
|
},
|
|
|
|
|
2018-07-29 16:51:32 -04:00
|
|
|
keyboard: function(value, target) {
|
|
|
|
return keyboardHelper(value, target, selector);
|
2018-07-24 14:12:09 -04:00
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2017-12-22 07:08:12 -05:00
|
|
|
isExpanded: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return find(selector).hasClass("is-expanded");
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
isFocused: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return find(selector).hasClass("is-focused");
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
isHidden: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return find(selector).hasClass("is-hidden");
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
header: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return headerHelper(find(selector).find(".select-kit-header"));
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
filter: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return filterHelper(find(selector).find(".select-kit-filter"));
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
rows: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return find(selector).find(".select-kit-row");
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
rowByValue: function(value) {
|
2018-07-24 14:12:09 -04:00
|
|
|
return rowHelper(
|
|
|
|
find(selector).find('.select-kit-row[data-value="' + value + '"]')
|
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
rowByName: function(name) {
|
2018-07-24 14:12:09 -04:00
|
|
|
return rowHelper(
|
|
|
|
find(selector).find('.select-kit-row[data-name="' + name + '"]')
|
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
rowByIndex: function(index) {
|
2018-07-24 14:12:09 -04:00
|
|
|
return rowHelper(
|
|
|
|
find(selector).find(".select-kit-row:eq(" + index + ")")
|
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
el: function() {
|
|
|
|
return find(selector);
|
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2017-12-22 07:08:12 -05:00
|
|
|
noneRow: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return rowHelper(find(selector).find(".select-kit-row.none"));
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2018-04-05 10:45:19 -04:00
|
|
|
validationMessage: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
var validationMessage = find(selector).find(".validation-message");
|
2018-04-05 10:45:19 -04:00
|
|
|
|
|
|
|
if (validationMessage.length) {
|
|
|
|
return validationMessage.html().trim();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-12-22 07:08:12 -05:00
|
|
|
selectedRow: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return rowHelper(find(selector).find(".select-kit-row.is-selected"));
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
highlightedRow: function() {
|
2018-07-24 14:12:09 -04:00
|
|
|
return rowHelper(find(selector).find(".select-kit-row.is-highlighted"));
|
2017-12-22 07:08:12 -05:00
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2018-07-24 14:12:09 -04:00
|
|
|
exists: function() {
|
|
|
|
return exists(selector);
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
};
|
|
|
|
}
|