|
| 1 | +// Copyright 2011-2025, The Trustees of Indiana University and Northwestern |
| 2 | +// University. Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | + |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Unless required by applicable law or agreed to in writing, software distributed |
| 10 | +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | +// specific language governing permissions and limitations under the License. |
| 13 | +// --- END LICENSE_HEADER BLOCK --- |
| 14 | + |
| 15 | +// This script will enable support for the html5 form attribute |
| 16 | +// This should only be needed for IE but is currently applied wholesale |
| 17 | +// to all disjointed submit elements as is needed in some of the workflow steps |
| 18 | + |
| 19 | +$(() => add_new_playlist_option()); |
| 20 | + |
| 21 | +this.add_new_playlist_option = function() { |
| 22 | + const addnew = 'Add new playlist'; |
| 23 | + const select_element = $('#post_playlist_id'); |
| 24 | + const select_options = $('#post_playlist_id > option'); |
| 25 | + let add_success = false; |
| 26 | + let has_new_opt = false; |
| 27 | + |
| 28 | + // Prepend 'Add new playlist' option only when not present |
| 29 | + select_options.each(function() { |
| 30 | + if (this.value === addnew) { |
| 31 | + return has_new_opt = true; |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + if (!has_new_opt) { |
| 36 | + select_element.append(new Option(addnew)); |
| 37 | + } |
| 38 | + |
| 39 | + const getSearchTerm = () => $('span.select2-search--dropdown input').attr('data-testid', 'media-object-playlist-search-input').val(); |
| 40 | + |
| 41 | + const matchWithNew = function(params, data) { |
| 42 | + let term = params.term || ''; |
| 43 | + term = term.toLowerCase(); |
| 44 | + const text = data.text.toLowerCase(); |
| 45 | + if (text.includes(addnew.toLowerCase()) || text.includes(term)) { |
| 46 | + return data; |
| 47 | + } |
| 48 | + return null; |
| 49 | + }; |
| 50 | + |
| 51 | + const sortWithNew = data => data.sort(function(a, b) { |
| 52 | + if (b.text.trim() === addnew) { |
| 53 | + return 1; |
| 54 | + } |
| 55 | + if ((a.text < b.text) || (a.text.trim() === addnew)) { |
| 56 | + return -1; |
| 57 | + } |
| 58 | + if (a.text > b.text) { |
| 59 | + return 1; |
| 60 | + } |
| 61 | + return 0;}); |
| 62 | + |
| 63 | + const formatAddNew = function(data) { |
| 64 | + let term = getSearchTerm() || ''; |
| 65 | + if (data.text === addnew ) { |
| 66 | + if (term !== '') { |
| 67 | + term = addnew + ' "' + term + '"'; |
| 68 | + } else { |
| 69 | + term = addnew; |
| 70 | + } |
| 71 | + return `<a> \ |
| 72 | +<i class="fa fa-plus" aria-hidden="true"></i> <b>`+term+`</b> \ |
| 73 | +</a>`; |
| 74 | + } else { |
| 75 | + const start = data.text.toLowerCase().indexOf(term.toLowerCase()); |
| 76 | + if (start !== -1) { |
| 77 | + const end = (start+term.length)-1; |
| 78 | + return data.text.substring(0,start) + '<b>' + data.text.substring(start, end+1) + '</b>' + data.text.substring(end+1); |
| 79 | + } |
| 80 | + } |
| 81 | + return data.text; |
| 82 | + }; |
| 83 | + |
| 84 | + select_element.select2({ |
| 85 | + templateResult: formatAddNew, |
| 86 | + escapeMarkup(markup) { return markup; }, |
| 87 | + matcher: matchWithNew, |
| 88 | + sorter: sortWithNew |
| 89 | + }) |
| 90 | + .on('select2:selecting', |
| 91 | + function(evt) { |
| 92 | + const choice = evt.params.args.data.text; |
| 93 | + if (choice.indexOf(addnew) !== -1) { |
| 94 | + return showNewPlaylistModal(getSearchTerm()); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + var showNewPlaylistModal = function(playlistName) { |
| 99 | + //set to defaults first |
| 100 | + $('#new_playlist_submit').val('Create'); |
| 101 | + $('#new_playlist_submit').prop("disabled", false); |
| 102 | + $('#playlist_title').val(playlistName); |
| 103 | + $('#playlist_comment').val(""); |
| 104 | + $('#playlist_visibility_private').prop('checked', true); |
| 105 | + //remove any possible old errors |
| 106 | + $('#title_error').remove(); |
| 107 | + $('#playlist_title').parent().removeClass('has-error'); |
| 108 | + add_success = false; |
| 109 | + //finally show |
| 110 | + $('#add-playlist-modal').modal('show'); |
| 111 | + return true; |
| 112 | + }; |
| 113 | + |
| 114 | + $('#add-playlist-modal').on('hidden.bs.modal', function() { |
| 115 | + if (!add_success) { |
| 116 | + let newval; |
| 117 | + const op = select_element.children()[0]; |
| 118 | + if (op) { |
| 119 | + newval = op.value; |
| 120 | + } else { |
| 121 | + newval = -1; |
| 122 | + } |
| 123 | + return select_element.val(newval).trigger('change.select2'); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + $('#playlist_form').submit( |
| 128 | + function() { |
| 129 | + if($('#playlist_title').val()) { |
| 130 | + $('#new_playlist_submit').val('Saving...'); |
| 131 | + $('#new_playlist_submit').prop("disabled", true); |
| 132 | + return true; |
| 133 | + } |
| 134 | + if($('#title_error').length === 0) { |
| 135 | + $('#playlist_title') |
| 136 | + .after('<h5 id="title_error" class="error text-danger">Name is required</h5>'); |
| 137 | + $('#playlist_title').parent().addClass('has-error'); |
| 138 | + } |
| 139 | + return false; |
| 140 | + }); |
| 141 | + |
| 142 | + return $("#playlist_form").bind('ajax:success', |
| 143 | + function(event) { |
| 144 | + const [data, status, xhr] = Array.from(event.detail); |
| 145 | + $('#add-playlist-modal').modal('hide'); |
| 146 | + if (data.errors) { |
| 147 | + return console.log(data.errors.title[0]); |
| 148 | + } else { |
| 149 | + add_success = true; |
| 150 | + return select_element |
| 151 | + .append(new Option(data.title, data.id.toString(), true, true)) |
| 152 | + .trigger('change'); |
| 153 | + } |
| 154 | + }); |
| 155 | +}; |
0 commit comments