2016-01-15 17:58:48 +08:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-01 21:34:20 +08:00
|
|
|
import sdk from './index';
|
2016-01-15 17:58:48 +08:00
|
|
|
|
2016-01-22 23:21:41 +08:00
|
|
|
function isMatch(query, name, uid) {
|
|
|
|
query = query.toLowerCase();
|
|
|
|
name = name.toLowerCase();
|
|
|
|
uid = uid.toLowerCase();
|
|
|
|
|
|
|
|
// direct prefix matches
|
|
|
|
if (name.indexOf(query) === 0 || uid.indexOf(query) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip @ on uid and try matching again
|
2016-01-23 00:18:23 +08:00
|
|
|
if (uid.length > 1 && uid[0] === "@" && uid.substring(1).indexOf(query) === 0) {
|
2016-01-22 23:21:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-23 00:18:23 +08:00
|
|
|
// split spaces in name and try matching constituent parts
|
2017-07-01 21:34:20 +08:00
|
|
|
const parts = name.split(" ");
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
2016-01-22 23:21:41 +08:00
|
|
|
if (parts[i].indexOf(query) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-15 17:58:48 +08:00
|
|
|
/*
|
|
|
|
* Converts various data models to Entity objects.
|
|
|
|
*
|
|
|
|
* Entity objects provide an interface for UI components to use to display
|
|
|
|
* members in a data-agnostic way. This means they don't need to care if the
|
|
|
|
* underlying data model is a RoomMember, User or 3PID data structure, it just
|
|
|
|
* cares about rendering.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Entity {
|
|
|
|
constructor(model) {
|
|
|
|
this.model = model;
|
|
|
|
}
|
|
|
|
|
|
|
|
getJsx() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
matches(queryString) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MemberEntity extends Entity {
|
|
|
|
getJsx() {
|
2017-07-01 21:34:20 +08:00
|
|
|
const MemberTile = sdk.getComponent("rooms.MemberTile");
|
2016-01-15 17:58:48 +08:00
|
|
|
return (
|
|
|
|
<MemberTile key={this.model.userId} member={this.model} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
matches(queryString) {
|
2016-01-22 23:21:41 +08:00
|
|
|
return isMatch(queryString, this.model.name, this.model.userId);
|
2016-01-15 17:58:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UserEntity extends Entity {
|
2016-01-19 01:12:35 +08:00
|
|
|
constructor(model, showInviteButton, inviteFn) {
|
|
|
|
super(model);
|
|
|
|
this.showInviteButton = Boolean(showInviteButton);
|
|
|
|
this.inviteFn = inviteFn;
|
2017-07-01 21:34:20 +08:00
|
|
|
this.onClick = this.onClick.bind(this);
|
2016-01-19 01:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onClick() {
|
|
|
|
if (this.inviteFn) {
|
|
|
|
this.inviteFn(this.model.userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 17:58:48 +08:00
|
|
|
getJsx() {
|
2017-07-01 21:34:20 +08:00
|
|
|
const UserTile = sdk.getComponent("rooms.UserTile");
|
2016-01-15 17:58:48 +08:00
|
|
|
return (
|
2016-01-19 01:12:35 +08:00
|
|
|
<UserTile key={this.model.userId} user={this.model}
|
2017-07-01 21:34:20 +08:00
|
|
|
showInviteButton={this.showInviteButton} onClick={this.onClick} />
|
2016-01-15 17:58:48 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
matches(queryString) {
|
2017-07-01 21:34:20 +08:00
|
|
|
const name = this.model.displayName || this.model.userId;
|
2016-01-22 23:21:41 +08:00
|
|
|
return isMatch(queryString, name, this.model.userId);
|
2016-01-15 17:58:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2016-01-23 01:20:52 +08:00
|
|
|
newEntity: function(jsx, matchFn) {
|
2017-07-01 21:34:20 +08:00
|
|
|
const entity = new Entity();
|
2016-01-23 01:20:52 +08:00
|
|
|
entity.getJsx = function() {
|
|
|
|
return jsx;
|
|
|
|
};
|
|
|
|
entity.matches = matchFn;
|
|
|
|
return entity;
|
|
|
|
},
|
|
|
|
|
2016-01-15 17:58:48 +08:00
|
|
|
/**
|
|
|
|
* @param {RoomMember[]} members
|
|
|
|
* @return {Entity[]}
|
|
|
|
*/
|
|
|
|
fromRoomMembers: function(members) {
|
|
|
|
return members.map(function(m) {
|
|
|
|
return new MemberEntity(m);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {User[]} users
|
2016-01-19 01:12:35 +08:00
|
|
|
* @param {boolean} showInviteButton
|
|
|
|
* @param {Function} inviteFn Called with the user ID.
|
2016-01-15 17:58:48 +08:00
|
|
|
* @return {Entity[]}
|
|
|
|
*/
|
2016-01-19 01:12:35 +08:00
|
|
|
fromUsers: function(users, showInviteButton, inviteFn) {
|
2016-01-15 17:58:48 +08:00
|
|
|
return users.map(function(u) {
|
2016-01-19 01:12:35 +08:00
|
|
|
return new UserEntity(u, showInviteButton, inviteFn);
|
2017-01-20 22:22:27 +08:00
|
|
|
});
|
2017-07-01 21:34:20 +08:00
|
|
|
},
|
2016-01-15 17:58:48 +08:00
|
|
|
};
|