@@ -297,7 +297,8 @@ var refreshFoldersAndItems = function (data) {
297297
298298var hideNavAndShowEditor = function ( data ) {
299299 $ ( '#nav-buttons > ul' ) . addClass ( 'd-none' ) ;
300- $ ( '#content' ) . html ( data ) . removeClass ( 'preserve_actions_space' ) ;
300+ $ ( '#content' ) . html ( data ) ;
301+ $ ( '#pagination' ) . removeClass ( 'preserve_actions_space' )
301302 clearSelected ( ) ;
302303}
303304
@@ -309,20 +310,131 @@ function loadFolders() {
309310 } ) ;
310311}
311312
312- function loadItems ( ) {
313+ function generatePaginationHTML ( el , args ) {
314+ var template = '<li class="page-item"><\/li>' ;
315+ var linkTemplate = '<a class="page-link"><\/a>' ;
316+ var currentPage = args . currentPage ;
317+ var totalPage = args . totalPage ;
318+ var rangeStart = args . rangeStart ;
319+ var rangeEnd = args . rangeEnd ;
320+ var i ;
321+
322+ // Disable page range, display all the pages
323+ if ( args . pageRange === null ) {
324+ for ( i = 1 ; i <= totalPage ; i ++ ) {
325+ var button = $ ( template )
326+ . attr ( 'data-num' , i )
327+ . append ( $ ( linkTemplate ) . html ( i ) ) ;
328+ if ( i == currentPage ) {
329+ button . addClass ( 'active' ) ;
330+ }
331+ el . append ( button ) ;
332+ }
333+
334+ return ;
335+ }
336+
337+ if ( rangeStart <= 3 ) {
338+ for ( i = 1 ; i < rangeStart ; i ++ ) {
339+ var button = $ ( template )
340+ . attr ( 'data-num' , i )
341+ . append ( $ ( linkTemplate ) . html ( i ) ) ;
342+ if ( i == currentPage ) {
343+ button . addClass ( 'active' ) ;
344+ }
345+ el . append ( button ) ;
346+ }
347+ } else {
348+ var button = $ ( template )
349+ . attr ( 'data-num' , 1 )
350+ . append ( $ ( linkTemplate ) . html ( 1 ) ) ;
351+ el . append ( button ) ;
352+
353+ var button = $ ( template )
354+ . addClass ( 'disabled' )
355+ . append ( $ ( linkTemplate ) . html ( '...' ) ) ;
356+ el . append ( button ) ;
357+ }
358+
359+ for ( i = rangeStart ; i <= rangeEnd ; i ++ ) {
360+ var button = $ ( template )
361+ . attr ( 'data-num' , i )
362+ . append ( $ ( linkTemplate ) . html ( i ) ) ;
363+ if ( i == currentPage ) {
364+ button . addClass ( 'active' ) ;
365+ }
366+ el . append ( button ) ;
367+ }
368+
369+ if ( rangeEnd >= totalPage - 2 ) {
370+ for ( i = rangeEnd + 1 ; i <= totalPage ; i ++ ) {
371+ var button = $ ( template )
372+ . attr ( 'data-num' , i )
373+ . append ( $ ( linkTemplate ) . html ( i ) ) ;
374+ el . append ( button ) ;
375+ }
376+ } else {
377+ var button = $ ( template )
378+ . addClass ( 'disabled' )
379+ . append ( $ ( linkTemplate ) . html ( '...' ) ) ;
380+ el . append ( button ) ;
381+
382+ var button = $ ( template )
383+ . attr ( 'data-num' , totalPage )
384+ . append ( $ ( linkTemplate ) . html ( totalPage ) ) ;
385+ el . append ( button ) ;
386+ }
387+ }
388+
389+ function createPagination ( paginationSetting ) {
390+ var el = $ ( '<ul class="pagination" role="navigation"></ul>' ) ;
391+
392+ var currentPage = paginationSetting . current_page ;
393+ var pageRange = 5 ;
394+ var totalPage = Math . ceil ( paginationSetting . total / paginationSetting . per_page ) ;
395+
396+ var rangeStart = currentPage - pageRange ;
397+ var rangeEnd = currentPage + pageRange ;
398+
399+ if ( rangeEnd > totalPage ) {
400+ rangeEnd = totalPage ;
401+ rangeStart = totalPage - pageRange * 2 ;
402+ rangeStart = rangeStart < 1 ? 1 : rangeStart ;
403+ }
404+
405+ if ( rangeStart <= 1 ) {
406+ rangeStart = 1 ;
407+ rangeEnd = Math . min ( pageRange * 2 + 1 , totalPage ) ;
408+ }
409+
410+ generatePaginationHTML ( el , {
411+ totalPage : totalPage ,
412+ currentPage : currentPage ,
413+ pageRange : pageRange ,
414+ rangeStart : rangeStart ,
415+ rangeEnd : rangeEnd
416+ } ) ;
417+
418+ $ ( '#pagination' ) . append ( el ) ;
419+ }
420+
421+ function loadItems ( page ) {
313422 loading ( true ) ;
314- performLfmRequest ( 'jsonitems' , { show_list : show_list , sort_type : sort_type } , 'html' )
423+ performLfmRequest ( 'jsonitems' , { show_list : show_list , sort_type : sort_type , page : page || 1 } , 'html' )
315424 . done ( function ( data ) {
316425 selected = [ ] ;
317426 var response = JSON . parse ( data ) ;
318427 var working_dir = response . working_dir ;
319428 items = response . items ;
320429 var hasItems = items . length !== 0 ;
430+ var hasPaginator = ! ! response . paginator ;
321431 $ ( '#empty' ) . toggleClass ( 'd-none' , hasItems ) ;
322432 $ ( '#content' ) . html ( '' ) . removeAttr ( 'class' ) ;
433+ $ ( '#pagination' ) . html ( '' ) . removeAttr ( 'class' ) ;
323434
324435 if ( hasItems ) {
325- $ ( '#content' ) . addClass ( response . display ) . addClass ( 'preserve_actions_space' ) ;
436+ $ ( '#content' ) . addClass ( response . display ) ;
437+ $ ( '#pagination' ) . addClass ( 'preserve_actions_space' ) ;
326438
327439 items . forEach ( function ( item , index ) {
328440 var template = $ ( '#item-template' ) . clone ( )
@@ -352,6 +464,18 @@ function loadItems() {
352464 } ) ;
353465 }
354466
467+ if ( hasPaginator ) {
468+ createPagination ( response . paginator ) ;
469+
470+ $ ( '#pagination a' ) . on ( 'click' , function ( event ) {
471+ event . preventDefault ( ) ;
472+
473+ loadItems ( $ ( this ) . closest ( 'li' ) [ 0 ] . getAttribute ( 'data-num' ) ) ;
474+
475+ return false ;
476+ } ) ;
477+ }
478+
355479 $ ( '#nav-buttons > ul' ) . removeClass ( 'd-none' ) ;
356480
357481 $ ( '#working_dir' ) . val ( working_dir ) ;
@@ -558,6 +682,17 @@ function use(items) {
558682 }
559683 }
560684
685+ function useTinymce5 ( url ) {
686+ if ( ! usingTinymce5 ( ) ) { return ; }
687+
688+ parent . postMessage ( {
689+ mceAction : 'insert' ,
690+ content : url
691+ } ) ;
692+
693+ parent . postMessage ( { mceAction : 'close' } ) ;
694+ }
695+
561696 function useCkeditor3 ( url ) {
562697 if ( ! usingCkeditor3 ( ) ) { return ; }
563698
@@ -589,6 +724,8 @@ function use(items) {
589724
590725 useTinymce4AndColorbox ( url ) ;
591726
727+ useTinymce5 ( url ) ;
728+
592729 useCkeditor3 ( url ) ;
593730
594731 useFckeditor2 ( url ) ;
@@ -626,6 +763,10 @@ function usingTinymce4AndColorbox() {
626763 return ! ! getUrlParam ( 'field_name' ) ;
627764}
628765
766+ function usingTinymce5 ( ) {
767+ return ! ! getUrlParam ( 'editor' ) ;
768+ }
769+
629770function usingCkeditor3 ( ) {
630771 return ! ! getUrlParam ( 'CKEditor' ) || ! ! getUrlParam ( 'CKEditorCleanUpFuncNum' ) ;
631772}
@@ -635,7 +776,7 @@ function usingFckeditor2() {
635776}
636777
637778function usingWysiwygEditor ( ) {
638- return usingTinymce3 ( ) || usingTinymce4AndColorbox ( ) || usingCkeditor3 ( ) || usingFckeditor2 ( ) ;
779+ return usingTinymce3 ( ) || usingTinymce4AndColorbox ( ) || usingTinymce5 ( ) || usingCkeditor3 ( ) || usingFckeditor2 ( ) ;
639780}
640781
641782// ==================================
0 commit comments