mahi /atk4/lib/jQuery/Chain.php

Language PHP Lines 246
MD5 Hash 5dbb95a9008b7fa430b840204cfa6d0e
Repository https://github.com/mahimarathore/mahi.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/***********************************************************
  jQuery chain proxy. Returned by view->js(). Calls to this
  class will be converted into jQuery calls.

  Feel free to call _XX functions in this class

  Reference:
  http://agiletoolkit.org/doc/ref

==ATK4===================================================
   This file is part of Agile Toolkit 4
    http://agiletoolkit.org/

   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
=====================================================ATK4=*/
/*
   This class represents sequentall calls to one jQuery object
 */
class jQuery_Chain extends AbstractModel {
    public $str='';
    public $prepend='';
    public $selector=null;
    public $enclose=false;
    public $preventDefault=false;
    public $base='';
    function __call($name,$arguments){
        if($arguments){
            $a2=$this->_flattern_objects($arguments,true);
            $this->str.=".$name(".$a2.")";
        }else{
            $this->str.=".$name()";
        }
        return $this;
    }
    /* convert reserved words or used methods into js calls, such as "execute" */
    function _fn($name,$arguments=array()){
        // Wrapper for functons which use reserved words
        return $this->__call($name,$arguments);
    }
    /* converting this object into string will produce JavaScript code */
    function __toString(){
        return $this->_render();
    }

    /* Some methods shouldn't be special! */
    function each(){
        return $this->__call('each',func_get_args());
    }

