Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finally implementing Grid #93

Merged
merged 24 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
982cbb2
fixes #86
DarkSide666 Mar 25, 2017
b7aa1ab
comment unused variables
DarkSide666 Mar 25, 2017
0a11fe0
fix codeclimate
DarkSide666 Mar 25, 2017
c6a5258
Initial implementation of Label class
romaninsh Mar 27, 2017
1955f05
Apply fixes from StyleCI
romaninsh Mar 27, 2017
02fcb4b
Merge branch 'develop' into feature/implement-label
romaninsh Mar 27, 2017
15899b4
prevent multi-render, resolve #85
romaninsh Mar 27, 2017
0580f8f
prevent multi-render, resolve #85
romaninsh Mar 27, 2017
2008f38
rename .jade to .pug
romaninsh Mar 27, 2017
812310c
Merge branch 'feature/fix-86' into feature/grid-part3
romaninsh Mar 27, 2017
38afc03
Merge branch 'feature/implement-label' into feature/grid-part3
romaninsh Mar 27, 2017
be9f270
Merge branch 'feature/rename-jade-to-pug' into feature/grid-part3
romaninsh Mar 27, 2017
93ac5cf
Implement Grid - added bar, actions, checkboxes
romaninsh Mar 30, 2017
02129bc
Apply fixes from StyleCI
romaninsh Mar 30, 2017
2f87594
Merge branch 'develop' into feature/grid-part3
romaninsh Mar 30, 2017
0f89853
various improvements
romaninsh Mar 30, 2017
239a284
finally a good logic
romaninsh Mar 30, 2017
bcb9c2e
Fixed bugs with variable names
romaninsh Mar 30, 2017
2995007
Merge branch 'develop' into feature/grid-part3
romaninsh Mar 31, 2017
bbd3892
better use $g because $m is "reserved" for model :)
DarkSide666 Mar 31, 2017
bb9033b
fixed this once
DarkSide666 Mar 31, 2017
7fd74d0
add comments
DarkSide666 Mar 31, 2017
332c0e8
correct comment
DarkSide666 Mar 31, 2017
f432f2c
add comment
DarkSide666 Mar 31, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions demos/grid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

require 'init.php';
require 'database.php';

$m = $layout->add(['Grid']);
$m->setModel(new Country($db));
$m->addQuickSearch();

$m->menu->addItem(['Add Country', 'icon'=>'add square'], new \atk4\ui\jsExpression('alert(123)'));
$m->menu->addItem(['Re-Import', 'icon'=>'power']);
$m->menu->addItem(['Delete All', 'icon'=>'trash', 'red active']);

$m->addAction('Say HI', new \atk4\ui\jsExpression('alert("hi")'));
$m->addAction(['icon'=>'pencil'], new \atk4\ui\jsExpression('alert($(this).closest("tr").data("id"))'));

$sel = $m->addSelection();
$m->menu->addItem('show selection')->on('click', new \atk4\ui\jsExpression(
'alert("Selected: "+[].join(", "))', [$sel->jsChecked()]
));
2 changes: 1 addition & 1 deletion demos/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$form = $layout->leftMenu->addGroup(['Grid and Table', 'icon'=>'table']);
$form->addItem('Data table with formatted columns', ['table']);
$form->addItem('Table interractions', ['multitable']);
$form->addItem('Grid - Table+Bar+Search+Paginator', ['table']);
$form->addItem('Grid - Table+Bar+Search+Paginator', ['grid']);
$form->addItem('Interactivity - Modals and Expanders', ['expander']);

$basic = $layout->leftMenu->addGroup(['Basics', 'icon'=>'cubes']);
Expand Down
6 changes: 4 additions & 2 deletions docs/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ with the button, it will add 'labeled' class.
Label
=====

.. php:class:: Icon

Implementing http://semantic-ui.com/elements/label.html for Agile UI, Label is a very basic view,
that can be used on it's own or as part of another UI view (such as menu item).

