@@ -60,6 +60,69 @@ $(document).on('turbolinks:load', function(){
60
60
$ ( ".delete-room" ) . click ( function ( ) {
61
61
showDeleteRoom ( this )
62
62
} )
63
+
64
+ $ ( '.selectpicker' ) . selectpicker ( {
65
+ liveSearchPlaceholder : "Start searching..."
66
+ } ) ;
67
+ // Fixes turbolinks issue with bootstrap select
68
+ $ ( window ) . trigger ( 'load.bs.select.data-api' ) ;
69
+
70
+ $ ( ".share-room" ) . click ( function ( ) {
71
+ // Update the path of save button
72
+ $ ( "#save-access" ) . attr ( "data-path" , $ ( this ) . data ( "path" ) )
73
+
74
+ // Get list of users shared with and display them
75
+ displaySharedUsers ( $ ( this ) . data ( "users-path" ) )
76
+ } )
77
+
78
+ $ ( "#shareRoomModal" ) . on ( "show.bs.modal" , function ( ) {
79
+ $ ( ".selectpicker" ) . selectpicker ( 'val' , '' )
80
+ } )
81
+
82
+ $ ( ".bootstrap-select" ) . on ( "click" , function ( ) {
83
+ $ ( ".bs-searchbox" ) . siblings ( ) . hide ( )
84
+ } )
85
+
86
+ $ ( ".bs-searchbox input" ) . on ( "input" , function ( ) {
87
+ if ( $ ( ".bs-searchbox input" ) . val ( ) == '' || $ ( ".bs-searchbox input" ) . val ( ) . length < 3 ) {
88
+ $ ( ".bs-searchbox" ) . siblings ( ) . hide ( )
89
+ } else {
90
+ $ ( ".bs-searchbox" ) . siblings ( ) . show ( )
91
+ }
92
+ } )
93
+
94
+ $ ( ".remove-share-room" ) . click ( function ( ) {
95
+ $ ( "#remove-shared-confirm" ) . parent ( ) . attr ( "action" , $ ( this ) . data ( "path" ) )
96
+ } )
97
+
98
+ // User selects an option from the Room Access dropdown
99
+ $ ( ".bootstrap-select" ) . on ( "changed.bs.select" , function ( ) {
100
+ // Get the uid of the selected user
101
+ let uid = $ ( ".selectpicker" ) . selectpicker ( 'val' )
102
+
103
+ // If the value was changed to blank, ignore it
104
+ if ( uid == "" ) return
105
+
106
+ let currentListItems = $ ( "#user-list li" ) . toArray ( ) . map ( user => $ ( user ) . data ( "uid" ) )
107
+
108
+ // Check to make sure that the user is not already there
109
+ if ( ! currentListItems . includes ( uid ) ) {
110
+ // Create the faded list item and display it
111
+ let option = $ ( "option[value='" + uid + "']" )
112
+
113
+ let listItem = document . createElement ( "li" )
114
+ listItem . setAttribute ( 'class' , 'list-group-item text-left not-saved add-access' ) ;
115
+ listItem . setAttribute ( "data-uid" , uid )
116
+
117
+ let spanItem = "<span class='avatar float-left mr-2'>" + option . text ( ) . charAt ( 0 ) + "</span> <span class='shared-user'>" +
118
+ option . text ( ) + " <span class='text-muted'>" + option . data ( "subtext" ) + "</span></span>" +
119
+ "<span class='text-primary float-right shared-user cursor-pointer' onclick='removeSharedUser(this)'><i class='fas fa-times'></i></span>"
120
+
121
+ listItem . innerHTML = spanItem
122
+
123
+ $ ( "#user-list" ) . append ( listItem )
124
+ }
125
+ } )
63
126
}
64
127
} ) ;
65
128
@@ -150,3 +213,44 @@ function ResetAccessCode(){
150
213
$ ( "#create-room-access-code" ) . text ( getLocalizedString ( "modal.create_room.access_code_placeholder" ) )
151
214
$ ( "#room_access_code" ) . val ( null )
152
215
}
216
+
217
+ function saveAccessChanges ( ) {
218
+ let listItemsToAdd = $ ( "#user-list li:not(.remove-shared)" ) . toArray ( ) . map ( user => $ ( user ) . data ( "uid" ) )
219
+
220
+ $ . post ( $ ( "#save-access" ) . data ( "path" ) , { add : listItemsToAdd } )
221
+ }
222
+
223
+ // Get list of users shared with and display them
224
+ function displaySharedUsers ( path ) {
225
+ $ . get ( path , function ( users ) {
226
+ // Create list element and add to user list
227
+ var user_list_html = ""
228
+
229
+ users . forEach ( function ( user ) {
230
+ user_list_html += "<li class='list-group-item text-left' data-uid='" + user . uid + "'>"
231
+
232
+ if ( user . image ) {
233
+ user_list_html += "<img id='user-image' class='avatar float-left mr-2' src='" + user . image + "'></img>"
234
+ } else {
235
+ user_list_html += "<span class='avatar float-left mr-2'>" + user . name . charAt ( 0 ) + "</span>"
236
+ }
237
+ user_list_html += "<span class='shared-user'>" + user . name + "<span class='text-muted ml-1'>" + user . uid + "</span></span>"
238
+ user_list_html += "<span class='text-primary float-right shared-user cursor-pointer' onclick='removeSharedUser(this)'><i class='fas fa-times'></i></span>"
239
+ user_list_html += "</li>"
240
+ } )
241
+
242
+ $ ( "#user-list" ) . html ( user_list_html )
243
+ } ) ;
244
+ }
245
+
246
+ // Removes the user from the list of shared users
247
+ function removeSharedUser ( target ) {
248
+ let parentLI = target . closest ( "li" )
249
+
250
+ if ( parentLI . classList . contains ( "not-saved" ) ) {
251
+ parentLI . parentNode . removeChild ( parentLI )
252
+ } else {
253
+ parentLI . removeChild ( target )
254
+ parentLI . classList . add ( "remove-shared" )
255
+ }
256
+ }
0 commit comments