Skip to content

Commit

Permalink
add: possibility to show warning before detach image from picker
Browse files Browse the repository at this point in the history
  • Loading branch information
noam148 committed Oct 20, 2016
1 parent b34411f commit 5184beb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -68,6 +68,7 @@ To load the image picker see below (make sure you have a field in you table wher
echo $form->field($model, 'ImageManager_id_avatar')->widget(\noam148\imagemanager\components\ImageManagerInputWidget::className(), [
'aspectRatio' => (16/9), //set the aspect ratio
'showPreview' => true, //false to hide the preview
'showDeletePickedImageConfirm' => false, //on true show warning before detach image
]);
```
![Image widget](/docs/images/img_doc-image-widget.jpg)
Expand Down
10 changes: 9 additions & 1 deletion assets/source/js/script.imagemanager.input.js
@@ -1,5 +1,7 @@
var imageManagerInput = {
baseUrl: null,
//language
message: null,
//init imageManagerInput
init: function(){
//create modal
Expand Down Expand Up @@ -52,6 +54,13 @@ var imageManagerInput = {
var sFieldId = inputId;
var sFieldNameId = sFieldId+"_name";
var sImagePreviewId = sFieldId+"_image";
var bShowConfirm = JSON.parse($(".delete-selected-image[data-input-id='"+inputId+"']").data("show-delete-confirm"));
//show warning if bShowConfirm == true
if(bShowConfirm){
if(confirm(imageManagerInput.message.detachWarningMessage) == false){
return false;
}
}
//set input data
$('#'+sFieldId).val("");
$('#'+sFieldNameId).val("");
Expand All @@ -68,7 +77,6 @@ $(document).ready(function () {
//init Image manage
imageManagerInput.init();


//open media manager modal
$(document).on("click", ".open-modal-imagemanager", function () {
var aspectRatio = $(this).data("aspect-ratio");
Expand Down
20 changes: 17 additions & 3 deletions components/ImageManagerInputWidget.php
@@ -1,8 +1,10 @@
<?php
namespace noam148\imagemanager\components;

use Yii;
use yii\widgets\InputWidget;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use noam148\imagemanager\models\ImageManager;
use noam148\imagemanager\assets\ImageManagerInputAsset;
Expand All @@ -11,10 +13,19 @@ class ImageManagerInputWidget extends InputWidget{
//default ratio
public $aspectRatio = null; //option info: https://github.com/fengyuanchen/cropper/#aspectratio
public $cropViewMode = 1; //option info: https://github.com/fengyuanchen/cropper/#viewmode
public $showPreview = true;
public $showPreview = true;
public $showDeletePickedImageConfirm = false;

public function init(){

parent::init();
//set language
if (!isset(Yii::$app->i18n->translations['imagemanager'])) {
Yii::$app->i18n->translations['imagemanager'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@noam148/imagemanager/messages'
];
}
}

/**
Expand Down Expand Up @@ -50,7 +61,7 @@ public function run()
}
//end input group
$sHideClass = $ImageManager_id === null ? 'hide' : '';
$field .= "<a href='#' class='input-group-addon btn btn-primary delete-selected-image ".$sHideClass."' data-input-id='".$sFieldId."'><i class='glyphicon glyphicon-remove' aria-hidden='true'></i></a>";
$field .= "<a href='#' class='input-group-addon btn btn-primary delete-selected-image ".$sHideClass."' data-input-id='".$sFieldId."' data-show-delete-confirm='".($this->showDeletePickedImageConfirm ? "true" : "false")."'><i class='glyphicon glyphicon-remove' aria-hidden='true'></i></a>";
$field .= "<a href='#' class='input-group-addon btn btn-primary open-modal-imagemanager' data-aspect-ratio='".$this->aspectRatio."' data-crop-view-mode='".$this->cropViewMode."' data-input-id='".$sFieldId."'>";
$field .= "<i class='glyphicon glyphicon-folder-open' aria-hidden='true'></i>";
$field .= "</a></div>";
Expand Down Expand Up @@ -85,5 +96,8 @@ public function registerClientScript()
$sBaseUrl = Url::to(['imagemanager/manager']);
//set base url
$view->registerJs("imageManagerInput.baseUrl = '".$sBaseUrl."';");
$view->registerJs("imageManagerInput.message = ".Json::encode([
'detachWarningMessage' => Yii::t('imagemanager','Are you sure you want to detach the image?'),
]).";");
}
}
1 change: 1 addition & 0 deletions messages/en/imagemanager.php
@@ -1,6 +1,7 @@
<?php
return [
'Are you sure you want to delete this image?' => 'Are you sure you want to delete this image?',
'Are you sure you want to detach the image?' => 'Are you sure you want to detach the image?',
'Cancel' => 'Cancel',
'Created' => 'Created',
'Crop' => 'Crop',
Expand Down
1 change: 1 addition & 0 deletions messages/nl/imagemanager.php
@@ -1,6 +1,7 @@
<?php
return [
'Are you sure you want to delete this image?' => 'Weet je zeker dat je de afbeelding wilt verwijderen?',
'Are you sure you want to detach the image?' => 'Weet je zeker dat je de afbeelding wilt losmaken?',
'Cancel' => 'Annuleer',
'Created' => 'Aangemaakt',
'Crop' => 'Bijsnijden',
Expand Down

0 comments on commit 5184beb

Please sign in to comment.