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 23 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';

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

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

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

$sel = $g->addSelection();
$g->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
1 change: 0 additions & 1 deletion docs/misc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ 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
Expand Down
81 changes: 79 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class App

public $ui_persistence = null;

/**
* Constructor.
*
* @param array $defaults
*/
public function __construct($defaults = [])
{
// Process defaults
Expand Down Expand Up @@ -88,6 +93,11 @@ public function __construct($defaults = [])
}
}

/**
* Catch exception.
*
* @param mixed $exception
*/
public function caughtException($exception)
{
$l = new \atk4\ui\App();
Expand All @@ -107,6 +117,11 @@ public function caughtException($exception)
$this->run_called = true;
}

/**
* Outputs debug info.
*
* @param string $str
*/
public function outputDebug($str)
{
echo 'DEBUG:'.$str.'<br/>';
Expand All @@ -116,6 +131,8 @@ public function outputDebug($str)
* Will perform a preemptive output and terminate. Do not use this
* directly, instead call it form Callback, jsCallback or similar
* other classes.
*
* @param string $output
*/
public function terminate($output = null)
{
Expand All @@ -124,6 +141,14 @@ public function terminate($output = null)
exit;
}

/**
* Initializes layout.
*
* @param string|Layout\Generic $layout
* @param array $options
*
* @return $this
*/
public function initLayout($layout, $options = [])
{
if (is_string($layout)) {
Expand Down Expand Up @@ -154,6 +179,14 @@ public function addStyle($style)
$this->html->template->appendHTML('HEAD', $this->getTag('style', $style));
}

/**
* Normalizes class name.
*
* @param string $name
* @param string $prefix
*
* @return string
*/
public function normalizeClassNameApp($name, $prefix = null)
{
if (strpos('/', $name) === false && strpos('\\', $name) === false) {
Expand All @@ -163,6 +196,11 @@ public function normalizeClassNameApp($name, $prefix = null)
return $name;
}

/**
* Create object and associate it with this app.
*
* @return object
*/
public function add()
{
if ($this->layout) {
Expand All @@ -180,6 +218,9 @@ public function add()
}
}

/**
* Runs app and echo rendered template.
*/
public function run()
{
$this->run_called = true;
Expand All @@ -201,6 +242,13 @@ public function init()
$this->_init();
}

/**
* Load template.
*
* @param string $name
*
* @return Template
*/
public function loadTemplate($name)
{
$template = new Template();
Expand Down Expand Up @@ -297,20 +345,49 @@ public function url($args = [])
* 10. pass array as text to net tags (array must contain 1 to 3 elements corresponding to arguments):
* getTag('a', ['href'=>'foo.html'], ['b','click here']);
* --> <a href="foo.html"><b>click here</b></a>
*
* @param string|array $tag
* @param string $attr
* @param string|array $value
*
* @return string
*/
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
Loading