mirror of
https://github.com/silverstripe/silverstripe-tagfield
synced 2024-06-29 07:59:26 +02:00
BUGFIX Passing all tests now
MINOR Added css stubs
This commit is contained in:
parent
dddedf48a1
commit
e75e5a5a45
|
@ -61,7 +61,8 @@ class TagField extends TextField {
|
|||
public function Field() {
|
||||
Requirements::javascript(THIRDPARTY_DIR . "/jquery/jquery.js");
|
||||
Requirements::javascript("tagfield/javascript/jquery.tags.js");
|
||||
Requirements::customScript("$.ready(function() {
|
||||
Requirements::css("tagfield/css/TagField.css");
|
||||
Requirements::customScript("jQuery(document).ready(function() {
|
||||
$('#" . $this->id() . "').tagSuggest({
|
||||
url: '" . $this->Link() . "/suggest'
|
||||
});
|
||||
|
@ -71,13 +72,15 @@ class TagField extends TextField {
|
|||
}
|
||||
|
||||
/**
|
||||
* Helper for autocompletion in javascript library.
|
||||
*
|
||||
* @return string JSON array
|
||||
*/
|
||||
public function suggest($request) {
|
||||
$tagTopicClassObj = singleton($this->tagTopicClass);
|
||||
|
||||
$searchString = $request->requestVar($this->Name());
|
||||
|
||||
|
||||
if($this->customTags) {
|
||||
$tags = $this->customTags;
|
||||
} else if($tagTopicClassObj->many_many($this->Name())) {
|
||||
|
@ -105,6 +108,9 @@ class TagField extends TextField {
|
|||
}
|
||||
|
||||
protected function saveIntoObjectTags($record) {
|
||||
// HACK We can't save relationship tables without having an ID
|
||||
if(!$record->isInDB()) $record->write();
|
||||
|
||||
$tagsArr = $this->splitTagsToArray($this->value);
|
||||
$relationName = $this->Name();
|
||||
$existingTagsComponentSet = $record->$relationName();
|
||||
|
@ -187,17 +193,24 @@ class TagField extends TextField {
|
|||
Convert::raw2sql($searchString)
|
||||
);
|
||||
if($this->tagFilter) $SQL_filter .= ' AND ' . $this->tagFilter;
|
||||
|
||||
|
||||
$allTopicObjs = DataObject::get($this->tagTopicClass, $SQL_filter, $this->tagSort);
|
||||
$multipleTagsArr = ($allTopicObjs) ? array_values($allTopicObjs->map('ID', $this->Name)) : array();
|
||||
$tagArr = array();
|
||||
$multipleTagsArr = ($allTopicObjs) ? array_values($allTopicObjs->map('ID', $this->Name())) : array();
|
||||
$filteredTagArr = array();
|
||||
foreach($multipleTagsArr as $multipleTags) {
|
||||
$tagArr += $this->splitTagsToArray($multipleTags);
|
||||
$singleTagsArr = $this->splitTagsToArray($multipleTags);
|
||||
foreach($singleTagsArr as $singleTag) {
|
||||
// only add those tags of the whole string which
|
||||
// match the search terms
|
||||
if(stripos($singleTag, $searchString) !== false) {
|
||||
$filteredTagArr[] = $singleTag;
|
||||
}
|
||||
}
|
||||
}
|
||||
// remove duplicates (retains case sensitive duplicates)
|
||||
$tagArr = array_unique($tagArr);
|
||||
$filteredTagArr = array_unique($filteredTagArr);
|
||||
|
||||
return $tagArr;
|
||||
return $filteredTagArr;
|
||||
}
|
||||
|
||||
public function setTagFilter($sql) {
|
||||
|
|
11
css/TagField.css
Normal file
11
css/TagField.css
Normal file
|
@ -0,0 +1,11 @@
|
|||
.field.tag span.tagMatches {
|
||||
margin-left: 10px;
|
||||
}
|
||||
|
||||
.field.tag span.tagMatches span {
|
||||
padding: 2px;
|
||||
margin-right: 4px;
|
||||
background-color: #0000AB;
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
}
|
|
@ -49,41 +49,92 @@ class TagFieldTest extends FunctionalTest {
|
|||
'tag1 tag3'
|
||||
);
|
||||
}
|
||||
|
||||
function testObjectSuggest() {
|
||||
/*
|
||||
function testSuggestRequest() {
|
||||
// partial
|
||||
$response = $this->post('TagFieldTestController/ObjectTestForm/fields/Tags/suggest', array('tag','tag'));
|
||||
$this->assertEquals($response->getBody(), '["tag1","tag2"]');
|
||||
}
|
||||
*/
|
||||
function testObjectSuggest() {
|
||||
$field = new TagField('Tags', null, null, 'TagFieldTest_BlogEntry');
|
||||
|
||||
// partial
|
||||
$request = new HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/ObjectTestForm/fields/Tags/suggest',
|
||||
null,
|
||||
array('Tags' => 'tag')
|
||||
);
|
||||
$this->assertEquals($field->suggest($request), '["tag1","tag2"]');
|
||||
|
||||
// full
|
||||
$response = $this->post('TagFieldTestController/ObjectTestForm/fields/Tags/suggest', array('tag','tag1'));
|
||||
$this->assertEquals($response->getBody(), '["tag1"]');
|
||||
$request = new HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/ObjectTestForm/fields/Tags/suggest',
|
||||
null,
|
||||
array('Tags' => 'tag1')
|
||||
);
|
||||
|
||||
$this->assertEquals($field->suggest($request), '["tag1"]');
|
||||
|
||||
// case insensitive
|
||||
$response = $this->post('TagFieldTestController/ObjectTestForm/fields/Tags/suggest', array('tag','TAG1'));
|
||||
$this->assertEquals($response->getBody(), '["tag1"]');
|
||||
$request = new HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/ObjectTestForm/fields/Tags/suggest',
|
||||
null,
|
||||
array('Tags' => 'TAG1')
|
||||
);
|
||||
$this->assertEquals($field->suggest($request), '["tag1"]');
|
||||
|
||||
// no match
|
||||
$response = $this->post('TagFieldTestController/ObjectTestForm/fields/Tags/suggest', array('tag','unknown'));
|
||||
$this->assertEquals($response->getBody(), '[]');
|
||||
$request = new HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/ObjectTestForm/fields/Tags/suggest',
|
||||
null,
|
||||
array('Tags' => 'unknown')
|
||||
);
|
||||
$this->assertEquals($field->suggest($request), '[]');
|
||||
}
|
||||
|
||||
function testTextbasedSuggest() {
|
||||
$field = new TagField('TextbasedTags', null, null, 'TagFieldTest_BlogEntry');
|
||||
|
||||
// partial
|
||||
$response = $this->post('TagFieldTestController/TextbasedTestForm/fields/Tags/suggest', array('tag','tag'));
|
||||
$this->assertEquals($response->getBody(), '["tag1","tag2"]');
|
||||
$request = new HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/TextbasedTestForm/fields/Tags/suggest',
|
||||
null,
|
||||
array('TextbasedTags' => 'tag')
|
||||
);
|
||||
$this->assertEquals($field->suggest($request), '["textbasedtag1","textbasedtag2"]');
|
||||
|
||||
// full
|
||||
$response = $this->post('TagFieldTestController/TextbasedTestForm/fields/Tags/suggest', array('tag','tag1'));
|
||||
$this->assertEquals($response->getBody(), '["tag1"]');
|
||||
$request = new HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/TextbasedTestForm/fields/Tags/suggest',
|
||||
null,
|
||||
array('TextbasedTags' => 'tag1')
|
||||
);
|
||||
$this->assertEquals($field->suggest($request), '["textbasedtag1"]');
|
||||
|
||||
// case insensitive
|
||||
$response = $this->post('TagFieldTestController/TextbasedTestForm/fields/Tags/suggest', array('tag','TAG1'));
|
||||
$this->assertEquals($response->getBody(), '["tag1"]');
|
||||
$request = new HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/TextbasedTestForm/fields/Tags/suggest',
|
||||
null,
|
||||
array('TextbasedTags' => 'TAG1')
|
||||
);
|
||||
$this->assertEquals($field->suggest($request), '["textbasedtag1"]');
|
||||
|
||||
// no match
|
||||
$response = $this->post('TagFieldTestController/TextbasedTestForm/fields/Tags/suggest', array('tag','unknown'));
|
||||
$this->assertEquals($response->getBody(), '[]');
|
||||
$request = new HTTPRequest(
|
||||
'get',
|
||||
'TagFieldTestController/TextbasedTestForm/fields/Tags/suggest',
|
||||
null,
|
||||
array('TextbasedTags' => 'unknown')
|
||||
);
|
||||
$this->assertEquals($field->suggest($request), '[]');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,10 +6,10 @@ TagFieldTest_Tag:
|
|||
TagFieldTest_BlogEntry:
|
||||
blogentry1:
|
||||
Title: blogentry1
|
||||
Tags: => TagFieldTest_Tag.tag1,TagFieldTest_Tag.tag2
|
||||
Tags: =>TagFieldTest_Tag.tag1,=>TagFieldTest_Tag.tag2
|
||||
blogentry2:
|
||||
Title: blogentry2
|
||||
Tags: => TagFieldTest_Tag.tag1
|
||||
Tags: =>TagFieldTest_Tag.tag1
|
||||
blogentry3:
|
||||
Title: blogentry3
|
||||
blogentry4:
|
||||
|
|
Loading…
Reference in New Issue
Block a user