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

Revision 2875, 5.1 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1058 - Flexibilizando a manipulação de dados das requisição capturadas.

Line 
1( function( )
2{
3        var _connector = new XConnector;
4        var _script = new XScript;
5        var _location = window.location;
6
7        function Link( )
8        {
9                var _path = ( arguments.length && arguments[ 0 ].constructor == String ) ? arguments[ 0 ] : '/';
10
11                _path = (
12                        ( ( _path.indexOf( _location.protocol ) != 0 ) ? _location.protocol + '//' : '' )
13                        + ( ( _path.indexOf( _location.host ) != 0 ) ? _location.host : '' )
14                        + ( ( _path.indexOf( '/' ) != 0 ) ? '/' : '' )
15                ) + _path;
16
17                var _last_request = null;
18                var _handlers_registred = {
19                        after : null,
20                        before : null,
21                        data : null
22                };
23
24                if ( _location.protocol + '//' + _location.host + _location.pathname != _path )
25                        window.location.href = _path + (
26                                ( _location.hash ) ?
27                                        _location.hash : '#' + String( _location.protocol + '//' + _location.host + _location.pathname + _location.search ).substr( _path.length )
28                        );
29
30                function _click( _action, _cache )
31                {
32                        switch ( _action.indexOf( _location.pathname ) )
33                        {
34                                case -1 :
35                                        _action = _path + ( ( _action.indexOf( '/' ) == 0 ) ? _action.substr( 1 ) : _action );
36                                        break;
37                                case 0 :
38                                        _action = _action.replace( _location.pathname, _path );
39                                        break;
40                                default :
41                                        if ( _action.indexOf( _path ) != 0 )
42                                                _action = _path + _action;
43                        }
44
45                        var _post = ( arguments.length == 3 && arguments[ 2 ] && arguments[ 2 ].constructor == String ) ? arguments[ 2 ] : null;
46
47                        if ( _post == null && _action == _last_request )
48                                return false;
49
50                        if ( _handlers_registred.before )
51                                _handlers_registred.before( );
52
53                        _connector.go(
54                        {
55                                'access' : _action,
56                                'cache' : _cache,
57                                'handler' : _manipulator,
58                                'post' : _post
59                        } );
60
61                        _last_request = _action;
62
63                        window.location = '#' + (
64                                ( _action.indexOf( _path ) < 0 ) ?
65                                        _action : _action.substr( _action.indexOf( _path ) + _path.length )
66                        );
67                }
68
69                function _intercept( pLink )
70                {
71                        var action = pLink.href;
72                        if ( action == '' )
73                                return false;
74
75                        var a = ( action.indexOf( 'javascript:' ) === 0 );
76                        a = ( a || ( action.indexOf( '#' ) === 0 ) );
77                        a = ( a || ( action.indexOf( window.location + '#' ) === 0 ) );
78                        a = ( a || ! ( action.indexOf( 'workflow' ) < 0 ) );
79
80                        if ( ! a )
81                                pLink.onclick = function( )
82                                {
83                                        var _cacheit = false;//( ( pLink.hasAttribute( 'cacheit' ) && pLink.getAttribute( 'cacheit' ) === 'true' ) ? true : false );
84
85                                        _click( action, _cacheit );
86
87                                        return false;
88                                };
89                }
90
91                function _manipulator( _pData )
92                {
93                        if ( ! ( _pData.constructor == String ) )
94                                return false;
95
96                        if ( _handlers_registred.data )
97                                _pData = _handlers_registred.data( _pData );
98
99                        var _line_feed = '__LINE_FEED_CHARACTER_' + ( Date.parse( new Date ) ) + '__';
100
101                        _pData = _pData.trim( ).replace( /[\r\t]/g, '' ).replace( /\n/g, _line_feed );
102
103                        _line_feed = new RegExp( _line_feed, 'g' );
104
105                        var _js = _pData.match( new RegExp( '<script[^>]*>.*?<\/script>', 'gi' ) );
106
107                        if ( _js )
108                        {
109                                var _tag = /<script([^>]+src=["'](\S+)["'])?[^>]*>(.*?)<\/script>/i;
110
111                                for ( var i = 0; i < _js.length; i++ )
112                                {
113                                        _pData = _pData.replace( _js[ i ], '' );
114
115                                        _js[ i ] = _tag.exec( _js[ i ] );
116
117                                        var _external = _js[ i ][ 2 ];
118
119                                        if ( _external == undefined || _external.length == 0 )
120                                                _script.inline( _js[ i ][ 3 ].replace( _line_feed, '\n' ) );
121                                        else
122                                        {
123                                                if ( _external.indexOf( window.location.host ) < 0 )
124                                                        _external = window.location.protocol + '//' + window.location.host + _external;
125
126                                                _script.external(
127                                                        _external.substr(
128                                                                _path.length - 1,
129                                                                _external.length - _path.length + 1 - (
130                                                                        ( _external.indexOf( '?' ) < 0 ) ?
131                                                                                0 : ( _external.length - _external.indexOf( '?' ) )
132                                                                )
133                                                        )
134                                                );
135                                        }
136                                }
137                        }
138
139                        var el = document.createElement( 'div' );
140                        el.innerHTML = _pData.replace( _line_feed, '' );
141
142                        var _links = el.getElementsByTagName( 'a' );
143                        for ( var i = 0; i < _links.length; i++ )
144                                _intercept( _links.item( i ) );
145
146                        if ( _handlers_registred.after )
147                                _handlers_registred.after( el );
148
149                        _script.load( );
150                }
151
152                function _handler( )
153                {
154                        if ( arguments.length != 1 )
155                                return false;
156
157                        switch ( arguments[ 0 ].constructor )
158                        {
159                                case Function :
160                                        _handler.after = arguments[ 0 ];
161                                        break;
162                                case Object :
163                                        if ( arguments[ 0 ].after && arguments[ 0 ].after.constructor == Function )
164                                                _handlers_registred.after = arguments[ 0 ].after;
165
166                                        if ( arguments[ 0 ].before && arguments[ 0 ].before.constructor == Function )
167                                                _handlers_registred.before = arguments[ 0 ].before;
168
169                                        if ( arguments[ 0 ].data && arguments[ 0 ].data.constructor == Function )
170                                                _handlers_registred.data = arguments[ 0 ].data;
171                                        break;
172                                default : return false;
173                        }
174                }
175
176                this.click = function( )
177                {
178                        _click( arguments[ 0 ],
179                                (
180                                        ( arguments.length > 1 && arguments[ 1 ] && arguments[ 1 ].constructor == Boolean ) ?
181                                                arguments[ 1 ] : false
182                                ),
183                                (
184                                        ( arguments.length > 2 && arguments[ 2 ] && arguments[ 2 ].constructor == String ) ?
185                                                arguments[ 2 ] : null
186                                )
187                        );
188                };
189
190                this.intercept = _intercept;
191                this.handler = _handler;
192                this.manipulator = _manipulator;
193        }
194
195        window.XLink = Link;
196} )( );
Note: See TracBrowser for help on using the repository browser.