Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Гонимар Сергей committed Jul 1, 2015
0 parents commit 91f7371
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
110 changes: 110 additions & 0 deletions Typeahead.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace yiicms\widgets;


use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\web\View;
use yii\widgets\InputWidget;

class Typeahead extends InputWidget
{
public $clientOptions = [];
public $bloodhoundOptions = [];
public $events = [];

public $options = ['class' => 'form-control'];

/**
* @var string the hashed variable to store the pluginOptions
*/
protected $hashVar;
protected $hashBloodhoundVar;

public function init()
{
parent::init();

}

/**
* @inheritdoc
*/
public function run()
{
if ($this->hasModel()) {
echo Html::activeTextInput($this->model, $this->attribute, $this->options);
} else {
echo Html::textInput($this->name, $this->value, $this->options);
}
$this->registerClientScript();
}

/**
* Initializes bloodhound options
*/
protected function initBloodhoundOptions()
{
$bloodhound = isset($this->bloodhoundOptions) ? $this->bloodhoundOptions : [];
foreach ($bloodhound as $key => $value) {
if (in_array($key, ['datumTokenizer', 'queryTokenizer']) && !$value instanceof JsExpression) {
$bloodhound[$key] = new JsExpression($value);
}
}
if (empty($bloodhound['datumTokenizer'])) {
$bloodhound['datumTokenizer'] = new JsExpression("Bloodhound.tokenizers.obj.whitespace('value')");
}
if (empty($bloodhound['queryTokenizer'])) {
$bloodhound['queryTokenizer'] = new JsExpression("Bloodhound.tokenizers.whitespace");
}
$this->bloodhoundOptions = $bloodhound;
}

/**
* Initializes client options
*/
protected function initClientOptions()
{
$this->initBloodhoundOptions();

$options = $this->clientOptions;
foreach ($options as $key => $value) {
if (in_array($key, ['name', 'source']) && !$value instanceof JsExpression) {
$options[$key] = new JsExpression($value);
}
}
$this->clientOptions = $options;
}

/**
* Registers the needed client script and options.
*/
public function registerClientScript()
{
$js = '';
$view = $this->getView();
$this->initClientOptions();
//$this->hashPluginOptions($view);

$encBloodhoundOptions = empty($this->bloodhoundOptions) ? '{}' : Json::encode($this->bloodhoundOptions);
$this->hashBloodhoundVar = 'typeahead_bloodhound_' . hash('crc32', $encBloodhoundOptions);
$view->registerJs("var {$this->hashBloodhoundVar} = new Bloodhound({$encBloodhoundOptions});\n");
$view->registerJs("{$this->hashBloodhoundVar}.initialize();\n");
$id = $this->options['id'];
if (empty($this->clientOptions['source'])) {
$this->clientOptions['source'] = new JsExpression("{$this->hashBloodhoundVar}.ttAdapter()");
}
$encOptions = empty($this->clientOptions) ? '{}' : Json::encode($this->clientOptions);

$events = '';
foreach ($this->events as $event => $handler) {
$events .= ".bind('{$event}', {$handler})";
}

$js .= '$("#' . $id . '")' . ".typeahead(null, {$encOptions}){$events};\n";
TypeaheadAsset::register($view);
$view->registerJs($js);
}
}
36 changes: 36 additions & 0 deletions TypeaheadAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace yiicms\widgets;


use yii\web\AssetBundle;

class TypeaheadAsset extends AssetBundle
{
/**
* @inheritdoc
*/
public $sourcePath = '@bower/typeahead.js/dist';

/**
* @inheritdoc
*/
public $css = [
//'css/typeaheadjs.css',
];

/**
* @inheritdoc
*/
public $js = [
'bloodhound.min.js',
'typeahead.jquery.min.js',
];

/**
* @inheritdoc
*/
public $depends = [
'common\widgets\typeahead\TypeaheadBootstrapAsset',
];
}
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "yii-cms/yii2-typeahead",
"description": "Typeahead widget for Yii2 framework.",
"keywords": [
"yii2",
"typeahead"
],
"type": "yii2-extension",
"authors": [
{
"name": "Гонимар Сергей",
"email": "[email protected]"
}
],
"require": {
"yiisoft/yii2": ">=2.0",
"bower-asset/typeahead.js": "~0.10.5"
},
"autoload": {
"psr-4": {"yiicms\\widgets\\": ""}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}

0 comments on commit 91f7371

Please sign in to comment.