Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dmstr/yii2-json-editor
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.5.0
Choose a base ref
...
head repository: dmstr/yii2-json-editor
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 5 commits
  • 2 files changed
  • 2 contributors

Commits on Aug 28, 2024

  1. json editor flysystem plugin

    gbisurgi committed Aug 28, 2024

    Partially verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
    Copy the full SHA
    713cd46 View commit details
  2. Merge pull request #38 from dmstr/feature/flysystem-editor

    json editor flysystem plugin
    schmunk42 authored Aug 28, 2024
    Copy the full SHA
    95cbb27 View commit details

Commits on Aug 29, 2024

  1. Copy the full SHA
    4d34898 View commit details

Commits on Sep 23, 2024

  1. Migrated to select2 input for flysystem picker. Allows for infinite s…

    …croll pagination and coherence with yii2 picker used with flysystem
    gbisurgi committed Sep 23, 2024
    Copy the full SHA
    33bba58 View commit details

Commits on Sep 25, 2024

  1. Copy the full SHA
    cb042a2 View commit details
Showing with 161 additions and 1 deletion.
  1. +5 −1 src/JsonEditorPluginsAsset.php
  2. +156 −0 src/assets/editors/flysystem.js
6 changes: 5 additions & 1 deletion src/JsonEditorPluginsAsset.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@

namespace dmstr\jsoneditor;

use kartik\select2\Select2Asset;
use kartik\select2\ThemeKrajeeBs3Asset;
use yii\web\AssetBundle;
use dosamigos\selectize\SelectizeAsset;
use yii\web\JqueryAsset;
@@ -19,6 +21,7 @@ class JsonEditorPluginsAsset extends AssetBundle

public $js = [
'editors/filefly.js',
'editors/flysystem.js',
'editors/ckeditor.js',
'editors/ckplugins/divarea.js',
];
@@ -30,7 +33,8 @@ class JsonEditorPluginsAsset extends AssetBundle
public $depends = [
JsonEditorAsset::class,
SelectizeAsset::class,
Select2Asset::class,
ThemeKrajeeBs3Asset::class,
JqueryAsset::class
];

}
156 changes: 156 additions & 0 deletions src/assets/editors/flysystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
var StringEditor = JSONEditor.defaults.editors.string

class FlysystemEditor extends StringEditor {
setValue(value) {
value = value || ''
if (this.value === value) return

this.input.value = value
this.value = value
this.setOption(value)
this.onChange()
}

build() {
this.selectInput = this.theme.getSelectInput()
this.input = this.theme.getFormInputField('hidden')

this.input.addEventListener('change', (e) => {
e.preventDefault()
e.stopPropagation()
this.onInputChange()
})

this.control = this.theme.getFormControl(this.label, this.input, this.description, this.infoButton)
this.container.appendChild(this.control)
this.control.appendChild(this.selectInput)
}

postBuild() {
super.postBuild()
this.initSelect2()
this.theme.afterInputReady(this.input)
}

hasImageExtension(path) {
if (typeof path !== 'string') {
throw 'path is not a string!'
}

const imageExtensions = ['jpg', 'jpeg', 'gif', 'svg', 'png', 'bmp'].map(ext => ext.toLowerCase())
const extension = path.split('.').pop().toLowerCase()

return imageExtensions.includes(extension)
}

initSelect2() {
this.destroySelect2()
this.searchUrl = this.schema?.searchUrl || '/filemanager/api/search'
this.streamUrl = this.schema?.streamUrl || '/filemanager/api/stream'

this.select2instance = $(this.selectInput).select2({
width: '100%',
theme: 'krajee-bs3',
ajax: {
cache: true,
url: this.searchUrl,
dataType: 'json',
delay: 250,
data: (params) => ({
q: params.term,
page: params.page || 0 // Send the page number to the server
}),
processResults: (data, params) => {
params.page = params.page || 0

return {
results: data.results.map(item => ({
id: item.fullPath
})),
pagination: {
more: data.pagination.more // Indicate if there are more results https://select2.org/data-sources/ajax#pagination
}
}
}
},
error: (jqXHR, textStatus, errorThrown) => {
console.error('AJAX error:', textStatus, errorThrown)
},
placeholder: 'Search for a file...',
allowClear: true,
debug: true,
minimumInputLength: 0,
templateResult: (item) => {
if (item.loading) return item.id
if (this.hasImageExtension(item.id)) {
return $(`<span><img src='${this.streamUrl}?path=${item.id}' style='max-width: 70px; max-height: 70px;' /> ${item.id}</span>`)
}
return $(`<span><i class='fa fa-file' style='font-size: 50px;'></i> ${item.id}</span>`)
},
templateSelection: (item) => {
if (!item.id) {
return item.text;
}

if (item.id && this.hasImageExtension(item.id)) {
return $(`<span><img src='${this.streamUrl}?path=${item.id}' style='max-width: 20px; max-height: 20px;' /> ${item.id}</span>`)
}
return $(`<span><i class='fa fa-file'></i> ${item.id}</span>`)
},
escapeMarkup: (markup) => markup
})

this.select2instance.on('select2:select', (e) => {
this.input.value = e.params.data.id
this.onInputChange()
})

this.select2instance.on('select2:clear', (e) => {
this.input.value = ''
this.onInputChange()

setTimeout(() => {
this.select2instance.select2('close');
})
})

this.setOption(this.input.value)
}

setOption(value) {
const option = new Option(value, value, true, true)
$(this.selectInput).append(option)
}

onInputChange() {
this.value = this.select2instance.val()

if (this.value !== '') {
this.setOption(this.value)
}

this.onChange(true)
}

destroy() {
this.destroySelect2()
if (this.label && this.label.parentNode) this.label.parentNode.removeChild(this.label)
if (this.description && this.description.parentNode) this.description.parentNode.removeChild(this.description)
if (this.input && this.input.parentNode) this.input.parentNode.removeChild(this.input)
super.destroy()
}

destroySelect2() {
if ($(this.input).data('select2')) {
$(this.input).select2('destroy')
}
}
}

JSONEditor.defaults.editors.flysystem = FlysystemEditor

JSONEditor.defaults.resolvers.unshift(function (schema) {
if (schema.type === 'string' && schema.format === 'flysystem') {
return 'flysystem'
}
})