    /* Chain binds to parent object by default. Use this to use other selector $('selector') */
    function _selector($selector=null){
        $this->selector=$selector;
        return $this;
    }
    /* Use this to bind chain to document $(document)... */
    function _selectorDocument(){
        $this->selector='__atk_selector_document';
        return $this;
    }
    /* Use this to bind chain to window $(window)... */
    function _selectorWindow(){
        $this->selector='__atk_selector_window';
        return $this;
    }
    /* Use this to bind chain to "this" $(this)... */
    function _selectorThis(){
        $this->selector='__atk_selector_this';
        return $this;
    }
    /* Use this to bind chain to "region" $(region). Region is defined by ATK when reloading */
    function _selectorRegion(){
        $this->selector='__atk_selector_region';
        return $this;
    }
    /* Execute more JavaScript code before chain. Avoid using. */
    function _prepend($code){
        if(is_array($code)){
            $code=join(';',$code);
        }
        $this->prepend=$code.';'.$this->prepend;
        return $this;
    }
    function debug(){
        $this->debug=true;
        return $this;
    }
    /* Send chain in response to form submit, button click or ajaxec() function for AJAX control output */
    function execute(){
        if(isset($_POST['ajax_submit']) || $_SERVER['HTTP_X_REQUESTED_WITH']=='XMLHttpRequest'){
            //if($this->api->jquery)$this->api->jquery->getJS($this->owner);

            $x=$this->api->template->get('document_ready');
            if(is_array($x))$x=join('',$x);
            echo $this->_render();
            $this->api->hook('post-js-execute');
            exit;
        }else return $this;
    }
    /* [private] used by custom json_encoding */
    function _safe_js_string($str) {
        $l=strlen($str);
        $ret="";
        for($i=0;$i<$l;++$i) {
            switch($str[$i]) {
                case "\r": $ret.="\\r"; break;
                case "\n": $ret.="\\n"; break;
                case "\"":     case "'":     case "<": case ">":  case "&":  case "\\":
                           $ret.='\x'.dechex( ord($str[$i] ) );
                           break;
                default:
                           $ret.=$str[$i];
                           break;
            }
        }
        return $ret;
    }
    /* [private] custom json_encoding. called on function arguments. */
    protected function _flattern_objects($arg,$return_comma_list=false){
        /*
         * This function is very similar to json_encode, however it will traverse array
         * before encoding in search of objects based on AbstractObject. Those would
         * be replaced with their json representation if function exists, otherwise
         * with string representation
         */
        if(is_object($arg)){
            if($arg instanceof jQuery_Chain){
                $r=$arg->_render();
                if(substr($r,-1)==';')$r=substr($r,0,-1);
                return $r;
            }elseif($arg instanceof AbstractView){
                return "'#".str_replace('/','_',$arg->name)."'";
            }else{
                return "'".$this->_safe_js_string((string)$arg)."'";    // indirectly call toString();
            }
        }elseif(is_array($arg)){
            $a2=array();
            // is array associative? (hash)
            $assoc=$arg!=array_values($arg);

            foreach($arg as $key=>$value){
                $value=$this->_flattern_objects($value);
                $key=$this->_flattern_objects($key);
                if(!$assoc || $return_comma_list){
                    $a2[]=$value;
                }else{
                    $a2[]=$key.':'.$value;
                }
            }
            if($return_comma_list){
                $s=join(',',$a2);
            }elseif($assoc){
                $s='{'.join(',',$a2).'}';
            }else{
                $s='['.join(',',$a2).']';
            }
        }elseif(is_string($arg)){
            $s="'".$this->_safe_js_string($arg)."'";
        }elseif(is_bool($arg)){
            $s=json_encode($arg);
        }elseif(is_numeric($arg)){
            $s=json_encode($arg);
        }elseif(is_null($arg)){
            $s=json_encode($arg);
        }else{
            throw $this->exception('Unable to encode value for jQuery Chain - unknown type')
                ->addMoreInfo('arg',$arg);
        }
        return $s;
    }
    /* Calls real redirect (from univ), but accepts page name. Use url() for 1st argument manually anyway. */
    function redirect($page=null,$arg=null){
        $url=$this->api->url($page,$arg);
        return $this->_fn('redirect',array($url));
    }
    /* Reload object. You can bind this to custom event and trigger it if object is not directly accessible. */
    function reload($arguments=array(),$fn=null,$url=null){
        if($fn)$fn->_enclose();
        $id=$this->owner;
        if(!$url)$url=$this->api->url(null,array('cut_object'=>$id->name));
        return $this->_fn('atk4_reload',array($url,$arguments,$fn));
    }
    /* Chain will not be called but will return callable function instead. */
    function _enclose($fn=null,$preventDefault=false){
        // builds structure $('obj').$fn(function(){ $('obj').XX; });
        if($fn===null)$fn=true;
        $this->enclose=$fn;
        $this->preventDefault=$preventDefault;
        return $this;
    }
    function _render(){
        $ret='';
        $ret.=$this->prepend;
        if($this->selector===false){
            $ret.="$";
        }elseif($this->selector==='__atk_selector_this'){
            $ret.="$(this)";
        }elseif($this->selector==='__atk_selector_document'){
            $ret.="$(document)";
        }elseif($this->selector==='__atk_selector_window'){
            $ret.="$(window)";
        }elseif($this->selector==='__atk_selector_region'){
            $ret.="$(region)";
        }else{
            if($this->str)$ret.="$('".($this->selector?$this->selector:'#'.str_replace('/','_',$this->owner->name))."')";
        }
        $ret.=$this->str;
        if($this->enclose===true){
            if($this->preventDefault){
                $ret="function(ev){ev.preventDefault();ev.stopPropagation(); ".$ret." }";
            }else{
                $ret="function(){ ".$ret." }";
            }
        }elseif($this->enclose){
            $ret="$('".($this->selector?$this->selector:'#'.$this->owner->name)."')".
                ".bind('".$this->enclose."',function(ev){ ev.preventDefault();ev.stopPropagation(); ".$ret." })";
        }
        if(@$this->debug){
            echo "<font color='blue'>".htmlspecialchars($ret).";</font><br/>";
            $this->debug=false;
        }
        return $ret;
    }
    /* Returns HTML for a link with text $text. When clicked will execute this chain. */
    function getLink($text){
        return '<a href="javascript:void(0)" onclick="'.$this->getString().'">'.$text.'</a>';
    }
    function getString(){
        return $this->_render();
    }
    /* Specify requirement for stylesheet. Will load dynamically. */
    function _css($file){
        $this->api->jquery->addStylesheet($file);
        return $this;
    }
    /* Specify requirement for extra javascript include. Will load dynamically. */
    function _load($file){
        $this->api->jquery->addInclude($file);
        return $this;
    }
}
Back to Top