Expand All @@ -128,11 +130,11 @@ Label has the following propetries:
All the above can be string, array (passed to Icon, Image or View class) or an object.


.. php:class:: HelloWorld

HelloWorld
==========

.. php:class:: HelloWorld

A very basic class that says hello world. This is a manefistation of our component concept. If using
other PHP frameworks may require you to create multiple files and spend considerable time creating even
a "Hello, World!" app, then we do it in a single line::
Expand Down
27 changes: 25 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,37 @@ public function getTag($tag = null, $attr = null, $value = null)
if ($tag === null) {
$tag = 'div';
} elseif (is_array($tag)) {
$value = $attr;
$attr = $tag;
$tmp = $tag;

$tag = 'div';
$value = '';

if (isset($tmp[0])) {
$tag = $tmp[0];
unset($tmp[0]);
}

if (isset($tmp[1])) {
$value = $tmp[1];
unset($tmp[1]);
}

$attr = $tmp;
}
if ($tag[0] === '<') {
return $tag;
}
if (is_string($attr)) {
$value = $attr;
$attr = null;
}

if (is_string($value)) {
$value = $this->encodeHTML($value);
} elseif (is_array($value)) {
$value = $this->getTag($value);
}

if (!$attr) {
return "<$tag>".($value ? ($value)."</$tag>" : '');
}
Expand Down
20 changes: 0 additions & 20 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,4 @@ public function renderView()

parent::renderView();
}

/**
* Makes button into a "<a>" element with a link.
*
* @param string $url
*
* @return $this
*/
public function link($url)
{
$this->element = 'a';
if (is_string($url)) {
$this->setAttr('href', $url);

return $this;
}
$this->setAttr('href', $this->app->url($url));

return $this;
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to View

82 changes: 82 additions & 0 deletions src/Grid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

// vim:ts=4:sw=4:et:fdm=marker:fdl=0

namespace atk4\ui;

/**
* Implements a more sophisticated and interractive Data-Table component.
*/
class Grid extends View
{
public $menu = null;

public $quickSearch = null;

public $table = null;

public $buttons = null;

public $actions = null;

public $defaultTemplate = 'grid.html';

public $ipp = 20;

public $paginator = null;

public $selection = null;

public function init()
{
parent::init();

if (!$this->menu) {
$this->menu = $this->add(['Menu', 'activate_on_click'=>false], 'Menu');
}

if (!$this->table) {
$this->table = $this->add(['Table', 'very compact'], 'Table');
}
}

public function addButton($text)
{
return $this->menu->addItem()->add(new Button($text));
}

public function addQuickSearch($fields = [])
{
if (!$fields) {
$fields = [$this->model->title_field];
}

$x = $this->menu->addMenuRight();
$this->quickSearch = $x->addItem()->setElement('div')->add(new \atk4\ui\FormField\Input(['placeholder'=>'Search', 'icon'=>'search']))->addClass('transparent');
}

public function addAction($label, $action)
{
if (!$this->actions) {
$this->actions = $this->table->addColumn('TableColumn/Actions');
}

$this->actions->addAction($label, $action);
}

public function setModel(\atk4\data\Model $model, $columns = null)
{
return $this->model = $this->table->setModel($model, $columns)->setLimit($this->ipp);
}

public function addSelection()
{
$this->selection = $this->table->addColumn('TableColumn/Checkbox');

// Move element to the beginning
$k = array_search($this->selection, $this->table->columns);
$this->table->columns = [$k => $this->table->columns[$k]] + $this->table->columns;

return $this->selection;
}
}
23 changes: 20 additions & 3 deletions src/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ class Menu extends View

public function addItem($name = null, $action = null)
{
$item = $this->add(new Item(['element'=>'a']));
if (!is_null($name)) {
$item->set($name);
if (is_object($name)) {
$item = $name;
} elseif ($name) {
$item = new Item($name);
} else {
$item = new Item();
}

$item = $this->add($item)->setElement('a');

if (is_array($action)) {
$action = $this->app->url($action);
}
Expand All @@ -30,6 +35,10 @@ public function addItem($name = null, $action = null)
$item->setAttr('href', $action);
}

if ($action instanceof jsExpressionable) {
$item->js('click', $action);
}

return $item;
}

Expand Down Expand Up @@ -77,6 +86,14 @@ public function addGroup($title)
return $group;
}

