Files
phabricator/src/view/form/control/AphrontFormTokenizerControl.php

109 lines
3.0 KiB
PHP
Raw Normal View History

2011-01-25 13:48:05 -08:00
<?php
final class AphrontFormTokenizerControl extends AphrontFormControl {
2011-01-25 13:48:05 -08:00
private $datasource;
private $disableBehavior;
private $limit;
private $placeholder;
2011-01-25 13:48:05 -08:00
public function setDatasource($datasource) {
$this->datasource = $datasource;
return $this;
}
2011-02-04 22:45:42 -08:00
public function setDisableBehavior($disable) {
$this->disableBehavior = $disable;
return $this;
}
2011-01-25 13:48:05 -08:00
protected function getCustomControlClass() {
return 'aphront-form-control-tokenizer';
}
public function setLimit($limit) {
$this->limit = $limit;
return $this;
}
public function setPlaceholder($placeholder) {
$this->placeholder = $placeholder;
return $this;
}
2011-01-25 13:48:05 -08:00
protected function renderInput() {
$name = $this->getName();
2011-03-22 20:41:02 -07:00
$values = nonempty($this->getValue(), array());
2011-01-25 13:48:05 -08:00
assert_instances_of($values, 'PhabricatorObjectHandle');
2011-02-04 22:45:42 -08:00
if ($this->getID()) {
$id = $this->getID();
} else {
$id = celerity_generate_unique_node_id();
}
2011-01-25 13:48:05 -08:00
if (!$this->placeholder) {
$this->placeholder = $this->getDefaultPlaceholder();
}
2011-03-22 20:41:02 -07:00
$template = new AphrontTokenizerTemplateView();
$template->setName($name);
$template->setID($id);
$template->setValue($values);
$username = null;
if ($this->user) {
$username = $this->user->getUsername();
}
2011-02-04 22:45:42 -08:00
if (!$this->disableBehavior) {
Javelin::initBehavior('aphront-basic-tokenizer', array(
'id' => $id,
'src' => $this->datasource,
'value' => mpull($values, 'getFullName', 'getPHID'),
'icons' => mpull($values, 'getTypeIcon', 'getPHID'),
'limit' => $this->limit,
'username' => $username,
'placeholder' => $this->placeholder,
2011-02-04 22:45:42 -08:00
));
}
2011-01-25 13:48:05 -08:00
2011-03-22 20:41:02 -07:00
return $template->render();
2011-01-25 13:48:05 -08:00
}
private function getDefaultPlaceholder() {
$datasource = $this->datasource;
$matches = null;
if (!preg_match('@^/typeahead/common/(.*)/$@', $datasource, $matches)) {
return null;
}
$request = $matches[1];
$map = array(
'users' => pht('Type a user name...'),
'authors' => pht('Type a user name...'),
'usersorprojects' => pht('Type a user or project name...'),
'searchowner' => pht('Type a user name...'),
'accounts' => pht('Type a user name...'),
'mailable' => pht('Type a user, project, or mailing list...'),
'allmailable' => pht('Type a user, project, or mailing list...'),
'searchproject' => pht('Type a project name...'),
'projects' => pht('Type a project name...'),
'repositories' => pht('Type a repository name...'),
'packages' => pht('Type a package name...'),
'macros' => pht('Type a macro name...'),
'arcanistproject' => pht('Type an arc project name...'),
'accountsorprojects' => pht('Type a user or project name...'),
'usersprojectsorpackages' =>
pht('Type a user, project, or package name...'),
);
return idx($map, $request);
}
2011-01-25 13:48:05 -08:00
}