3 final class AphrontFormTokenizerControl
extends AphrontFormControl
{
6 private $disableBehavior;
10 private $initialValue;
12 public function setDatasource(PhabricatorTypeaheadDatasource
$datasource) {
13 $this->datasource
= $datasource;
17 public function setDisableBehavior($disable) {
18 $this->disableBehavior
= $disable;
22 protected function getCustomControlClass() {
23 return 'aphront-form-control-tokenizer';
26 public function setLimit($limit) {
27 $this->limit
= $limit;
31 public function setPlaceholder($placeholder) {
32 $this->placeholder
= $placeholder;
36 public function setInitialValue(array $initial_value) {
37 $this->initialValue
= $initial_value;
41 public function getInitialValue() {
42 return $this->initialValue
;
45 public function willRender() {
46 // Load the handles now so we'll get a bulk load later on when we actually
51 protected function renderInput() {
52 $name = $this->getName();
54 $handles = $this->loadHandles();
55 $handles = iterator_to_array($handles);
60 $id = celerity_generate_unique_node_id();
63 $datasource = $this->datasource
;
66 pht('You must set a datasource to use a TokenizerControl.'));
68 $datasource->setViewer($this->getUser());
71 if (!strlen($this->placeholder
)) {
72 $placeholder = $datasource->getPlaceholderText();
75 $values = nonempty($this->getValue(), array());
76 $tokens = $datasource->renderTokens($values);
78 foreach ($tokens as $token) {
79 $token->setInputName($this->getName());
82 $template = id(new AphrontTokenizerTemplateView())
87 $initial_value = $this->getInitialValue();
88 if ($initial_value !== null) {
89 $template->setInitialValue($initial_value);
93 if ($this->hasViewer()) {
94 $username = $this->getViewer()->getUsername();
97 $datasource_uri = $datasource->getDatasourceURI();
98 $browse_uri = $datasource->getBrowseURI();
100 $template->setBrowseURI($browse_uri);
103 if (!$this->disableBehavior
) {
104 Javelin
::initBehavior('aphront-basic-tokenizer', array(
106 'src' => $datasource_uri,
107 'value' => mpull($tokens, 'getValue', 'getKey'),
108 'icons' => mpull($tokens, 'getIcon', 'getKey'),
109 'types' => mpull($tokens, 'getTokenType', 'getKey'),
110 'colors' => mpull($tokens, 'getColor', 'getKey'),
111 'availabilityColors' => mpull(
113 'getAvailabilityColor',
115 'limit' => $this->limit
,
116 'username' => $username,
117 'placeholder' => $placeholder,
118 'browseURI' => $browse_uri,
119 'disabled' => $this->getDisabled(),
123 return $template->render();
126 private function loadHandles() {
127 if ($this->handles
=== null) {
128 $viewer = $this->getUser();
132 'Call %s before rendering tokenizers. '.
133 'Use %s on %s to do this easily.',
139 $values = nonempty($this->getValue(), array());
142 foreach ($values as $value) {
143 if (!PhabricatorTypeaheadDatasource
::isFunctionToken($value)) {
148 $this->handles
= $viewer->loadHandles($phids);
151 return $this->handles
;