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

Revision 2903, 5.4 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1058 - Corrigindo problemas na inclusão de folhas de estilo.

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 _css = _pData.match( new RegExp( '<link type="text\/css" rel="StyleSheet"[^>]*\/>', 'gi' ) );
106                        if ( _css )
107                                for ( var i = 0; _css, i < _css.length; i++ )
108                                {
109                                        _pData = _pData.replace( _css[ i ], '' );
110                                        XStyle.add( ( /<[^>]*href="(\S+)"[^>]*\/>/i ).exec( _css[ i ] )[ 1 ] );
111                                }
112
113                        var _js = _pData.match( new RegExp( '<script[^>]*>.*?<\/script>', 'gi' ) );
114                        if ( _js )
115                                for ( var i = 0; i < _js.length; i++ )
116                                {
117                                        _pData = _pData.replace( _js[ i ], '' );
118
119                                        _js[ i ] = ( /<script([^>]+src=["'](\S+)["'])?[^>]*>(.*?)<\/script>/i ).exec( _js[ i ] );
120
121                                        var _external = _js[ i ][ 2 ];
122
123                                        if ( _external == undefined || _external.length == 0 )
124                                                _script.inline( _js[ i ][ 3 ].replace( _line_feed, '\n' ) );
125                                        else
126                                        {
127                                                if ( _external.indexOf( window.location.host ) < 0 )
128                                                        _external = window.location.protocol + '//' + window.location.host + _external;
129
130                                                _script.external(
131                                                        _external.substr(
132                                                                _path.length - 1,
133                                                                _external.length - _path.length + 1 - (
134                                                                        ( _external.indexOf( '?' ) < 0 ) ?
135                                                                                0 : ( _external.length - _external.indexOf( '?' ) )
136                                                                )
137                                                        )
138                                                );
139                                        }
140                                }
141
142                        var el = document.createElement( 'div' );
143                        el.innerHTML = _pData.replace( _line_feed, '' );
144
145                        var _links = el.getElementsByTagName( 'a' );
146                        for ( var i = 0; i < _links.length; i++ )
147                                _intercept( _links.item( i ) );
148
149                        if ( _handlers_registred.after )
150                                _handlers_registred.after( el );
151
152                        _script.load( );
153                }
154
155                function _handler( )
156                {
157                        if ( arguments.length != 1 )
158                                return false;
159
160                        switch ( arguments[ 0 ].constructor )
161                        {
162                                case Function :
163                                        _handler.after = arguments[ 0 ];
164                                        break;
165                                case Object :
166                                        if ( arguments[ 0 ].after && arguments[ 0 ].after.constructor == Function )
167                                                _handlers_registred.after = arguments[ 0 ].after;
168
169                                        if ( arguments[ 0 ].before && arguments[ 0 ].before.constructor == Function )
170                                                _handlers_registred.before = arguments[ 0 ].before;
171
172                                        if ( arguments[ 0 ].data && arguments[ 0 ].data.constructor == Function )
173                                                _handlers_registred.data = arguments[ 0 ].data;
174                                        break;
175                                default : return false;
176                        }
177                }
178
179                this.click = function( )
180                {
181                        _click( arguments[ 0 ],
182                                (
183                                        ( arguments.length > 1 && arguments[ 1 ] && arguments[ 1 ].constructor == Boolean ) ?
184                                                arguments[ 1 ] : false
185                                ),
186                                (
187                                        ( arguments.length > 2 && arguments[ 2 ] && arguments[ 2 ].constructor == String ) ?
188                                                arguments[ 2 ] : null
189                                )
190                        );
191                };
192
193                this.intercept = _intercept;
194                this.handler = _handler;
195                this.manipulator = _manipulator;
196        }
197
198        window.XLink = Link;
199} )( );
Note: See TracBrowser for help on using the repository browser.