source: trunk/phpgwapi/js/tools/xlink.js @ 2712

Revision 2712, 4.4 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1058 - Adaptando todo o expressoAdmin1_2 para não realizar reload.

Line 
1( function( )
2{
3        var _path = null;
4        var _last_request = null;
5
6        var _connector = null;
7        var _script = new XScript;
8
9        var _container = null;
10        var _divAppboxHeader = null;
11
12        var _loading = new Image( );
13
14        function _click( _action, _cache )
15        {
16                if ( _action.indexOf( window.location.host ) < 0 )
17                        _action = window.location.protocol + '//' + window.location.host + _action;
18
19                var _post = ( arguments.length == 3 && arguments[ 2 ] && arguments[ 2 ].constructor == String ) ? arguments[ 2 ] : null;
20
21                if ( _post == null && _action == _last_request )
22                        return false;
23
24                if ( _divAppboxHeader )
25                        _divAppboxHeader.innerHTML = '';
26
27                if ( _container )
28                {
29                        var _center = document.createElement( 'center' );
30                        _center.appendChild( _loading );
31
32                        _container.innerHTML = '';
33                        _container.appendChild( _center );
34                }
35
36                _connector.go(
37                {
38                        'access' : _action,
39                        'cache' : _cache,
40                        'handler' : _handler,
41                        'post' : _post
42                } );
43
44                _last_request = _action;
45
46                window.location = '#' + (
47                        ( _action.indexOf( _path ) < 0 ) ?
48                                _action : _action.substr( _action.indexOf( _path ) + URL_SERVER.length )
49                );
50        }
51
52        function _intercept( pLink )
53        {
54                var action = pLink.href;
55                if ( action == '' )
56                        return false;
57
58                var a = ( action.indexOf( 'javascript:' ) === 0 );
59                a = ( a || ( action.indexOf( '#' ) === 0 ) );
60                a = ( a || ( action.indexOf( window.location + '#' ) === 0 ) );
61                a = ( a || ! ( action.indexOf( 'workflow' ) < 0 ) );
62
63                if ( ! a )
64                        pLink.onclick = function( )
65                        {
66                                var _cacheit = false;//( ( pLink.hasAttribute( 'cacheit' ) && pLink.getAttribute( 'cacheit' ) === 'true' ) ? true : false );
67
68                                _click( action, _cacheit );
69
70                                return false;
71                        };
72        }
73
74        function _handler( data )
75        {
76                var js = data.match( /<script[^>]*>.*?<\/script>/g );
77                var _new_scripts = [ ];
78                if ( js )
79                {
80                        var tag = /<script([^>]* src="(\S+)"|[^>]*)>(.*?)<\/script>/;
81                        for ( var i = 0; i < js.length; i++ )
82                        {
83                                var type = tag.exec( js[ i ] );
84                                data = data.replace( "\n\t\t" + type[ 0 ], '' );
85                                _new_scripts[ _new_scripts.length ] = type;
86                        }
87                }
88
89                var el = document.createElement( 'div' );
90                el.innerHTML = data;
91
92                var _links = el.getElementsByTagName( 'a' );
93                for ( var i = 0; i < _links.length; i++ )
94                        _intercept( _links.item( i ) );
95
96                _container.innerHTML = '';
97                while ( el.hasChildNodes( ) )
98                        _container.appendChild( el.firstChild );
99
100                var _count = 0;
101                function _scripts( )
102                {
103                        if ( _new_scripts[ _count ][ 2 ] == undefined || _new_scripts[ _count ][ 2 ].length == 0 )
104                                _script.load( function( )
105                                {
106                                        if ( ++_count < _new_scripts.length )
107                                                _scripts( );
108                                }, _new_scripts[ _count ][ 3 ] );
109                        else
110                        {
111                                if ( _new_scripts[ _count ][ 2 ].indexOf( window.location.host ) < 0 )
112                                        _new_scripts[ _count ][ 2 ] = window.location.protocol + '//' + window.location.host + _new_scripts[ _count ][ 2 ];
113
114                                _script.add(
115                                        _new_scripts[ _count ][ 2 ].substr(
116                                                _path.length - 1,
117                                                _new_scripts[ _count ][ 2 ].length - _path.length + 1 - (
118                                                        ( _new_scripts[ _count ][ 2 ].indexOf( '?' ) < 0 ) ?
119                                                                0 : ( _new_scripts[ _count ][ 2 ].length - _new_scripts[ _count ][ 2 ].indexOf( '?' ) )
120                                                )
121                                        )
122                                );
123                                if ( ++_count < _new_scripts.length )
124                                        _scripts( );
125                        }
126                }
127
128                if ( _new_scripts.length )
129                {
130                        _scripts( );
131                        _script.load( );
132                }
133        }
134
135        function Link( )
136        {
137                _path = ( arguments.length && arguments[ 0 ].constructor == String ) ? arguments[ 0 ] : '/';
138
139                if ( window.location.protocol + '//' + window.location.host + window.location.pathname != _path )
140                        window.location.href = _path + (
141                                ( window.location.hash ) ?
142                                        window.location.hash : '#' + String( window.location.protocol + '//' + window.location.host + window.location.pathname + window.location.search ).substr( _path.length )
143                        );
144
145                _loading.src = _path + 'phpgwapi/templates/'+template+'/images/loading.gif';
146                _connector = new XConnector;
147        }
148
149        Link.prototype.click = function( )
150        {
151                _click( arguments[ 0 ],
152                        (
153                                ( arguments.length > 1 && arguments[ 1 ] && arguments[ 1 ].constructor == Boolean ) ?
154                                        arguments[ 1 ] : false
155                        ),
156                        (
157                                ( arguments.length > 2 && arguments[ 2 ] && arguments[ 2 ].constructor == String ) ?
158                                        arguments[ 2 ] : null
159                        )
160                );
161        };
162        Link.prototype.intercept = _intercept;
163
164        window.XLink = Link;
165
166        XEvents.add( window, 'onload', function( )
167        {
168                _divAppboxHeader = document.getElementById( 'divAppboxHeader' );
169                _divAppboxHeader.innerHTML = '';
170
171                _container = document.getElementById( 'divAppbox' );
172        } );
173} )( );
Note: See TracBrowser for help on using the repository browser.