Completed
Push — master ( f99fed...57dceb )
by Jean-Christophe
03:29
created

JsonDataTable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 76
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A _generateContent() 0 5 1
A _addRowModel() 0 7 1
A _associatePaginationBehavior() 0 17 3
A jsJsonArray() 0 4 1
A jsonArrayOn() 0 3 1
A jsonArrayOnClick() 0 3 1
A paginate() 0 3 1
1
<?php
2
3
namespace Ajax\semantic\widgets\datatable;
4
5
use Ajax\semantic\widgets\datatable\DataTable;
6
use Ajax\service\JReflection;
7
use Ajax\common\html\BaseHtml;
8
use Ajax\service\AjaxCall;
9
use Ajax\JsUtils;
10
use Ajax\semantic\html\collections\menus\HtmlMenu;
11
12
class JsonDataTable extends DataTable {
13
	protected $_modelClass="_jsonArrayModel";
14
15
	public function __construct($identifier, $model, $modelInstance=NULL) {
16
		parent::__construct($identifier, $model, $modelInstance);
17
	}
18
19
	protected function _generateContent($table){
20
		$this->_addRowModel($table);
21
		$this->_rowClass="_json";
22
		parent::_generateContent($table);
23
	}
24
25
	protected function _addRowModel($table){
26
		$object=JReflection::jsonObject($this->_model);
27
		$row=$this->_generateRow($object, $table,"_jsonArrayChecked");
28
		$row->setClass($this->_modelClass);
29
		$row->addToProperty("style","display:none;");
30
		$table->getBody()->_addRow($row);
31
	}
32
33
	/**
34
	 * {@inheritDoc}
35
	 * @see DataTable::_associatePaginationBehavior()
36
	 */
37
	protected function _associatePaginationBehavior(HtmlMenu $menu,JsUtils $js=NULL){
38
		$callback=null;
39
		if(isset($js)){
40
			$id=$this->identifier;
41
			$offset=$js->scriptCount();
42
			$this->run($js);
43
			$callback=$js->getScript($offset);
44
			$callback.=$js->trigger("#".$id." [name='selection[]']","change",false)."$('#".$id." tbody .ui.checkbox').checkbox();".$js->execOn("change", "#".$id." [name='selection[]']", $this->_getCheckedChange($js));
45
			$callback.=";var page=parseInt($(self).attr('data-page'));
46
			$('#pagination-{$id} .item').removeClass('active');
47
			$('#pagination-{$id} [data-page='+page+']:not(.no-active)').addClass('active');
48
			$('#pagination-{$id} ._firstPage').attr('data-page',Math.max(1,page-1));
49
			var lastPage=$('#pagination-{$id} ._lastPage');lastPage.attr('data-page',Math.min(lastPage.attr('data-max'),page+1));";
50
		}
51
		if(isset($this->_urls["refresh"]))
52
			$this->jsonArrayOnClick($menu, $this->_urls["refresh"],"post","{'p':$(this).attr('data-page')}",$callback);
53
	}
54
55
	/**
56
	 * Returns a new AjaxCall object, must be compiled using $jquery object
57
	 * @param string $url
58
	 * @param string $method
59
	 * @param string $params
60
	 * @param callable $jsCallback
61
	 * @return AjaxCall
62
	 */
63
	public function jsJsonArray($url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
64
		$parameters=\array_merge($parameters,["modelSelector"=>"#".$this->_identifier." tr.".$this->_modelClass,"url"=>$url,"method"=>$method,"params"=>$params,"jsCallback"=>$jsCallback]);
65
		return new AjaxCall("jsonArray", $parameters);
66
	}
67
68
	public function jsonArrayOn(BaseHtml $element,$event,$url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
69
		return $element->_addEvent($event, $this->jsJsonArray($url,$method,$params,$jsCallback,$parameters));
70
	}
71
72
	public function jsonArrayOnClick(BaseHtml $element,$url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
73
		return $this->jsonArrayOn($element, "click", $url,$method,$params,$jsCallback,$parameters);
74
	}
75
76
	/**
77
	 * Paginates the DataTable element with a Semantic HtmlPaginationMenu component
78
	 * @param number $page the active page number
79
	 * @param number $total_rowcount the total number of items
80
	 * @param number $items_per_page The number of items per page
81
	 * @param number $pages_visibles The number of visible pages in the Pagination component
82
	 * @return DataTable
83
	 */
84
	public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=null){
85
		return parent::paginate($page, $total_rowcount,$items_per_page,null);
86
	}
87
}