Completed
Push — master ( 4bf172...5f7aee )
by Jean-Christophe
03:35
created

JsonDataTable::_createRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
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
/**
13
 * @author jc
14
 * a DataTable refreshed with JSON
15
 * @since 2.2.2
16
 */
17
class JsonDataTable extends DataTable {
18
	protected $_modelClass="_jsonArrayModel";
19
	protected $_rowModelCallback;
20
21
	public function __construct($identifier, $model, $modelInstance=NULL) {
22
		parent::__construct($identifier, $model, $modelInstance);
23
		$this->_rowClass="_json";
24
	}
25
26
	protected function _generateContent($table){
27
		$this->_addRowModel($table);
28
		parent::_generateContent($table);
29
	}
30
31
	protected function _addRowModel($table){
32
		$row=$this->_createRow($table, $this->_modelClass);
33
		$row->addToProperty("style","display:none;");
34
		$table->getBody()->_addRow($row);
35
	}
36
37
	protected function _createRow($table,$rowClass){
38
		$object=JReflection::jsonObject($this->_model);
39
		if(isset($this->_rowModelCallback)){
40
			$callback=$this->_rowModelCallback;
41
			$callback($object);
42
		}
43
		$row=$this->_generateRow($object, $table,"_jsonArrayChecked");
44
		$row->setClass($rowClass);
45
		return $row;
46
	}
47
48
	/**
49
	 * {@inheritDoc}
50
	 * @see DataTable::_associatePaginationBehavior()
51
	 */
52
	protected function _associatePaginationBehavior(HtmlMenu $menu,JsUtils $js=NULL){
53
		$callback=null;
54
		if(isset($js)){
55
			$id=$this->identifier;
56
			$offset=$js->scriptCount();
57
			$this->run($js);
58
			$callback=$js->getScript($offset);
59
			$callback.=$js->trigger("#".$id." [name='selection[]']","change",false)."$('#".$id." tbody .ui.checkbox').checkbox();".$js->execOn("change", "#".$id." [name='selection[]']", $this->_getCheckedChange($js));
60
			$callback.=";var page=parseInt($(self).attr('data-page'));
61
			$('#pagination-{$id} .item').removeClass('active');
62
			$('#pagination-{$id} [data-page='+page+']:not(.no-active)').addClass('active');
63
			$('#pagination-{$id} ._firstPage').attr('data-page',Math.max(1,page-1));
64
			var lastPage=$('#pagination-{$id} ._lastPage');lastPage.attr('data-page',Math.min(lastPage.attr('data-max'),page+1));";
65
		}
66
		if(isset($this->_urls["refresh"]))
67
			$this->jsonArrayOnClick($menu, $this->_urls["refresh"],"post","{'p':$(this).attr('data-page')}",$callback);
68
	}
69
70
	/**
71
	 * Returns a new AjaxCall object, must be compiled using $jquery object
72
	 * @param string $url
73
	 * @param string $method
74
	 * @param string $params
75
	 * @param string $jsCallback
76
	 * @return AjaxCall
77
	 */
78
	public function jsJsonArray($url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
79
		$parameters=\array_merge($parameters,["modelSelector"=>"#".$this->_identifier." tr.".$this->_modelClass,"url"=>$url,"method"=>$method,"params"=>$params,"jsCallback"=>$jsCallback]);
80
		return new AjaxCall("jsonArray", $parameters);
81
	}
82
83
	public function jsClear(){
84
		return "$('#{$this->identifier} tbody').find('._json').remove();";
85
	}
86
87
	public function clearOn(BaseHtml $element,$event, $stopPropagation=false, $preventDefault=false){
88
		return $element->addEvent($event, $this->jsClear(),$stopPropagation,$preventDefault);
89
	}
90
91
	public function clearOnClick(BaseHtml $element,$stopPropagation=false, $preventDefault=false){
92
		return $this->clearOn($element, "click",$stopPropagation,$preventDefault);
93
	}
94
95
	public function jsonArrayOn(BaseHtml $element,$event,$url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
96
		return $element->_addEvent($event, $this->jsJsonArray($url,$method,$params,$jsCallback,$parameters));
97
	}
98
99
	public function jsonArrayOnClick(BaseHtml $element,$url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
100
		return $this->jsonArrayOn($element, "click", $url,$method,$params,$jsCallback,$parameters);
101
	}
102
103
	/**
104
	 * Paginates the DataTable element with a Semantic HtmlPaginationMenu component
105
	 * @param number $page the active page number
106
	 * @param number $total_rowcount the total number of items
107
	 * @param number $items_per_page The number of items per page
108
	 * @param number $pages_visibles The number of visible pages in the Pagination component
109
	 * @return DataTable
110
	 */
111
	public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=null){
112
		return parent::paginate($page, $total_rowcount,$items_per_page,null);
113
	}
114
115
	public function setRowModelCallback($_rowModelCallback) {
116
		$this->_rowModelCallback=$_rowModelCallback;
117
		return $this;
118
	}
119
120
}