@@ -2,39 +2,24 @@ import { ReactiveVar } from 'meteor/reactive-var';
22import { Tracker } from 'meteor/tracker' ;
33import { FlowRouter } from 'meteor/kadira:flow-router' ;
44import { Template } from 'meteor/templating' ;
5- import s from 'underscore.string ' ;
5+ import _ from 'underscore' ;
66
7- import { EmojiCustom } from '../../../models' ;
87import { RocketChatTabBar , SideNav , TabBar } from '../../../ui-utils' ;
8+ import { APIClient } from '../../../utils/client' ;
9+
10+ const LIST_SIZE = 50 ;
11+ const DEBOUNCE_TIME_TO_SEARCH_IN_MS = 500 ;
912
1013Template . adminEmoji . helpers ( {
1114 searchText ( ) {
1215 const instance = Template . instance ( ) ;
1316 return instance . filter && instance . filter . get ( ) ;
1417 } ,
15- isReady ( ) {
16- if ( Template . instance ( ) . ready != null ) {
17- return Template . instance ( ) . ready . get ( ) ;
18- }
19- return undefined ;
20- } ,
2118 customemoji ( ) {
22- return Template . instance ( ) . customemoji ( ) ;
19+ return Template . instance ( ) . emojis . get ( ) ;
2320 } ,
2421 isLoading ( ) {
25- if ( Template . instance ( ) . ready != null ) {
26- if ( ! Template . instance ( ) . ready . get ( ) ) {
27- return 'btn-loading' ;
28- }
29- }
30- } ,
31- hasMore ( ) {
32- if ( Template . instance ( ) . limit != null ) {
33- if ( typeof Template . instance ( ) . customemoji === 'function' ) {
34- return Template . instance ( ) . limit . get ( ) === Template . instance ( ) . customemoji ( ) . length ;
35- }
36- }
37- return false ;
22+ return Template . instance ( ) . isLoading . get ( ) ;
3823 } ,
3924 flexData ( ) {
4025 return {
@@ -48,26 +33,32 @@ Template.adminEmoji.helpers({
4833 if ( ( currentTarget . offsetHeight + currentTarget . scrollTop ) < ( currentTarget . scrollHeight - 100 ) ) {
4934 return ;
5035 }
51- if ( instance . limit . get ( ) > instance . customemoji ( ) . length ) {
52- return false ;
36+ const emojis = instance . emojis . get ( ) ;
37+ if ( instance . total . get ( ) > emojis . length ) {
38+ instance . offset . set ( instance . offset . get ( ) + LIST_SIZE ) ;
5339 }
54- instance . limit . set ( instance . limit . get ( ) + 50 ) ;
5540 } ;
5641 } ,
5742 onTableItemClick ( ) {
5843 const instance = Template . instance ( ) ;
5944 return function ( { _id } ) {
60- instance . tabBarData . set ( EmojiCustom . findOne ( { _id } ) ) ;
45+ instance . tabBarData . set ( {
46+ emoji : instance . emojis . get ( ) . find ( ( emoji ) => emoji . _id === _id ) ,
47+ onSuccess : instance . onSuccessCallback ,
48+ } ) ;
6149 instance . tabBar . open ( 'admin-emoji-info' ) ;
6250 } ;
6351 } ,
6452} ) ;
6553
66- Template . adminEmoji . onCreated ( function ( ) {
54+ Template . adminEmoji . onCreated ( async function ( ) {
6755 const instance = this ;
68- this . limit = new ReactiveVar ( 50 ) ;
56+ this . emojis = new ReactiveVar ( [ ] ) ;
57+ this . offset = new ReactiveVar ( 0 ) ;
58+ this . total = new ReactiveVar ( 0 ) ;
6959 this . filter = new ReactiveVar ( '' ) ;
70- this . ready = new ReactiveVar ( false ) ;
60+ this . query = new ReactiveVar ( { } ) ;
61+ this . isLoading = new ReactiveVar ( false ) ;
7162
7263 this . tabBar = new RocketChatTabBar ( ) ;
7364 this . tabBar . showGroup ( FlowRouter . current ( ) . route . name ) ;
@@ -90,27 +81,40 @@ Template.adminEmoji.onCreated(function() {
9081 template : 'adminEmojiInfo' ,
9182 order : 2 ,
9283 } ) ;
93-
94- this . autorun ( function ( ) {
95- const limit = instance . limit != null ? instance . limit . get ( ) : 0 ;
96- const subscription = instance . subscribe ( 'fullEmojiData' , '' , limit ) ;
97- instance . ready . set ( subscription . ready ( ) ) ;
84+ this . onSuccessCallback = ( ) => {
85+ this . offset . set ( 0 ) ;
86+ return this . loadEmojis ( this . query . get ( ) , this . offset . get ( ) ) ;
87+ } ;
88+ this . tabBarData . set ( {
89+ onSuccess : instance . onSuccessCallback ,
9890 } ) ;
9991
100- this . customemoji = function ( ) {
101- const filter = instance . filter != null ? s . trim ( instance . filter . get ( ) ) : '' ;
92+ this . loadEmojis = _ . debounce ( async ( query , offset ) => {
93+ this . isLoading . set ( true ) ;
94+ const { emojis, total } = await APIClient . v1 . get ( `emoji-custom.all?count=${ LIST_SIZE } &offset=${ offset } &query=${ JSON . stringify ( query ) } ` ) ;
95+ this . total . set ( total ) ;
96+ if ( offset === 0 ) {
97+ this . emojis . set ( emojis ) ;
98+ } else {
99+ this . emojis . set ( this . emojis . get ( ) . concat ( emojis ) ) ;
100+ }
101+ this . isLoading . set ( false ) ;
102+ } , DEBOUNCE_TIME_TO_SEARCH_IN_MS ) ;
102103
103- let query = { } ;
104+ this . autorun ( ( ) => {
105+ this . filter . get ( ) ;
106+ this . offset . set ( 0 ) ;
107+ } ) ;
104108
109+ this . autorun ( ( ) => {
110+ const filter = this . filter . get ( ) && this . filter . get ( ) . trim ( ) ;
111+ const offset = this . offset . get ( ) ;
105112 if ( filter ) {
106- const filterReg = new RegExp ( s . escapeRegExp ( filter ) , 'i' ) ;
107- query = { $or : [ { name : filterReg } , { aliases : filterReg } ] } ;
113+ const regex = { $regex : filter , $options : 'i' } ;
114+ return this . loadEmojis ( { $or : [ { name : regex } , { aliases : regex } ] } , offset ) ;
108115 }
109-
110- const limit = instance . limit != null ? instance . limit . get ( ) : 0 ;
111-
112- return EmojiCustom . find ( query , { limit, sort : { name : 1 } } ) . fetch ( ) ;
113- } ;
116+ return this . loadEmojis ( { } , offset ) ;
117+ } ) ;
114118} ) ;
115119
116120Template . adminEmoji . onRendered ( ( ) =>
0 commit comments