@@ -14,6 +14,46 @@ window.CMBAP = window.CMBAP || {};
1414 app . doType = $wrap . find ( '.object-label' ) . length ;
1515 } ;
1616
17+ // Helper function to determine whteher we have exceeded our limit or not.
18+ app . _hasExceeded = function ( $wrap ) {
19+ var $input = app . getPostIdsInput ( $wrap ) ;
20+ var maxItems = $input . data ( 'max-items' ) ;
21+ var currentNumberItems = app . getPostIdsVal ( $input ) . length ;
22+
23+ return ( typeof maxItems !== "undefined" ) && currentNumberItems >= maxItems ;
24+ } ;
25+
26+ // Make sure we cannot attach more posts than we would like.
27+ app . updateReadOnly = function ( $wrap ) {
28+
29+ // If we have exceeded our limit, then ensure the user cannot attach more items.
30+ // If we haven't, make sure the user can attach items.
31+ if ( app . _hasExceeded ( $wrap ) ) {
32+ // Also ensure items aren't draggable
33+ // @see https://stackoverflow.com/questions/1324044/how-do-i-disable-a-jquery-ui-draggable
34+ // app.$retrievedPosts().draggable('disable');
35+ $wrap . find ( '.attached' ) . droppable ( "option" , "accept" , ".doesnotexist" ) ;
36+ } else {
37+ // app.$retrievedPosts().draggable('enable');
38+ $wrap . find ( '.attached' ) . droppable ( "option" , "accept" , ".retrieved li" ) ;
39+ }
40+ } ;
41+
42+ app . updateRemaining = function ( $wrap ) {
43+ var $input = app . getPostIdsInput ( $wrap ) ;
44+ var currentNumberItems = app . getPostIdsVal ( $input ) . length ;
45+ var maxItems = $input . data ( 'max-items' ) ;
46+ var $remainingLabel = $wrap . find ( '.attached-posts-remaining' ) ;
47+ var $remainingNumberLabel = $remainingLabel . find ( '.attached-posts-remaining-number' ) ;
48+ var remainingNumber = 0 ;
49+ if ( typeof maxItems !== "undefined" ) {
50+ // How many can we add?
51+ remainingNumber = maxItems - currentNumberItems ;
52+ // Show the label and update the number inside
53+ $remainingLabel . removeClass ( "hidden" ) ;
54+ $remainingNumberLabel . html ( remainingNumber ) ;
55+ }
56+ } ;
1757 app . init = function ( ) {
1858 app . cache ( ) ;
1959
@@ -23,6 +63,14 @@ window.CMBAP = window.CMBAP || {};
2363 // Allow the right list to be droppable and sortable
2464 app . makeDroppable ( ) ;
2565
66+
67+ $ ( ".attached-posts-wrap" ) . each ( function ( ) {
68+ // Update whether or not the lists should be editable by the user. This is usually when a user has attached too many items.
69+ app . updateReadOnly ( $ ( this ) ) ;
70+
71+ app . updateRemaining ( $ ( this ) )
72+ } ) ;
73+
2674 $ ( '.cmb2-wrap > .cmb2-metabox' )
2775 // Add posts when the plus icon is clicked
2876 . on ( 'click' , '.attached-posts-wrap .retrieved .add-remove' , app . _moveRowToAttached )
@@ -75,6 +123,8 @@ window.CMBAP = window.CMBAP || {};
75123 item . clone ( ) . appendTo ( $wrap . find ( '.attached' ) ) ;
76124
77125 app . resetAttachedListItems ( $wrap ) ;
126+ app . updateRemaining ( $wrap ) ;
127+
78128 } ;
79129
80130 // Add the items when the plus icon is clicked
@@ -87,6 +137,10 @@ window.CMBAP = window.CMBAP || {};
87137 var itemID = $li . data ( 'id' ) ;
88138 var $wrap = $li . parents ( '.attached-posts-wrap' ) ;
89139
140+ if ( app . _hasExceeded ( $wrap ) ) {
141+ return ;
142+ }
143+
90144 if ( $li . hasClass ( 'added' ) ) {
91145 return ;
92146 }
@@ -103,6 +157,8 @@ window.CMBAP = window.CMBAP || {};
103157 $wrap . find ( '.attached' ) . append ( $li . clone ( ) ) ;
104158
105159 app . resetAttachedListItems ( $wrap ) ;
160+ app . updateReadOnly ( $wrap ) ;
161+ app . updateRemaining ( $wrap ) ;
106162 } ;
107163
108164 // Remove items from our attached list when the minus icon is clicked
@@ -123,6 +179,8 @@ window.CMBAP = window.CMBAP || {};
123179 $wrap . find ( '.retrieved li[data-id="' + itemID + '"]' ) . removeClass ( 'added' ) ;
124180
125181 app . resetAttachedListItems ( $wrap ) ;
182+ app . updateReadOnly ( $wrap ) ;
183+ app . updateRemaining ( $wrap ) ;
126184 } ;
127185
128186 app . inputHasId = function ( $wrap , itemID ) {
0 commit comments