diff --git a/index.html b/index.html index 831cbbd..c7e3c73 100644 --- a/index.html +++ b/index.html @@ -4,157 +4,15 @@ + +
- - - - - - - - - +
+ Filtering - Local + + + + + + + + + +
+ +
+ Filtering - Remoto + + + + + + + + + +
+ +
+ Chaining - Local and Remote + + + + + + + + + +
+ +
diff --git a/jquery.chainem.js b/jquery.chainem.js index 70e69f4..90f396e 100644 --- a/jquery.chainem.js +++ b/jquery.chainem.js @@ -14,8 +14,7 @@ methods: {}, 'remote-methods': { buildPattern: true, - url: 'http://localhost/jquery-chainem/test_remote_script.php', -// url: 'http://chainem.localhost/test_remote_script.php', + url: 'http://chainem.localhost/test_remote_script.php', pattern: 'get' }, 'link-config': { @@ -97,7 +96,16 @@ function SelectLink($element, updatingMethod, settings){ this.options = []; this.settings = settings || {}; - + + // Configurable + if(this.settings['selectMode'] === "filtering"){ + this.getOptions = getOptionsForFiltering; + this.shouldPreventNextStep = shouldPreventNextStepForFiltering; + } else { + this.getOptions = getOptionsForChaining; + this.shouldPreventNextStep = shouldPreventNextStepForChaining; + } + // super(), a la Java genericLink.call(this, $element, updatingMethod); this.init(); @@ -106,7 +114,7 @@ /* Add/Override methods to SelectLink prototype (which is genericLink) */ SelectLink.prototype.init = function(){ this.loadOptions(); - + var link = this; // .change this.element @@ -170,13 +178,13 @@ }; function getOptionsForFiltering(newValues) { - return $.grep(this.options, function(e){ + return $.grep(this.options, function(e){ return $.inArray(e.id, newValues) !== -1; }); }; function getOptionsForChaining(newValues) { - return $.map(newValues, function(elem, key) { + return $.map(newValues, function(elem, key) { return {id: key, val: elem}; }); }; @@ -210,16 +218,22 @@ if(next) next.element.trigger('chainem.clear'); }; + // encontre el bug + // basicamente el prototipo se comparte entre instancias + // lo que no quiero en este caso + /* SelectLink.configure = function (settings) { + if(settings['selectMode'] === "filtering"){ - SelectLink.prototype.getOptions = getOptionsForFiltering; - SelectLink.prototype.shouldPreventNextStep = shouldPreventNextStepForFiltering; + this.getOptions = getOptionsForFiltering; + this.shouldPreventNextStep = shouldPreventNextStepForFiltering; } else { - SelectLink.prototype.getOptions = getOptionsForChaining; - SelectLink.prototype.shouldPreventNextStep = shouldPreventNextStepForChaining; + this.getOptions = getOptionsForChaining; + this.shouldPreventNextStep = shouldPreventNextStepForChaining; } }; - + */ + /* Chain model */ function Chain() { @@ -305,15 +319,7 @@ ); }, - configLinks: function () { - var plug = this; - - $.each(plug.settings['link-config'], function(linkType, linkTypeConf){ - var constructorName = plug.linkTypes[linkType]; - constructorName.configure(linkTypeConf); - }); - }, - + init: function() { var plug = this, $elements = this.element, @@ -321,7 +327,7 @@ linkType = ""; // Build prototype according to config - this.configLinks(); + // this.configLinks(); // Traversing the chain of elements $elements.each(function(i, elem){ @@ -330,11 +336,11 @@ updatingMethod = (i === 0)? function(){ return []; } : plug.getSourceMethod(id); updatingMethod.isRemote = (i === 0)? false : plug.isRemote(id); - + linkType = $el.prop("tagName").toLowerCase(); link = plug.createLink(linkType, $el, updatingMethod); - + plug.chain.push(link); }); @@ -399,16 +405,28 @@ remoteSettings = this.settings['remote-methods'], camelCasedId = id.charAt(0).toUpperCase() + this.toCamelCase(id.slice(1)); - + /* url += remoteSettings['url'] + '/'; url += (remoteSettings['buildPattern'])? remoteSettings['pattern']: ''; url += camelCasedId; - + */ + + url += remoteSettings['url'] + '/'; + + if((remoteSettings['buildPattern'])){ + url += remoteSettings['pattern']; // "get" by default + url += camelCasedId; // "X", camelcased + } + return { url: url, async: remoteSettings['asyncronic'], type: "POST", - data: (remoteSettings['buildPattern'])? previousValues: {'previousValues': previousValues, 'get': id}, + data: (remoteSettings['buildPattern'])? previousValues: { + 'previousValues': previousValues, + 'element': id, + 'verb': remoteSettings['pattern'] + }, dataType: "json" }; } diff --git a/local_chaining_test.js b/local_chaining_test.js new file mode 100644 index 0000000..01b7217 --- /dev/null +++ b/local_chaining_test.js @@ -0,0 +1,77 @@ +function daFuncionParaChaining(genero, espectaculo){ + + var ret = { + '0': 'NS', + '1': 'Iron Man 3', + '2': 'La familia de mi novia', + '3': 'Stravaganza', + '4': 'Les Luthiers' + }; + + if(espectaculo == '0'){ + + switch (genero){ + // Null es el caso en que el combo de genero + // queda vacio porque los combos anteriores + // quedan vacios + case null: + ret = {}; + break; + case '1': + delete ret['0']; + delete ret['1']; + delete ret['3']; + break; + case '2': + delete ret['0']; + delete ret['2']; + delete ret['3']; + delete ret['4']; + break; + case '3': + delete ret['0']; + delete ret['1']; + delete ret['2']; + delete ret['4']; + break; + } + } else if(espectaculo == '1'){ + // obra de teatro + switch (genero){ + case '0': + alert("todo en 0!"); + ret = {}; + break; + case '1': + delete ret['0']; + delete ret['1']; + delete ret['2']; + delete ret['3']; + break; + case '3': + delete ret['0']; + delete ret['1']; + delete ret['2']; + delete ret['4']; + break; + } + } else if(espectaculo == '2'){ + // pelicula + switch (genero){ + case '1': + delete ret['0']; + delete ret['1']; + delete ret['3']; + delete ret['4']; + break; + case '2': + delete ret['0']; + delete ret['2']; + delete ret['3']; + delete ret['4']; + break; + } + } + + return ret; +} \ No newline at end of file diff --git a/local_filtering_test.js b/local_filtering_test.js new file mode 100644 index 0000000..7eef563 --- /dev/null +++ b/local_filtering_test.js @@ -0,0 +1,58 @@ +// Devuelve genero en funcion del espectaculo +function daGenero(espectaculo){ + var ret = []; + switch(espectaculo){ + case '0': ret.push('0', '1', '2', '3'); + break; + case '1': ret.push('1', '3'); + break; + case '2': ret.push('1', '2'); + break; + } + + return ret; +} + +// devuelve funcion en funcion del genero y el espectacilo +function daFuncion(genero, espectaculo){ + var ret = []; + + if(espectaculo == '0'){ + switch (genero){ + case '0': ret.push('0', '1', '2', '3', '4'); + break; + case '1': ret.push('2', '4'); + break; + case '2': ret.push('1'); + break; + case '3': ret.push('3'); + break; + } + } else if(espectaculo == '1'){ + // obra de teatro + switch (genero){ + // case '0': ret.push('0', '1', '2', '3', '4'); + // break; + case '1': ret.push('4'); + break; + // case '2': ret.push('1'); + // break; + case '3': ret.push('3'); + break; + } + } else if(espectaculo == '2'){ + // pelicula + switch (genero){ + // case '0': ret.push('0', '1', '2', '3', '4'); + // break; + case '1': ret.push('2'); + break; + case '2': ret.push('1'); + break; + // case '3': ret.push('3'); + // break; + } + } + + return ret; +} \ No newline at end of file diff --git a/test_remote_script_chaining_mode.php b/test_remote_script_chaining_mode.php index b293486..ced18f7 100644 --- a/test_remote_script_chaining_mode.php +++ b/test_remote_script_chaining_mode.php @@ -25,9 +25,15 @@ function daGeneroChaining($espectaculo){ return $db; } - $espectaculo = $_POST['previousValues']['espectaculo']; - - $genero = daGeneroChaining($espectaculo); + + if(isset($_POST['previousValues'])){ + // $param = $_POST['get']; -> Me dice a que funcion llamar + $value = $_POST['previousValues']['espectaculo']; + } else { + $value = $_POST['espectaculo']; + } + + $genero = daGeneroChaining($value); echo json_encode($genero); diff --git a/test_remote_script_filtering_mode.php b/test_remote_script_filtering_mode.php index 921ecda..768da78 100644 --- a/test_remote_script_filtering_mode.php +++ b/test_remote_script_filtering_mode.php @@ -13,11 +13,57 @@ function daGeneroFiltering($espectaculo){ return $ret; } - - $espectaculo = $_POST['previousValues']['espectaculo']; - $genero = daGeneroFiltering($espectaculo); - - echo json_encode($genero); - - \ No newline at end of file + function getMes($dia){ + $ret = array(); + switch($dia){ + case '30': array_push($ret, '4', '6', '9', '11'); + break; + case '31': array_push($ret, '1', '3', '5', '7', '8', '10', '12'); + break; + // Todos los meses tienen al menos 28 dias... excepto febrero que puede no tenerlo (excp bisiesto) + default: + array_push($ret, '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'); + break; + } + + return $ret; + } + + function getAnio($dia, $mes){ + $devolver_anios_bisiestos = ($dia == '29' && $mes == '2')? true: false; + $anios = range(1990, 2015); + $ret = array(); + + if($devolver_anios_bisiestos){ + + foreach ($anios as $anio) { + if ((($anio % 4) == 0) && ( ($anio % 100 != 0) || (($anio % 400 != 0)) )){ + $ret[] = strval($anio); + } + } + } else { + $ret = array_map('strval', $anios); + } + + + return $ret; + } + + + + if(isset($_POST['verb']) && !empty($_POST['verb'])) { + $action = $_POST['element']; + switch($action) { + case 'mes' : $ret = getMes($_POST['previousValues']['dia']); break; + case 'anio' : $ret = getAnio($_POST['previousValues']['dia'], $_POST['previousValues']['mes']); break; + } + + echo json_encode($ret); + + } else { + $espectaculo = $_POST['previousValues']['espectaculo']; + $genero = daGeneroFiltering($espectaculo); + echo json_encode($genero); + } +