Skip to content

Commit

Permalink
Added AliasInput::widget()
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-wdmg committed Apr 16, 2020
1 parent 015503c commit 7c0e404
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
=========

## 1.0.3 (2020-04-16)
* Added AliasInput::widget()

## 1.0.2 (2020-04-11)
* Added LangSwitcher::widget()

Expand Down
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Progress](https://img.shields.io/badge/required-Yii2_v2.0.33-blue.svg)](https://packagist.org/packages/yiisoft/yii2)
[![Github all releases](https://img.shields.io/github/downloads/wdmg/yii2-widgets/total.svg)](https://GitHub.com/wdmg/yii2-widgets/releases/)
[![GitHub version](https://badge.fury.io/gh/wdmg/yii2-widgets.svg)](https://github.com/wdmg/yii2-widgets)
[![Yii2](https://img.shields.io/badge/required-Yii2_v2.0.33-blue.svg)](https://packagist.org/packages/yiisoft/yii2)
[![Downloads](https://img.shields.io/packagist/dt/wdmg/yii2-widgets.svg)](https://packagist.org/packages/wdmg/yii2-widgets)
[![Packagist Version](https://img.shields.io/packagist/v/wdmg/yii2-widgets.svg)](https://packagist.org/packages/wdmg/yii2-widgets)
![Progress](https://img.shields.io/badge/progress-in_development-red.svg)
[![GitHub license](https://img.shields.io/github/license/wdmg/yii2-widgets.svg)](https://github.com/wdmg/yii2-widgets/blob/master/LICENSE)

Expand Down Expand Up @@ -90,8 +90,26 @@ Example of usecase LangSwitcher::widget() in view instance of dashboard:
]);
?>


Example of usecase AliasInput::widget() in view instance of dashboard:

<?php

use wdmg\widgets\AliasInput;

<?= $form->field($model, 'alias')->widget(AliasInput::class, [
'labels' => [
'edit' => 'Edit',
'save' => 'Save'
],
'options' => [
'baseUrl' => $model->url
]
])->label('Post URL'); ?>


# Status and version [in progress development]
* v.1.0.3 - Added AliasInput::widget()
* v.1.0.2 - Added LangSwitcher::widget()
* v.1.0.1 - Up to date dependencies
* v.1.0.0 - Added NavContents::widget() and MenuContents::widget()
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["yii", "yii2", "extension", "widgets", "bootstrap", "widget", "wdmg"],
"type": "yii2-extension",
"license": "MIT",
"version": "1.0.2",
"version": "1.0.3",
"homepage": "https://github.com/wdmg/yii2-widgets",
"support": {
"source": "https://github.com/wdmg/yii2-widgets",
Expand Down
167 changes: 167 additions & 0 deletions src/AliasInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

namespace wdmg\widgets;

/**
* Yii2 Alias Input
*
* @category Widgets
* @version 1.0.3
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-widgets
* @copyright Copyright (c) 2019 - 2020 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/

use Yii;
use yii\helpers\Html;
use yii\widgets\InputWidget;

class AliasInput extends InputWidget
{

public $labels;

/**
* @inheritdoc
*/
public function run()
{
$value = null;
$options = $this->options;

if ($this->hasModel()) {
$input = Html::activeInput('text', $this->model, $this->attribute, $options);
$value = isset($this->options['value']) ?
$this->options['value'] :
Html::getAttributeValue($this->model, $this->attribute);
} else {
$input = Html::input('text', $this->name, $this->value, $options);
$value = $this->value;
}

$url = null;
if (isset($this->options['baseUrl'])) {
$url = $this->options['baseUrl'];
$url = rtrim($url, '/');
}

$base = null;
if (!is_null($url)) {
$base = str_replace($value, '', $url);
$base = rtrim($base, '/');
}

echo '<div id="collapse-' . $this->getId() . '">';
echo '<div class="collapse fade in" id="collapseView-' . $this->getId() . '">';
echo '<div class="form-inline">';
echo '<div class="input-group">';
echo Html::a($url . "/", $url . "/", [
'class' => 'form-control-static',
'target' => '_blank',
'data' => [
'base' => $base,
'pjax' => 0
]
]);
echo '</div>';
echo '<div class="input-group">';
echo '&nbsp';
echo Html::button((isset($this->labels['edit']) ? $this->labels['edit'] : 'Edit'), [
'class' => 'btn btn-edit btn-default',
'data' => [
'toggle' => "collapse",
'parent' => "#collapse-" . $this->getId(),
'target' => "#collapseEdit-" . $this->getId()
],
'aria' => [
'expanded' => true,
'controls' => "collapseView-" . $this->getId()
],
]);
echo '</div>';
echo '</div>';
echo '</div>';

echo '<div class="collapse fade" id="collapseEdit-' . $this->getId() . '">';
echo '<div class="form-inline">';
echo '<div class="input-group">';
echo Html::tag('span', $base . "/", [
'class' => 'form-control-static',
]);
echo '</div>';
echo '<div class="input-group">';
echo $input;
echo '<div class="input-group-btn">';
echo Html::button((isset($this->labels['save']) ? $this->labels['save'] : 'Save'), [
'class' => 'btn btn-save btn-default',
'data' => [
'toggle' => "collapse",
'parent' => "#collapse-" . $this->getId(),
'target' => "#collapseView-" . $this->getId()
],
'aria' => [
'expanded' => false,
'controls' => "collapseEdit-" . $this->getId()
],
]);
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';

// Register assets
$this->registerAssets();
}

/**
* Register required assets for the widgets
*/
public function registerAssets()
{
$view = $this->getView();
$view->registerJs(<<< JS
$(document).ready(function() {
function aliasEditor() {
var collapseView = $('#collapseView-$this->id').collapse({
toggle: false
});
var collapseEdit = $('#collapseEdit-$this->id').collapse({
toggle: false
});
collapseView.on('show.bs.collapse', function () {
collapseEdit.collapse('hide');
var alias = collapseEdit.find('input').val();
var url = collapseView.find('a').data('base') + "/" + alias;
collapseView.find('a').attr('href', url).text(url);
}).on('hidden.bs.collapse', function () {
collapseEdit.collapse('show');
collapseEdit.find('input').focus();
});
collapseEdit.on('show.bs.collapse', function () {
collapseView.collapse('hide');
}).on('hidden.bs.collapse', function () {
collapseView.collapse('show');
});
}
aliasEditor();
$(document).on('pjax:success', function() {
aliasEditor();
});
});
JS
);
}
}
2 changes: 1 addition & 1 deletion src/LangSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Yii2 Language switcher
*
* @category Widgets
* @version 1.0.2
* @version 1.0.3
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-widgets
* @copyright Copyright (c) 2019 - 2020 W.D.M.Group, Ukraine
Expand Down
2 changes: 1 addition & 1 deletion src/MenuContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Yii2 Table of contents based on Menu::widget()
*
* @category Widgets
* @version 1.0.2
* @version 1.0.3
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-widgets
* @copyright Copyright (c) 2019 - 2020 W.D.M.Group, Ukraine
Expand Down
2 changes: 1 addition & 1 deletion src/NavContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Yii2 Table of contents based on bootstrap Nav::widget()
*
* @category Widgets
* @version 1.0.2
* @version 1.0.3
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-widgets
* @copyright Copyright (c) 2019 - 2020 W.D.M.Group, Ukraine
Expand Down

0 comments on commit 7c0e404

Please sign in to comment.