Commit b6df8deb authored by Ghislain Loas's avatar Ghislain Loas
Browse files

dev #67009 improve autocomplete

parent 934ac16e
......@@ -1464,17 +1464,6 @@ class Class_Album extends Storm_Model_Abstract {
}
public function getLabel() {
return $this->getTitre();
}
public function getFullLabel() {
return $this->_('%s de %s',
$this->getAbsolutePath(),
$this->getMainAuthorName());
}
/**
* Main author
* @return string
......
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_ModelViewer_AjaxSearch {
public static function newWith($model) {
if('Class_Album' == get_class($model))
return new Class_ModelViewer_AjaxSearch_Album($model);
return new Class_ModelViewer_AjaxSearch_Null($model);
}
}
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_ModelViewer_AjaxSearch_Album extends Class_ModelViewer_AjaxSearch_Null {
public function getLabel() {
return $this->_model->getAbsolutePath();
}
}
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2017, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_ModelViewer_AjaxSearch_Null {
use Trait_Translator;
protected $_model;
public function __construct($model) {
$this->_model = $model;
}
public function getLabel() {
return '';
}
}
\ No newline at end of file
......@@ -97,7 +97,7 @@ class ZendAfi_Controller_Plugin_Manager_Manager extends ZendAfi_Controller_Plugi
$response = [];
foreach($models as $model)
$response [] = ['label' => $model->getFullLabel(),
$response [] = ['label' => Class_ModelViewer_AjaxSearch::newWith($model)->getLabel(),
'value' => $model->getId()];
$this->_helper->json($response);
......
......@@ -48,6 +48,7 @@ class ZendAfi_Form_Configuration_Widget_Albums extends ZendAfi_Form_Configuratio
'id_albums',
['label' => $this->_('Album'),
'source' => Class_Url::absolute('/admin/album/ajax-search'),
'model' => 'Class_Album',
'placeholder' => $this->_('Rechercher un album')])
->addElement('imagesViewer',
......
......@@ -25,10 +25,19 @@ class ZendAfi_Form_Decorator_AjaxSearch extends Zend_Form_Decorator_Abstract {
public function render($content) {
$settings = new Class_Entity(array_merge(
['InputName' => $this->_element->getName(),
'InputValue' => $this->_element->getValue(),
'InputValue' => $this->_getValue(),
'Source' => $this->_element->getSource()],
$this->_element->getAttribs()));
return $this->_element->getView()->ajaxSearch($settings);
}
protected function _getValue() {
$class = $this->_element->getAttrib('model');
$id = $this->_element->getValue();
if(!$instance = call_user_func([$class, 'find'], $id))
return '';
return Class_ModelViewer_AjaxSearch::newWith($instance)->getLabel();
}
}
\ No newline at end of file
......@@ -20,7 +20,7 @@
*/
class ZendAfi_Form_Element_AjaxSearch extends Zend_Form_Element {
class ZendAfi_Form_Element_AjaxSearch extends Zend_Form_Element_Hidden {
protected
$_source;
......@@ -32,7 +32,6 @@ class ZendAfi_Form_Element_AjaxSearch extends Zend_Form_Element {
foreach ($decorators as $name => $value)
$this->_decorators[$name] = $value;
$this->removeDecorator('ViewHelper');
}
......
......@@ -22,19 +22,16 @@
class ZendAfi_View_Helper_AjaxSearch extends ZendAfi_View_Helper_BaseHelper {
public function ajaxSearch($settings) {
$name = $settings->getInputName();
$name = 'ajax_search_' . $settings->getInputName();
Class_ScriptLoader::getInstance()
->addJQueryReady(sprintf('$("#%s").autocomplete({ source: "%s" , delay: 300 , minLength: 2 });',
->addAdminScript('ajax_search.js')
->addJqueryReady(sprintf('$("#%s").ajax_search({source: "%s"})',
$name,
$settings->getSource()));
return $this->_tag('input',
null,
['type' => 'text',
'id' => $name,
'name' => $name,
'value' => $settings->getInputValue(),
'placeholder' => $settings->getplaceholder()]);
return $this->view->formText($name,
$settings->getInputValue(),
['placeholder' => $settings->getplaceholder()]);
}
}
\ No newline at end of file
(function ( $ ) {
$.fn.ajax_search = function ( options ) {
var widget = $(this);
widget.autocomplete({ source: options.source ,
delay: 300 ,
minLength: 2,
focus: function(event, ui)
{
event.preventDefault();
},
select: function(event, ui)
{
event.preventDefault();
var element = ui.item;
var input_name = widget.attr('name').substr(('ajax_search_').length);
var input = widget.closest('form').find('#' + input_name).val(element.value);
widget.val(element.label);
}
});
}
} (jQuery));
......@@ -3401,6 +3401,6 @@ class Admin_AlbumControllerAjaxSearchTest extends Admin_AbstractControllerTestCa
/** @test */
public function shouldReturnAlbumsJson() {
$this->assertEquals('[{"label":"Albums non class\u00e9s>Hello moon de ","value":2},{"label":"Albums non class\u00e9s>Hello world de ","value":1}]', $this->_response->getBody());
$this->assertEquals('[{"label":"Albums non class\u00e9s>Hello moon","value":2},{"label":"Albums non class\u00e9s>Hello world","value":1}]', $this->_response->getBody());
}
}
\ No newline at end of file
......@@ -609,7 +609,12 @@ class WidgetsEditAlbumsWidgetTest extends WidgetsSimpleTestCase {
public function setUp() {
parent::setUp();
$this->fixture('Class_Album',
['id' => 5,
'titre' => 'Hello world']);
$simple_widgets = ['modules' => ['4' => ['division' => '1',
'preferences' => ['id_albums' => '5'],
'type_module' => Class_Systeme_ModulesAccueil_BibliothequeNumerique::CODE]]];;
Class_Profil::getCurrentProfil()
......@@ -621,6 +626,18 @@ class WidgetsEditAlbumsWidgetTest extends WidgetsSimpleTestCase {
/** @test */
public function ajaxSearchShouldBePresent() {
$this->assertXPathContentContains('//script', '$("#id_albums").autocomplete');
$this->assertXPathContentContains('//script', '$("#ajax_search_id_albums").ajax_search');
}
/** @test */
public function inputTextValueShouldBeHelloWorld() {
$this->assertXPath('//input[@name="ajax_search_id_albums"][contains(@value, "Albums non cla")][contains(@value, "Hello world")]');
}
/** @test */
public function inputHiddenAlbumsValueShouldBe5() {
$this->assertXPath('//input[@type="hidden"][@name="id_albums"][@value="5"]');
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment