-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change users selectors from select2 to tom-select
- Loading branch information
Showing
8 changed files
with
55 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,20 @@ | ||
// I haven't found a way to include jquery plugins without making webpack repeat jQuery itself | ||
// the workaround is to use CDN providers and include them via standard <script> before any javascript_pack_tag | ||
// require("select2/src/js/jquery.select2.js") | ||
import TomSelect from "tom-select/dist/cjs/tom-select.popular"; | ||
|
||
$(() => { | ||
$("select.multiusers-select").each(function() { | ||
const url = $(this).attr("data-url"); | ||
$(this).select2({ | ||
ajax: { | ||
url: url, | ||
delay: 100, | ||
dataType: "json", | ||
processResults: (data) => { | ||
return { | ||
results: data | ||
} | ||
} | ||
}, | ||
escapeMarkup: (markup) => markup, | ||
templateSelection: (item) => `${item.text}`, | ||
minimumInputLength: 1, | ||
theme: "foundation" | ||
}); | ||
}); | ||
document.addEventListener("DOMContentLoaded", () => { | ||
const tagContainers = document.querySelectorAll("#users_list"); | ||
tagContainers.forEach((container) => { | ||
const { tmName, tmItems, tmNoResults } = container.dataset | ||
const config = { | ||
plugins: ["remove_button", "dropdown_input"], | ||
allowEmptyOption: true, | ||
items: JSON.parse(tmItems), | ||
render: { | ||
item: (data, escape) => `<div>${escape(data.text)}<input type="hidden" name="${tmName}[]" value="${data.value}" /></div>`, | ||
// eslint-disable-next-line camelcase | ||
...(tmNoResults && { no_results: () => `<div class="no-results">${tmNoResults}</div>` }) | ||
} | ||
}; | ||
|
||
$("select.user-select").each(function() { | ||
const url = $(this).attr("data-url"); | ||
const placeholder = $(this).attr("placeholder"); | ||
$(this).select2({ | ||
ajax: { | ||
url: url, | ||
delay: 100, | ||
dataType: "json", | ||
processResults: (data) => { | ||
return { | ||
results: data | ||
} | ||
} | ||
}, | ||
minimumInputLength: 1, | ||
placeholder: placeholder, | ||
theme: "foundation", | ||
allowClear: true, | ||
selectOnClose: true, | ||
escapeMarkup: (markup) => markup, | ||
templateSelection: (item) => `<b>${item.id}</b> - ${item.text}`, | ||
templateResult: (item) => { | ||
console.log(item) | ||
return `<div class="select2-result-repository"> | ||
<div class="select2-result-repository__avatar" style="background-image:url(${item.avatar || window.Notify.defaultAvatar})"> | ||
<div class="hex1"></div><div class="hex2"></div> | ||
</div> | ||
<div class="select2-result-repository__meta"> | ||
<b>${item.id}</b> - ${item.text} | ||
</div> | ||
</div>`; | ||
} | ||
}); | ||
|
||
$(this).on("select2:open", () => $(".select2-search__field").select()); | ||
$(this).on("select2:close", () => $("#note_body").select()); | ||
$(this).on("select2:clear", function () { | ||
$(this).on("select2:opening.cancelOpen", function (evt) { | ||
evt.preventDefault(); | ||
|
||
$(this).off("select2:opening.cancelOpen"); | ||
}); | ||
}); | ||
}); | ||
|
||
$("select.chapter-select").each(function() { | ||
const placeholder = $(this).attr("placeholder"); | ||
|
||
$(this).select2({ | ||
selectOnClose: true, | ||
tags: true, | ||
allowClear: true, | ||
theme: "foundation", | ||
placeholder: placeholder, | ||
createTag: function (params) { | ||
let term = $.trim(params.term); | ||
|
||
if (term === "") { | ||
return null; | ||
} | ||
let n = { | ||
id: term, | ||
text: term | ||
} | ||
return n; | ||
} | ||
}); | ||
|
||
$(this).on("select2:close", () => $("#note_body").select()); | ||
$(this).on("select2:clear", function () { | ||
$(this).on("select2:opening.cancelOpen", function (evt) { | ||
evt.preventDefault(); | ||
|
||
$(this).off("select2:opening.cancelOpen"); | ||
}); | ||
}); | ||
}); | ||
return new TomSelect(container, config) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters