@@ -210,23 +210,25 @@ export default (editor, url) => {
210210 const input = document . querySelector ( '#page_url' ) ;
211211 if ( input ) {
212212 templateInputChoices = new Choices ( input , {
213- removeItemButton : true
213+ removeItemButton : true ,
214+ allowHTML : true ,
214215 } ) ;
215216
217+ initLookup ( data . page_page ) ;
218+
216219 input . addEventListener ( 'search' , event => {
217220 clearTimeout ( lookupTimeout ) ;
218221 lookupTimeout = setTimeout ( serverLookup , 200 ) ;
219222 } ) ;
220223
221224 input . addEventListener ( 'choice' , event => {
222225 templateInputChoices . setChoices ( initData , 'value' , 'pagetitle' , true ) ;
223-
224- choicesData . page_page = event . detail . choice . value ;
225- choicesData . page_url = event . detail . choice . customProperties . url ;
226+ choicesData . page_page = event . detail . value ;
227+ choicesData . page_url = '[[~' + event . detail . value + ']]' ;
226228
227229 if ( ! data . link_text ) {
228- data . link_text = event . detail . choice . label ;
229- linkText . value ( event . detail . choice . label ) ;
230+ data . link_text = event . detail . label ;
231+ linkText . value ( event . detail . label ) ;
230232 }
231233
232234 const pageAnchorEl = document . getElementById ( 'page_anchor-l' ) ;
@@ -294,7 +296,7 @@ export default (editor, url) => {
294296
295297 const populateOptions = options => {
296298 const toRemove = [ ] ;
297-
299+ console . log ( templateInputChoices ) ;
298300 /* templateInputChoices.currentState.items.forEach(item => {
299301 if (item.active) {
300302 toRemove.push(item.value);
@@ -315,8 +317,6 @@ export default (editor, url) => {
315317 if ( query in lookupCache ) {
316318 populateOptions ( lookupCache [ query ] ) ;
317319 } else {
318- // @todo implement server lookup
319- console . info ( 'TinyMCE RTE Choices server lookup' ) ;
320320 const resourceSearchUrl = TinyMCERTE . editorConfig . connector_url + '?action=mgr/resource/search' + ( MODx . ctx ? ( '&wctx=' + MODx . ctx ) : '' ) + '&HTTP_MODAUTH=' + MODx . siteId + '&query=' + query + '&limit=10&sort=pagetitle&dir=ASC' ;
321321 fetch ( resourceSearchUrl ) . then ( function ( response ) {
322322 return response . json ( ) ;
@@ -327,6 +327,25 @@ export default (editor, url) => {
327327 }
328328 } ;
329329
330+ const initLookup = ( value ) => {
331+ if ( value ) {
332+ const resourceSearchUrl = TinyMCERTE . editorConfig . connector_url + '?action=mgr/resource/search' + ( MODx . ctx ? ( '&wctx=' + MODx . ctx ) : '' ) + '&HTTP_MODAUTH=' + MODx . siteId + '&id=' + value + '&limit=1' ;
333+ fetch ( resourceSearchUrl ) . then ( function ( response ) {
334+ return response . json ( ) ;
335+ } ) . then ( function ( data ) {
336+ initData = data . results ;
337+ templateInputChoices . setValue ( [ {
338+ id : data . results [ 0 ] . id ,
339+ highlighted : true ,
340+ active : true ,
341+ selected : true ,
342+ label : data . results [ 0 ] . pagetitle ,
343+ value : data . results [ 0 ] . value ,
344+ } ] ) ;
345+ } )
346+ }
347+ }
348+
330349 initChoices ( ) ;
331350 }
332351
0 commit comments