diff --git a/code/TagField.php b/code/TagField.php index 2647067..1184cd6 100644 --- a/code/TagField.php +++ b/code/TagField.php @@ -73,8 +73,9 @@ class TagField extends TextField { */ protected $customTags; - function __construct($name, $title = null, $value = null, $tagTopicClass = null) { + function __construct($name, $title = null, $value = null, $tagTopicClass = null, $tagObjectField = "Title") { $this->tagTopicClass = $tagTopicClass; + $this->tagObjectField = $tagObjectField; parent::__construct($name, $title, $value); } @@ -85,22 +86,25 @@ class TagField extends TextField { Requirements::javascript("tagfield/javascript/jquery.tags.js"); Requirements::css("tagfield/css/TagField.css"); - if($this->customTags) { - Requirements::customScript("jQuery(document).ready(function() { - jQuery('#" . $this->id() . "').tagSuggest({ - tags: " . Convert::raw2json($this->customTags) . " - }); - });"); - } else { - Requirements::customScript("jQuery(document).ready(function() { - jQuery('#" . $this->id() . "').tagSuggest({ - url: '" . parse_url($this->Link(),PHP_URL_PATH) . "/suggest', - separator: '" . $this->separator . "' - }); - });"); - } + // Standard textfield stuff + $attributes = array( + 'type' => 'text', + 'class' => 'text tagField', + 'id' => $this->id(), + 'name' => $this->Name(), + 'value' => $this->Value(), + 'tabindex' => $this->getTabIndex(), + 'maxlength' => ($this->maxLength) ? $this->maxLength : null, + 'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null, + ); + if($this->disabled) $attributes['disabled'] = 'disabled'; - return parent::Field(); + // Data passed as custom attributes + if($this->customTags) $attributes['tags'] = $this->customTags; + else $attributes['href'] = parse_url($this->Link(),PHP_URL_PATH); + $attributes['rel'] = $this->separator; + + return $this->createTag('input', $attributes); } /** @@ -242,7 +246,7 @@ class TagField extends TextField { ); if($this->tagFilter) $SQL_filter .= ' AND ' . $this->tagFilter; - $tagObjs = DataObject::get($tagClass, $SQL_filter, $this->tagSort); + $tagObjs = DataObject::get($tagClass, $SQL_filter, $this->tagSort, "", 50); $tagArr = ($tagObjs) ? array_values($tagObjs->map('ID', $this->tagObjectField)) : array(); return $tagArr; diff --git a/javascript/jquery.tags.js b/javascript/jquery.tags.js index c22fc85..c8bd47a 100644 --- a/javascript/jquery.tags.js +++ b/javascript/jquery.tags.js @@ -131,7 +131,7 @@ for (i = 0; i < matches.length; i++) { html += '<' + settings.tagWrap + ' class="_tag_suggestion">' + matches[i] + ''; } - + tagMatches.html(html); suggestionsShow = !!(matches.length); } else { @@ -264,4 +264,26 @@ setSelection(); }); }; + + // SilverStripe loader + SSTagFieldLoader = function() { + $('input.tagField').each(function() { + var tags; + if(tags = $(this).attr('taglist')) { + $(this).tagSuggest({ + tags: tags, + separator: $(this).attr('rel') + }); + } else { + $(this).tagSuggest({ + url: $(this).attr("href") + '/suggest', + separator: $(this).attr('rel') + }); + } + }); + } + + if(typeof $(document).livequery != 'undefined') $(document).livequery(SSTagFieldLoader); + else $(document).ready(SSTagFieldLoader); + })(jQuery); \ No newline at end of file