-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate static files & improved project (#11)
* Improved project * Make icon usage dynamic between use class name and use as icon file * Improved example * bump version * Updated README * format codes
- Loading branch information
1 parent
5c105f4
commit 8ac3cf6
Showing
20 changed files
with
271 additions
and
293 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,2 +1,5 @@ | ||
include README.md | ||
include django_icon_picker/templates/django_icon_picker/icon_picker.html | ||
recursive-include django_icon_picker/static * | ||
recursive-include django_icon_picker/templates * | ||
ecursive-exclude * *.pyc | ||
exclude icon_picker.gif |
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
31 changes: 31 additions & 0 deletions
31
django-icon-picker/django_icon_picker/static/django_icon_picker/css/icon_picker.css
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
.icon-dropdown-list { | ||
max-height: 300px; | ||
overflow-y: auto; | ||
border: 1px solid #ddd; | ||
border-radius: 4px; | ||
background: white; | ||
box-shadow: 0 2px 4px rgba(0,0,0,0.1); | ||
} | ||
|
||
.icon-dropdown-item { | ||
display: flex; | ||
align-items: center; | ||
padding: 8px 12px; | ||
cursor: pointer; | ||
transition: background-color 0.2s; | ||
} | ||
|
||
.icon-dropdown-item:hover { | ||
background-color: #f5f5f5; | ||
} | ||
|
||
.icon-preview { | ||
width: 24px; | ||
height: 24px; | ||
margin-right: 12px; | ||
} | ||
|
||
.icon-name { | ||
font-size: 14px; | ||
color: #333; | ||
} |
150 changes: 150 additions & 0 deletions
150
django-icon-picker/django_icon_picker/static/django_icon_picker/js/icon_picker.js
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 |
---|---|---|
@@ -0,0 +1,150 @@ | ||
class IconPicker { | ||
constructor(options) { | ||
this.searchInput = document.getElementById(options.searchInputId); | ||
this.selectedIcon = document.getElementById("selectedIcon"); | ||
this.resultsDiv = document.getElementById("results"); | ||
this.prefixDropdown = document.getElementById("prefixDropdown"); | ||
this.colorPicker = document.getElementById("color"); | ||
this.savePath = options.savePath; | ||
this.selectedPrefix = ""; | ||
this.form = document.getElementById(`${options.model}_form`); | ||
this.objectId = options.objectId; | ||
this.model = options.model; | ||
this.icon = ""; | ||
|
||
this.init(); | ||
} | ||
|
||
init() { | ||
this.setupInitialIcon(); | ||
this.setupEventListeners(); | ||
this.fetchIconifyPrefixes(); | ||
} | ||
|
||
setupInitialIcon() { | ||
this.selectedIcon.src = this.searchInput.value.endsWith(".svg") | ||
? `/${this.searchInput.value}` | ||
: `https://api.iconify.design/${this.searchInput.value}.svg`; | ||
} | ||
|
||
setupEventListeners() { | ||
this.prefixDropdown.addEventListener("change", (event) => { | ||
this.selectedPrefix = event.target.value; | ||
}); | ||
|
||
this.searchInput.addEventListener("input", () => { | ||
const query = this.searchInput.value; | ||
if (query.length > 2) { | ||
this.searchIcons(query); | ||
} else { | ||
this.resultsDiv.innerHTML = ""; | ||
} | ||
}); | ||
|
||
this.form.addEventListener("submit", (event) => { | ||
if (this.savePath) { | ||
this.downloadAndSaveSvg( | ||
`${this.icon}.svg&color=${this.colorPicker.value.replace("#", "%23")}` | ||
); | ||
} | ||
this.form.submit(); | ||
}); | ||
} | ||
|
||
async searchIcons(query) { | ||
const response = await fetch( | ||
`https://api.iconify.design/search?query=${encodeURIComponent( | ||
query | ||
)}&limit=10&start=0&prefix=${this.selectedPrefix}` | ||
); | ||
const data = await response.json(); | ||
|
||
this.resultsDiv.innerHTML = ""; | ||
if (data.icons.length > 0) { | ||
const dropdownList = document.createElement("div"); | ||
dropdownList.className = "icon-dropdown-list"; | ||
|
||
data.icons.forEach((icon) => { | ||
const iconName = icon.replace(":", "-"); | ||
const color = this.colorPicker.value.replace("#", "%23"); | ||
const iconBaseUrl = `https://api.iconify.design/${icon}.svg`; | ||
const iconUrl = `${iconBaseUrl}?color=${color}`; | ||
|
||
const dropdownItem = this.createDropdownItem(icon, iconUrl); | ||
dropdownList.appendChild(dropdownItem); | ||
}); | ||
|
||
this.resultsDiv.appendChild(dropdownList); | ||
} else { | ||
this.resultsDiv.textContent = "No icons found."; | ||
} | ||
} | ||
|
||
createDropdownItem(icon, iconUrl) { | ||
const item = document.createElement("div"); | ||
item.className = "icon-dropdown-item"; | ||
|
||
const iconImg = document.createElement("img"); | ||
iconImg.src = iconUrl; | ||
iconImg.className = "icon-preview"; | ||
|
||
const iconText = document.createElement("span"); | ||
iconText.textContent = icon; | ||
iconText.className = "icon-name"; | ||
|
||
item.appendChild(iconImg); | ||
item.appendChild(iconText); | ||
|
||
item.addEventListener("click", () => { | ||
this.searchInput.value = icon; | ||
this.selectedIcon.src = iconUrl; | ||
this.resultsDiv.innerHTML = ""; | ||
this.icon = this.searchInput.value; | ||
if (this.savePath) { | ||
this.searchInput.value = | ||
this.savePath + `/${this.model}/icon-${this.objectId}.svg`; | ||
} else { | ||
this.searchInput.value = icon; | ||
} | ||
}); | ||
|
||
return item; | ||
} | ||
|
||
fetchIconifyPrefixes() { | ||
fetch("https://api.iconify.design/collections?pretty=1") | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
const prefixes = Object.keys(data); | ||
this.populatePrefixDropdown(prefixes); | ||
}) | ||
.catch((error) => console.error("Error fetching icon sets:", error)); | ||
} | ||
|
||
populatePrefixDropdown(prefixes) { | ||
const selectElement = document.createElement("select"); | ||
selectElement.id = "prefixDropdown"; | ||
selectElement.className = "form-select block"; | ||
|
||
prefixes.forEach((prefix) => { | ||
const option = document.createElement("option"); | ||
option.value = prefix; | ||
option.textContent = prefix; | ||
selectElement.appendChild(option); | ||
}); | ||
|
||
this.prefixDropdown.appendChild(selectElement); | ||
} | ||
|
||
downloadAndSaveSvg(svgIcon) { | ||
const downloadUrl = `/icon_picker/download-svg/?icon=${svgIcon}&id=${this.objectId}&model=${this.model}`; | ||
fetch(downloadUrl) | ||
.then((response) => response.text()) | ||
.then((data) => { | ||
this.searchInput.value = data; | ||
}) | ||
.catch((error) => { | ||
console.error("Error:", error); | ||
}); | ||
} | ||
} |
Oops, something went wrong.