javascript - Image upload manager -


i trying build image manager cms based on wordpress media manager.the cms being developed in yii2.

right able upload , view uploaded images. wanted next able add custom buttons (upload logo, upload header, etc) wich allow user call image manager in modal. either upload or pick image gallery , add images id hiddeninput field(logo, header, etc). having trouble in accomplishing this.

this have far:

i have 2 inputfields in 1 form, 1 logo , other favicon

logo hiddeninput: id -> site-logo_media_id

img preview: class -> preview_site-logo_media_id

button: id -> logo-button, custom attribute input_id -> site-logo_media_id, class-> showmodalbutton

        <?= $form->field($model, 'logo_media_id')->hiddeninput(['maxlength' => true]) ?>         <img class="preview_site-logo_media_id" src="" alt="">         <?= html::button('logo', ['title' => 'upload logo', 'id' => 'logo-button', 'input_id' => 'site-logo_media_id', 'class' => 'showmodalbutton btn btn-success']); ?> 

favicon hiddeninput: id -> site-favicon_media-id

img preview: class -> preview_site-favicon_media_id

button: id -> favicon-button, custom attribute input_id -> site-favicon_media_id, class-> showmodalbutton

        <?= $form->field($model, 'favicon_media_id')->hiddeninput(['maxlength' => true]) ?>         <img class="preview_site-favicon_media_id" src="" alt="">         <?= html::button('favicon', ['title' => 'upload favicon', 'id' => 'favicon-button', 'input_id' => 'site-favicon_media_id', 'class' => 'showmodalbutton btn btn-success']); ?> 

when 1 of buttons pressed modal called uploaded images , option upload more. images in gallery wrapped in li tags class gallery-item, attribute data-key containing image id.

javascript

show_file_manager();  function show_file_manager() {   $('.showmodalbutton').click(function() {     // opens modal     var input_id = $(this).attr("input_id");     var preview_class = "preview_" + $(this).attr("input_id");     // input_id gets hiddeninput id, preview class meant display selected images.     select_gallery_item(input_id, preview_class);     //the input_id , preview_class passed next function   }); }  function select_gallery_item(input_id, preview_class) {   $('.gallery-item').click(function() {     // gallery-item class name of li containting image     var file_id = $(this).attr('data-key');     // file_id image id     var src = $(this).children('img').attr('src');     // src gets value images src use in preview class     console.log(file_id);     if (file_id) {       $("#add-to-input").click(function() {         // button passes image id hiddeninput correct input id(site-logo_media-id, site-favicon_media-id) , images src preview class(preview_site-logo_media-id, preview_site-favicon_media-id)         $('#' + input_id).val(file_id);         $("." + preview_class).attr("src", src);       });     }   }); } 

the problems:

  • its seems function select_gallery_item (javascript) incremented every time .showmodalbutton run. pressing once shows console.log(file_id) once, pressing twice shows console.log(file_id) twice, etc

  • if press second button .showmodalbutton image id , src added both hiddeninputfields (site-favicon_media-id, site-logo_media-id)

i hope able convey problem, wil appreciated. doing wrong? how can make better? in advance.

the first problem caused select_gallery_item function. every time click modal button click event gets binded .gallery-item elements , #add-to-input element.

i guess trying pass parameters click events?

if check out question passing parameters click() & bind() event in jquery?

i guess second problem caused same reason. first click select image logo , gets binded #add-to-input , when change favicon first binded click event changes src , id logo well.

so try separate nested click bindings.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -