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

Revision 2802, 4.3 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1058 - Permitindo o envio de formulário por requisição background

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