Browse Source

Fixes #1104. Model generator table names auto complete.

tags/2.0.0-beta
resurtm 11 years ago
parent
commit
4cb1a26477
  1. 11
      framework/yii/gii/Generator.php
  2. 2
      framework/yii/gii/GiiAsset.php
  3. 8
      framework/yii/gii/assets/main.css
  4. 1139
      framework/yii/gii/assets/typeahead.js
  5. 51
      framework/yii/gii/assets/typeahead.js-bootstrap.css
  6. 24
      framework/yii/gii/components/ActiveField.php
  7. 12
      framework/yii/gii/generators/model/Generator.php

11
framework/yii/gii/Generator.php

@ -111,6 +111,17 @@ abstract class Generator extends Model
} }
/** /**
* Returns the list of auto complete values.
* The array keys are the attribute names, and the array values are the corresponding auto complete values.
* Auto complete values can also be callable typed in order one want to make postponed data generation.
* @return array the list of auto complete values
*/
public function autoCompleteData()
{
return [];
}
/**
* Returns the message to be displayed when the newly generated code is saved successfully. * Returns the message to be displayed when the newly generated code is saved successfully.
* Child classes may override this method to customize the message. * Child classes may override this method to customize the message.
* @return string the message to be displayed when the newly generated code is saved successfully. * @return string the message to be displayed when the newly generated code is saved successfully.

2
framework/yii/gii/GiiAsset.php

@ -26,12 +26,14 @@ class GiiAsset extends AssetBundle
*/ */
public $css = [ public $css = [
'main.css', 'main.css',
'typeahead.js-bootstrap.css',
]; ];
/** /**
* @inheritdoc * @inheritdoc
*/ */
public $js = [ public $js = [
'gii.js', 'gii.js',
'typeahead.js',
]; ];
/** /**
* @inheritdoc * @inheritdoc

8
framework/yii/gii/assets/main.css

@ -201,3 +201,11 @@ body {
.DifferencesInline .ChangeReplace del { .DifferencesInline .ChangeReplace del {
background: #e99; background: #e99;
} }
/* additional styles for typeahead.js-bootstrap.css */
.twitter-typeahead {
display: block !important;
}
.twitter-typeahead .tt-hint {
padding: 6px 12px !important;
}

1139
framework/yii/gii/assets/typeahead.js

File diff suppressed because it is too large Load Diff

51
framework/yii/gii/assets/typeahead.js-bootstrap.css vendored

@ -0,0 +1,51 @@
/* always keep this link here when updating this file: https://github.com/jharding/typeahead.js-bootstrap.css */
.twitter-typeahead .tt-query,
.twitter-typeahead .tt-hint {
margin-bottom: 0;
}
.tt-dropdown-menu {
min-width: 160px;
margin-top: 2px;
padding: 5px 0;
background-color: #fff;
border: 1px solid #ccc;
border: 1px solid rgba(0,0,0,.2);
*border-right-width: 2px;
*border-bottom-width: 2px;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2);
-moz-box-shadow: 0 5px 10px rgba(0,0,0,.2);
box-shadow: 0 5px 10px rgba(0,0,0,.2);
-webkit-background-clip: padding-box;
-moz-background-clip: padding;
background-clip: padding-box;
}
.tt-suggestion {
display: block;
padding: 3px 20px;
}
.tt-suggestion.tt-is-under-cursor {
color: #fff;
background-color: #0081c2;
background-image: -moz-linear-gradient(top, #0088cc, #0077b3);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#0088cc), to(#0077b3));
background-image: -webkit-linear-gradient(top, #0088cc, #0077b3);
background-image: -o-linear-gradient(top, #0088cc, #0077b3);
background-image: linear-gradient(to bottom, #0088cc, #0077b3);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff0088cc', endColorstr='#ff0077b3', GradientType=0)
}
.tt-suggestion.tt-is-under-cursor a {
color: #fff;
}
.tt-suggestion p {
margin: 0;
}

24
framework/yii/gii/components/ActiveField.php

@ -8,6 +8,7 @@
namespace yii\gii\components; namespace yii\gii\components;
use yii\gii\Generator; use yii\gii\Generator;
use yii\helpers\Json;
/** /**
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
@ -30,10 +31,18 @@ class ActiveField extends \yii\widgets\ActiveField
if (isset($hints[$this->attribute])) { if (isset($hints[$this->attribute])) {
$this->hint($hints[$this->attribute]); $this->hint($hints[$this->attribute]);
} }
$autoCompleteData = $this->model->autoCompleteData();
if (isset($autoCompleteData[$this->attribute])) {
if (is_callable($autoCompleteData[$this->attribute])) {
$this->autoComplete(call_user_func($autoCompleteData[$this->attribute]));
} else {
$this->autoComplete($autoCompleteData[$this->attribute]);
}
}
} }
/** /**
* Makes filed remember its value between page reloads * Makes field remember its value between page reloads
* @return static the field object itself * @return static the field object itself
*/ */
public function sticky() public function sticky()
@ -41,4 +50,17 @@ class ActiveField extends \yii\widgets\ActiveField
$this->options['class'] .= ' sticky'; $this->options['class'] .= ' sticky';
return $this; return $this;
} }
/**
* Makes field auto completable
* @param array $data auto complete data (array of callables or scalars)
* @return static the field object itself
*/
public function autoComplete($data)
{
static $counter = 0;
$this->inputOptions['class'] .= ' typeahead-' . (++$counter);
$this->form->getView()->registerJs("jQuery('.typeahead-{$counter}').typeahead({local: " . Json::encode($data) . "});");
return $this;
}
} }

12
framework/yii/gii/generators/model/Generator.php

@ -113,6 +113,18 @@ class Generator extends \yii\gii\Generator
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function autoCompleteData()
{
return [
'tableName' => function () {
return $this->getDbConnection()->getSchema()->getTableNames();
},
];
}
/**
* @inheritdoc
*/
public function requiredTemplates() public function requiredTemplates()
{ {
return ['model.php']; return ['model.php'];

Loading…
Cancel
Save