Passed
Branch master (b3745b)
by Henry Stivens
03:37
created

default/public/javascript/jquery/jquery.kumbiaphp.js   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 239

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 239
rs 8.2857
c 0
b 0
f 0
cc 1
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * KumbiaPHP web & app Framework
3
 *
4
 * LICENSE
5
 *
6
 * This source file is subject to the new BSD license that is bundled
7
 * with this package in the file LICENSE.txt.
8
 * It is also available through the world-wide-web at this URL:
9
 * http://wiki.kumbiaphp.com/Licencia
10
 * If you did not receive a copy of the license and are unable to
11
 * obtain it through the world-wide-web, please send an email
12
 * to [email protected] so we can send you a copy immediately.
13
 *
14
 * Plugin para jQuery que incluye los callbacks basicos para los Helpers
15
 *
16
 * @copyright  Copyright (c) 2005 - 2017 Kumbia Team (http://www.kumbiaphp.com)
17
 * @license	http://wiki.kumbiaphp.com/Licencia	 New BSD License
18
 */
19
20
(function($) {
21
	/**
22
	 * Objeto KumbiaPHP
23
	 *
24
	 */
25
	$.KumbiaPHP = {
26
		/**
27
		 * Ruta al directorio public en el servidor
28
		 *
29
		 * @var String
30
		 */
31
		publicPath : null,
32
33
		/**
34
		 * Plugins cargados
35
		 *
36
		 * @var Array
37
		 */
38
		plugin: [],
39
40
		/**
41
		 * Muestra mensaje de confirmacion
42
		 *
43
		 * @param Object event
44
		 */
45
		cConfirm: function(event) {
46
			var este=$(this);
47
			if(!confirm(este.data('msg'))) {
48
				event.preventDefault();
49
			}
50
		},
51
52
		/**
53
		 * Aplica un efecto a un elemento
54
		 *
55
		 * @param String fx
56
		 */
57
		cFx: function(fx) {
58
			return function(event) {
59
				event.preventDefault();
60
				var este=$(this),
61
					rel = $('#'+este.data('to'));
62
				rel[fx]();
63
			}
64
		},
65
66
		/**
67
		 * Carga con AJAX
68
		 *
69
		 * @param Object event
70
		 */
71
		cRemote: function(event) {
72
			var este=$(this), rel = $('#'+este.data('to'));
73
			event.preventDefault();
74
			rel.load(this.href);
75
		},
76
77
		/**
78
		 * Carga con AJAX y Confirmacion
79
		 *
80
		 * @param Object event
81
		 */
82
		cRemoteConfirm: function(event) {
83
			var este=$(this), rel = $('#'+este.data('to'));
84
			event.preventDefault();
85
			if(confirm(este.data('msg'))) {
86
				rel.load(this.href);
87
			}
88
		},
89
90
		/**
91
		 * Enviar formularios de manera asincronica, via POST
92
		 * Y los carga en un contenedor
93
		 */
94
		cFRemote: function(event){
95
			event.preventDefault();
96
			este = $(this);
97
			var button = $('[type=submit]', este);
98
			button.attr('disabled', 'disabled');
99
			var url = este.attr('action');
100
			var div = este.attr('data-to');
101
			$.post(url, este.serialize(), function(data, status){
102
				var capa = $('#'+div);
103
				capa.html(data);
104
				capa.hide();
105
				capa.show('slow');
106
				button.attr('disabled', null);
107
			});
108
		},
109
110
		/**
111
		 * Carga con AJAX al cambiar select
112
		 *
113
		 * @param Object event
114
		 */
115
		cUpdaterSelect: function(event) {
116
            var $t = $(this),$u= $('#' + $t.data('update'))
117
				url = $t.data('url');
118
            $u.empty();
119
            $.get(url, {'id':$t.val()}, function(d){
120
				for(i in d){
121
					var a = $('<option />').text(d[i]).val(i);
122
					$u.append(a);
123
				}
124
			}, 'json');
125
		},
126
127
		/**
128
		 * Enlaza a las clases por defecto
129
		 *
130
		 */
131
		bind : function() {
132
            // Enlace y boton con confirmacion
133
            $("body").on('click', "a.js-confirm, input.js-confirm",this.cConfirm);
134
135
            // Enlace ajax
136
            $("body").on('click', "a.js-remote",this.cRemote);
137
138
            // Enlace ajax con confirmacion
139
            $("body").on('click', "a.js-remote-confirm",this.cRemoteConfirm);
140
141
            // Efecto show
142
            $("body").on('click', "a.js-show",this.cFx('show'));
143
144
            // Efecto hide
145
            $("body").on('click', "a.js-hide",this.cFx('hide'));
146
147
            // Efecto toggle
148
            $("body").on('click', "a.js-toggle",this.cFx('toggle'));
149
150
            // Efecto fadeIn
151
            $("body").on('click', "a.js-fade-in",this.cFx('fadeIn'));
152
153
            // Efecto fadeOut
154
            $("body").on('click', "a.js-fade-out",this.cFx('fadeOut'));
155
156
            // Formulario ajax
157
            $("body").on('submit',"form.js-remote", this.cFRemote);
158
159
            // Lista desplegable que actualiza con ajax
160
            $("body").on('change',"select.js-remote", this.cUpdaterSelect);
161
162
            // Enlazar DatePicker
163
			$.KumbiaPHP.bindDatePicker();
164
			
165
		},
166
167
        /**
168
         * Implementa la autocarga de plugins, estos deben seguir
169
         * una convención para que pueda funcionar correctamente
170
         */
171
        autoload: function(){
172
            var elem = $("[class*='jp-']");
173
            $.each(elem, function(i, val){
174
                var este = $(this); //apunta al elemento con clase jp-*
175
                var classes = este.attr('class').split(' ');
176
                for (i in classes){
177
                    if(classes[i].substr(0, 3) == 'jp-'){
178
                        if($.inArray(classes[i].substr(3),$.KumbiaPHP.plugin) != -1)
179
                            continue;
180
                        $.KumbiaPHP.plugin.push(classes[i].substr(3))
181
                    }
182
                }
183
            });
184
            var head = $('head');
185
            for(i in $.KumbiaPHP.plugin){
186
                $.ajaxSetup({ cache: true});
187
                head.append('<link href="' + $.KumbiaPHP.publicPath + 'css/' + $.KumbiaPHP.plugin[i] + '.css" type="text/css" rel="stylesheet"/>');
188
				$.getScript($.KumbiaPHP.publicPath + 'javascript/jquery/jquery.' + $.KumbiaPHP.plugin[i] + '.js', function(data, text){});
189
            }
190
		},
191
		
192
		/**
193
		 * Carga y Enlaza Unobstrusive DatePicker en caso de ser necesario
194
		 *
195
		 */
196
		bindDatePicker: function() {
197
			
198
			// Selecciona los campos input
199
			var inputs = $('input.js-datepicker');
200
			/**
201
			 * Funcion encargada de enlazar el DatePicker a los Input
202
			 *
203
			 */
204
			var bindInputs = function() {
205
				inputs.each(function() {
206
					var opts = {monthSelector: true,yearSelector:true};
207
					var input = $(this);
208
					// Verifica si hay mínimo
209
					if(input.attr('min') != undefined) {
210
						opts.dateMin = input.attr('min').split('-');
211
					}
212
					// Verifica si ha máximo
213
					if(input.attr('max') != undefined) {
214
						opts.dateMax = input.attr('max').split('-');
215
					}
216
217
					// Crea el calendario
218
					input.pickadate(opts);
219
				});
220
			}
221
222
			// Si ya esta cargado Unobstrusive DatePicker, lo integra de una vez
223
			if(typeof($.pickadate) != "undefined") {
224
				return bindInputs();
225
			}
226
227
			// Carga la hoja de estilos
228
			$('head').append('<link href="' + this.publicPath + 'css/pickadate.css" type="text/css" rel="stylesheet"/>');
229
230
			// Carga Unobstrusive DatePicker, para poder usar cache
231
			jQuery.ajax({ dataType: "script",cache: true, url: this.publicPath + 'javascript/jquery/pickadate.js'}).done(function(){
232
				bindInputs();
233
			});
234
		},
235
236
		/**
237
		 * Inicializa el plugin
238
		 *
239
		 */
240
		initialize: function() {
241
			// Obtiene el publicPath, restando los caracteres que sobran
242
			// de la ruta, respecto a la ruta de ubicacion del plugin de KumbiaPHP
243
			// "javascript/jquery/jquery.kumbiaphp.js"
244
			var src = $('script:last').attr('src');
245
			this.publicPath = src.substr(0, src.length - 37);
246
247
			// Enlaza a las clases por defecto
248
			$(function(){
249
				$.KumbiaPHP.bind();
250
				$.KumbiaPHP.autoload();
251
				
252
			});
253
		}
254
	}
255
256
	// Inicializa el plugin
257
	$.KumbiaPHP.initialize();
258
})(jQuery);
259