Skip to content

Commit

Permalink
Add to playlist (#5423)
Browse files Browse the repository at this point in the history
* [WIP] Add to playlist form for page re-design

* Fix form collapse and create form

* Invoke coffeescript to add 'Add new playlist' option into dropdown, wire form submissions to API calls

* Fix duplicating add new playlist option, event listeners for buttons and collapse panels

* Fix user feedback panel, tandem share/add to playlist panels

* Cleanup MEJS add-to-playlist code, handle no playlist case, general code cleanup

* Fix failing tests, change structure item label parsing with rebase, fix playlist form updating with canvas switches

* Update app/views/media_objects/_add_to_playlist.html.erb

Co-authored-by: Chris Colvard <[email protected]>

* Fix from code review

---------

Co-authored-by: Chris Colvard <[email protected]>
  • Loading branch information
Dananji and cjcolvar authored Oct 18, 2023
1 parent bf347c0 commit b00236e
Show file tree
Hide file tree
Showing 24 changed files with 652 additions and 909 deletions.
222 changes: 126 additions & 96 deletions app/assets/javascripts/add-playlist-option.js.coffee
Original file line number Diff line number Diff line change
@@ -1,108 +1,138 @@
addnew = 'Add new playlist'
select_element = $('#post_playlist_id')
add_success = false
# Copyright 2011-2023, The Trustees of Indiana University and Northwestern
# University. Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# --- END LICENSE_HEADER BLOCK ---

select_element.prepend(new Option(addnew))
# This script will enable support for the html5 form attribute
# This should only be needed for IE but is currently applied wholesale
# to all disjointed submit elements as is needed in some of the workflow steps

getSearchTerm = () ->
return $('span.select2-search--dropdown input').val()
$ ->
add_new_playlist_option()

matchWithNew = (params, data) ->
term = params.term || ''
term = term.toLowerCase()
text = data.text.toLowerCase()
if (text.includes(addnew.toLowerCase()) || text.includes(term))
return data
return null
@add_new_playlist_option = () ->
addnew = 'Add new playlist'
select_element = $('#post_playlist_id')
select_options = $('#post_playlist_id > option')
add_success = false
has_new_opt = false

sortWithNew = (data) ->
return data.sort((a, b) ->
if (b.text.trim() == addnew)
return 1
if (a.text < b.text || a.text.trim() == addnew)
return -1
if (a.text > b.text)
return 1
return 0)
# Prepend 'Add new playlist' option only when not present
select_options.each ->
if @value == addnew
has_new_opt = true

if !has_new_opt
select_element.prepend(new Option(addnew))

formatAddNew = (data) ->
term = getSearchTerm() || ''
if (data.text == addnew )
if (term != '')
term = addnew + ' "' + term + '"'
else
term = addnew
return '<a>
<i class="fa fa-plus" aria-hidden="true"></i> <b>'+term+'</b>
</a>'
else
start = data.text.toLowerCase().indexOf(term.toLowerCase())
if (start != -1)
end = start+term.length-1
return data.text.substring(0,start) + '<b>' + data.text.substring(start, end+1) + '</b>' + data.text.substring(end+1)
return data.text
getSearchTerm = () ->
return $('span.select2-search--dropdown input').val()

showNewPlaylistModal = (playlistName) ->
#set to defaults first
$('#new_playlist_submit').val('Create')
$('#new_playlist_submit').prop("disabled", false)
$('#playlist_title').val(playlistName)
$('#playlist_comment').val("")
$('#playlist_visibility_private').prop('checked', true)
#remove any possible old errors
$('#title_error').remove()
$('#playlist_title').parent().removeClass('has-error')
add_success = false
#finally show
$('#add-playlist-modal').modal('show')
return true
matchWithNew = (params, data) ->
term = params.term || ''
term = term.toLowerCase()
text = data.text.toLowerCase()
if (text.includes(addnew.toLowerCase()) || text.includes(term))
return data
return null

sortWithNew = (data) ->
return data.sort((a, b) ->
if (b.text.trim() == addnew)
return 1
if (a.text < b.text || a.text.trim() == addnew)
return -1
if (a.text > b.text)
return 1
return 0)

$('#add-playlist-modal').on('hidden.bs.modal', () ->
if (!add_success)
op = select_element.children()[1]
if (op)
newval = op.value
formatAddNew = (data) ->
term = getSearchTerm() || ''
if (data.text == addnew )
if (term != '')
term = addnew + ' "' + term + '"'
else
term = addnew
return '<a>
<i class="fa fa-plus" aria-hidden="true"></i> <b>'+term+'</b>
</a>'
else
newval = -1
select_element.val(newval).trigger('change.select2')
)
start = data.text.toLowerCase().indexOf(term.toLowerCase())
if (start != -1)
end = start+term.length-1
return data.text.substring(0,start) + '<b>' + data.text.substring(start, end+1) + '</b>' + data.text.substring(end+1)
return data.text

select_element.select2({
templateResult: formatAddNew
escapeMarkup:
(markup) -> return markup
matcher: matchWithNew
sorter: sortWithNew
})
.on('select2:selecting',
(evt) ->
choice = evt.params.args.data.text
if (choice.indexOf(addnew) != -1)
showNewPlaylistModal(getSearchTerm())
)

select_element.select2({
templateResult: formatAddNew
escapeMarkup:
(markup) -> return markup
matcher: matchWithNew
sorter: sortWithNew
})
.on('select2:selecting',
(evt) ->
choice = evt.params.args.data.text
if (choice.indexOf(addnew) != -1)
showNewPlaylistModal(getSearchTerm())
)
showNewPlaylistModal = (playlistName) ->
#set to defaults first
$('#new_playlist_submit').val('Create')
$('#new_playlist_submit').prop("disabled", false)
$('#playlist_title').val(playlistName)
$('#playlist_comment').val("")
$('#playlist_visibility_private').prop('checked', true)
#remove any possible old errors
$('#title_error').remove()
$('#playlist_title').parent().removeClass('has-error')
add_success = false
#finally show
$('#add-playlist-modal').modal('show')
return true

$('#playlist_form').submit(
() ->
if($('#playlist_title').val())
$('#new_playlist_submit').val('Saving...')
$('#new_playlist_submit').prop("disabled", true)
return true
if($('#title_error').length == 0)
$('#playlist_title')
.after('<h5 id="title_error" class="error text-danger">Name is required</h5>')
$('#playlist_title').parent().addClass('has-error')
return false
)
$('#add-playlist-modal').on('hidden.bs.modal', () ->
if (!add_success)
op = select_element.children()[1]
if (op)
newval = op.value
else
newval = -1
select_element.val(newval).trigger('change.select2')
)

$("#playlist_form").bind('ajax:success',
(event) ->
[data, status, xhr] = event.detail
$('#add-playlist-modal').modal('hide')
if (data.errors)
console.log(data.errors.title[0])
else
add_success = true
select_element
.append(new Option(data.title, data.id.toString(), true, true))
.trigger('change')
$('#playlist_form').submit(
() ->
if($('#playlist_title').val())
$('#new_playlist_submit').val('Saving...')
$('#new_playlist_submit').prop("disabled", true)
return true
if($('#title_error').length == 0)
$('#playlist_title')
.after('<h5 id="title_error" class="error text-danger">Name is required</h5>')
$('#playlist_title').parent().addClass('has-error')
return false
)

$("#playlist_form").bind('ajax:success',
(event) ->
[data, status, xhr] = event.detail
$('#add-playlist-modal').modal('hide')
if (data.errors)
console.log(data.errors.title[0])
else
add_success = true
select_element
.append(new Option(data.title, data.id.toString(), true, true))
.trigger('change')
)
2 changes: 0 additions & 2 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
//= stub mediaelement/plugins/markers
//= stub mediaelement/plugins/quality-avalon
//= stub mediaelement/plugins/quality-i18n
//= stub media_player_wrapper/mejs4_plugin_add_to_playlist
//= stub media_player_wrapper/mejs4_plugin_add_marker_to_playlist
//= stub media_player_wrapper/mejs4_plugin_track_scrubber
//= stub media_player_wrapper/mejs4_link_back
//= stub media_player_wrapper/mejs4_plugin_playlist_items
Expand Down
Loading

0 comments on commit b00236e

Please sign in to comment.