PageSourceSearch

https://ccojubilee.org/js/ss/utils/Router.js?1790833698

js ccojubilee.org collected 2026-10-02 06:14:29 UTC 10,241 bytes, 237 lines download raw bytes

1/**
2 * @class SS.utils.Router
3 * @singleton
4 * 
5 * Router utilities to easily create urls to communicate with the backend system.
6 * @author Softped Development Team <[email protected]>
7 */
8
9if (typeof(Ext) != 'undefined' && Ext.define) {
10    
11    Ext.define('SS.utils.Router',{
12        singleton:true,
13
14        /**
15         * Creates a Site Stacker url based on the options given (see below)
16         *
17         * This method is similar to `Router::url()` method in CakePHP, except that is has the extra property `asClass`
18         *
19         * @param {Object} options Has the following properties:
20         *
21         * * `plugin` [optional] The plugin name. Default is null
22         * * `controller_prefix` [optional] The controller prefix (i.e. 'admin'). Default is null
23         * * `controller` [required] The controller name (i.e. 'Articles'). Default is 'UnspecifiedController'
24         * * `action` [required] The controller's action name (i.e. 'getTopTen'). Default is 'UnspecifiedAction'
25         * * `ext` [optional] The extension (i.e. 'json'). Default is null
26         * * `?` [optional] An object with query params (i.e. {'param'=>'value'}). Default is null
27         * * `#` [optional] A string that allows you to set URL hash fragments (i.e. 'top'). Default is null
28         * * `full_base` [optional] If true the FULL_BASE_URL constant will be prepended to generated URLs. Default is false
29         * * `asClass` [optional] If true the generated url will contain '.' (dots) instead of / to be used as a JS class. Default is false
30         *
31         * @return {String}
32         */
33        url: function(options) {
34            var defaults = {
35                'plugin': null,
36                'controller_prefix': null,
37                'controller': 'UnspecifiedController',
38                'action': 'UnspecifiedAction',
39                'ext': null,
40                '?': null,
41                '#': null,
42                'full_base': false,
43                'asClass': false
44            };
45            var pluginUrlPrefix = 'p',
46                appUrlPrefix = '_app_',
47                FULL_BASE_URL = window.location.origin ?
48                    window.location.origin :
49                    window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
50
51            for (var property in options) {
52                if (options[property] === undefined) {
53                    options[property] = defaults[property];
54                }
55            }
56
57            var o = options;
58            var url = [];
59            if (!o.asClass) url.push('');
60            url.push(o.plugin ? pluginUrlPrefix : appUrlPrefix);
61            if (o.plugin) url.push(o.plugin);
62            if (o.controller_prefix) url.push(o.controller_prefix);
63            url.push(o.controller);
64            url.push(o.action);
65            if (o.ext) url[url.length-1] += '.' + o.ext;
66            if (o['?']) {
67                var arr=[];
68                if(o['?'] instanceof Array) {
69                    arr = o['?'];
70                } else {
71                    for (var i in o['?']) arr.push(i+'='+o['?'][i]);
72                }
73                url[url.length-1] += '?' + arr.join('&');
74            }
75            if (o['#']) url[url.length-1] += '#' + o['#'];
76            if (o.full_base) url.unshift(FULL_BASE_URL);
77            return url.join(o.asClass ? '.' : '/');
78        }
79    },function(){
80        SS.route = function(plugin, controllerOrAction, action, controller_prefix, extension) {
81            if (arguments.length < 2) {
82                throw( "SS.route - at least 2 parameters are required" );
83            }
84            if (typeof controller_prefix == 'undefined' || (typeof controller_prefix == 'boolean' && controller_prefix)) controller_prefix = 'admin';
85            if (arguments.length == 2) {
86                return SS.utils.Router.url({
87                    plugin: plugin,
88                    controller: plugin,
89                    action:controllerOrAction,
90                    ext:'json',
91                    controller_prefix:controller_prefix
92                });
93            }
94            if (arguments.length == 3 || arguments.length == 4) {
95                return SS.utils.Router.url({
96                    plugin: plugin,
97                    controller: controllerOrAction,
98                    action:action,
99                    ext:'json',
100                    controller_prefix:controller_prefix
101                });
102            }
103            return SS.utils.Router.url({
104                plugin: plugin,
105                controller: controllerOrAction,
106                action:action,
107                ext:extension,
108                controller_prefix:controller_prefix
109            });
110        };
111    });
112    
113} else {
114    if (typeof(SS) == 'undefined') SS = {};
115    if (typeof(SS.utils) == 'undefined') SS.utils = {};
116
117    SS.utils.Router = {
118
119        /**
120         * Creates a Site Stacker url based on the options given (see below)
121         *
122         * This method is similar to `Router::url()` method in CakePHP, except that is has the extra property `asClass`
123         *
124         * @param {Object} options Has the following properties:
125         *
126         * * `plugin` [optional] The plugin name. Default is null
127         * * `controller_prefix` [optional] The controller prefix (i.e. 'admin'). Default is null
128         * * `controller` [required] The controller name (i.e. 'Articles'). Default is 'UnspecifiedController'
129         * * `action` [required] The controller's action name (i.e. 'getTopTen'). Default is 'UnspecifiedAction'
130         * * `ext` [optional] The extension (i.e. 'json'). Default is null
131         * * `?` [optional] An object with query params (i.e. {'param'=>'value'}). Default is null
132         * * `#` [optional] A string that allows you to set URL hash fragments (i.e. 'top'). Default is null
133         * * `full_base` [optional] If true the FULL_BASE_URL constant will be prepended to generated URLs. Default is false
134         * * `asClass` [optional] If true the generated url will contain '.' (dots) instead of / to be used as a JS class. Default is false
135         *
136         * @return {String}
137         */
138        url: function(options) {
139            var defaults = {
140                'plugin': null,
141                'controller_prefix': null,
142                'controller': 'UnspecifiedController',
143                'action': 'UnspecifiedAction',
144                'ext': null,
145                '?': null,
146                '#': null,
147                'full_base': false,
148                'asClass': false
149            };
150            var pluginUrlPrefix = 'p',
151                appUrlPrefix = '_app_',
152                FULL_BASE_URL = window.location.origin ?
153                    window.location.origin :
154                    window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
155
156            for (var property in options) {
157                if (options[property] === undefined) {
158                    options[property] = defaults[property];
159                }
160            }
161
162            var o = options;
163            var url = [];
164            if (!o.asClass) url.push('');
165            url.push(o.plugin ? pluginUrlPrefix : appUrlPrefix);
166            if (o.plugin) url.push(o.plugin);
167            if (o.controller_prefix) url.push(o.controller_prefix);
168            url.push(o.controller);
169            url.push(o.action);
170            if (o.ext) url[url.length-1] += '.' + o.ext;
171            if (o['?']) {
172                var arr=[];
173                if(o['?'] instanceof Array) {
174                    arr = o['?'];
175                } else {
176                    for (var i in o['?']) arr.push(i+'='+o['?'][i]);
177                }
178                url[url.length-1] += '?' + arr.join('&');
179            }
180            if (o['#']) url[url.length-1] += '#' + o['#'];
181            if (o.full_base) url.unshift(FULL_BASE_URL);
182            return url.join(o.asClass ? '.' : '/');
183        }
184    };
185
186    /**
187     * Shorthand for {@link SS.utils.Router#url}
188     *
189     * Only first 2 parameters are required. The following params rules apply:
190     *
191     * * if there are only 2 parameters set, it will use the first one as plugin and controller, the second one and action and the rest are default
192     * * if there are more than 2 parameters set, it will use all parameters in the default order
193     *
194     * @param {String} plugin The plugin name
195     * @param {String} controllerOrAction The controller name or the action if the controller
196     * is the same as the plugin name
197     * @param {String} [action] The action name
198     * @param {String|Boolean} [controller_prefix=admin] The controller prefix, usually 'admin'.
199     * If not set or true, it will be set to 'admin'.
200     * @param {String} [extension=json] The extension to use
201     * @return {String} Url
202     */
203    if (SS){
204        SS.route = function(plugin, controllerOrAction, action, controller_prefix, extension) {
205            if (arguments.length < 2) {
206                throw( "SS.route - at least 2 parameters are required" );
207            }
208            if (typeof controller_prefix == 'undefined' || (typeof controller_prefix == 'boolean' && controller_prefix)) controller_prefix = 'admin';
209            if (arguments.length == 2) {
210                return SS.utils.Router.url({
211                    plugin: plugin,
212                    controller: plugin,
213                    action:controllerOrAction,
214                    ext:'json',
215                    controller_prefix:controller_prefix
216                });
217            }
218            if (arguments.length == 3 || arguments.length == 4) {
219                return SS.utils.Router.url({
220                    plugin: plugin,
221                    controller: controllerOrAction,
222                    action:action,
223                    ext:'json',
224                    controller_prefix:controller_prefix
225                });
226            }
227            return SS.utils.Router.url({
228                plugin: plugin,
229                controller: controllerOrAction,
230                action:action,
231                ext:extension,
232                controller_prefix:controller_prefix
233            });
234        };
235    }
236}
237

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.