| 1 |
import { on } from '../utility/on'; |
| 2 |
|
| 3 |
export class LinkPicker { |
| 4 |
constructor() { |
| 5 |
this.modal = document.getElementById('wp-link-wrap'); |
| 6 |
|
| 7 |
if (!this.modal) { |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
this.modal.classList.add('ag_link-modal'); |
| 12 |
this.select = this.modal.querySelector('#wp-link-submit'); |
| 13 |
|
| 14 |
this.select.addEventListener('click', this.update); |
| 15 |
|
| 16 |
Array.from(document.querySelectorAll('.button--link')).forEach((button) => { |
| 17 |
button.addEventListener('click', this.open); |
| 18 |
}); |
| 19 |
|
| 20 |
|
| 21 |
on('#wpwrap', 'click', '.ag-field--link .button--remove', this.clear); |
| 22 |
} |
| 23 |
|
| 24 |
open = (e) => { |
| 25 |
this.input = e.target.parentNode.querySelector('input'); |
| 26 |
wpLink.open('wpwrap'); |
| 27 |
} |
| 28 |
|
| 29 |
update = (e) => { |
| 30 |
const { href } = wpLink.getAttrs(); |
| 31 |
this.input.value = href; |
| 32 |
|
| 33 |
let display = this.input.parentNode.querySelector('.link-display'); |
| 34 |
|
| 35 |
if (display) { |
| 36 |
display.textContent = href; |
| 37 |
} else { |
| 38 |
display = document.createElement('span'); |
| 39 |
display.className = 'link-display'; |
| 40 |
display.textContent = href; |
| 41 |
this.input.insertAdjacentElement('afterend', display); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
if (href) { |
| 46 |
this.appendButton(e.target); |
| 47 |
} |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
appendButton = () => { |
| 53 |
if (!this.input.parentNode.querySelector('.button--remove')) { |
| 54 |
const button = document.createElement('button'); |
| 55 |
button.type = 'button'; |
| 56 |
button.className = 'button button--remove'; |
| 57 |
button.textContent = 'Remove link'; |
| 58 |
|
| 59 |
// this.input.parentNode.appendChild(button); |
| 60 |
|
| 61 |
this.input.insertAdjacentElement('beforebegin', button); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
clear = (e) => { |
| 66 |
e.target.parentNode.querySelector('input').value = ''; |
| 67 |
|
| 68 |
e.target.parentNode.removeChild(e.target.parentNode.querySelector('.link-display')); |
| 69 |
e.target.parentNode.removeChild(e.target); |
| 70 |
} |
| 71 |
}; |
| 72 |
|