public function addMenuRight()
{
$menu = $this->add(new self(), ['RightMenu', 'ui'=>false]);
$menu->removeClass('item')->addClass('right menu');

return $menu;
}

public function add($object, $region = null)
{
$item = parent::add($object, $region);
Expand Down
51 changes: 41 additions & 10 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,21 @@ class Table extends Lister
*
* @param string $name Data model field name
* @param Column\Generic $columnDef
* @param array $fieldDef Array of defaults for new Model field
*
* @return Column\Generic
*/
public function addColumn($name, $columnDef = null, $fieldDef = [])
public function addColumn($name, $columnDef = null)
{
if (!$this->model) {
$this->model = new \atk4\ui\misc\ProxyModel();
}

$field = $this->model->hasElement($name);
if (!$field) {
$field = $this->model->addField($name, $fieldDef);
if ($name !== null) {
$field = $this->model->hasElement($name);
if (!$field) {
$columnDef = $name;
$name = null;
}
}

if ($columnDef === null) {
Expand All @@ -132,7 +134,16 @@ public function addColumn($name, $columnDef = null, $fieldDef = [])
}

$columnDef->table = $this;
$this->columns[$name] = $columnDef;
if (is_null($name)) {
$this->columns[] = $columnDef;
} elseif (isset($this->columns[$name])) {
if (!is_array($this->columns[$name])) {
$this->columns[$name] = [$this->columns[$name]];
}
$this->columns[$name][] = $columnDef;
} else {
$this->columns[$name] = $columnDef;
}

return $columnDef;
}
Expand Down Expand Up @@ -336,9 +347,19 @@ public function renderHeaderCells()
{
$output = [];
foreach ($this->columns as $name => $column) {
$field = $this->model->getElement($name);

$output[] = $column->getHeaderCell($field);
// If multiple formatters are defined, use the first for the header cell
if (is_array($column)) {
$column = $column[0];
}

if (!is_int($name)) {
$field = $this->model->getElement($name);

$output[] = $column->getHeaderCell($field);
} else {
$output[] = $column->getHeaderCell();
}
}

return implode('', $output);
Expand Down Expand Up @@ -384,9 +405,19 @@ public function getRowTemplate()
{
$output = [];
foreach ($this->columns as $name => $column) {
$field = $this->model->getElement($name);

$output[] = $column->getCellTemplate($field);
// If multiple formatters are defined, use the first for the header cell
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If data cell has multiple formatters, then all of them should be used here. This is not header cell here !

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unclear how to implement that. I need more info in #92 and #67. The mechanism is there, so I propose we just look for bugs and various combinations then address them.

if (is_array($column)) {
$column = $column[0];
}

if (!is_int($name)) {
$field = $this->model->getElement($name);

$output[] = $column->getCellTemplate($field);
} else {
$output[] = $column->getCellTemplate();
}
}

return implode('', $output);
Expand Down
40 changes: 40 additions & 0 deletions src/TableColumn/Actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace atk4\ui\TableColumn;

/**
* Formatting money.
*/
class Actions extends Generic
{
public $actions = [];

public function addAction($button, $callback)
{
$name = 'action_'.(count($this->actions) + 1);

if (!is_object($button)) {
$button = new \atk4\ui\Button($button);
}
$button->app = $this->table->app;

$this->actions[$name] = $button;
$button->addClass('b_'.$name);
$button->addClass('compact');
$this->table->on('click', '.b_'.$name, $callback);
}

public function getCellTemplate(\atk4\data\Field $f = null)
{
$output = '';

// render our actions
foreach ($this->actions as $action) {
$output .= $action->getHTML();
}

return $this->getTag('td', 'body', [$output]);
}

// rest will be implemented for crud
}
Loading