1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
| var artists = {
// tous les artistes
lastAddedArtist: null,
// le div conteneur
container: null,
// fonction d'init
init: function() {
var sbmButton = $("go");
var frm = $("monForm");
if(sbmButton && frm) {
sbmButton.observe("click", artists.postArtist);
}
},
// pst un artiste en ajax au script php. l'url est l'action du formulaire
postArtist: function(e) {
// on stop l'événement
e.stop();
// nouvelle requete ajax
new Ajax.Request($("monForm").action, {
method: "post",
parameters : $("monForm").serialize(true),
// on externalise le call back pour plus de lisibilité
onSuccess:artists.artist_callBack
});
},
// call back retour ajax
artist_callBack: function(response) {
// on evalue le JSON
var lastArtist = response.responseText.evalJSON();
// si le resultat est de type string, c'est un message d'erreur
if(Object.isString(lastArtist)) {
//on affiche une erreur
artists.writeError(lastArtist);
}
else {
// reinit du formulaire
$("monForm").reset();
// on ajoute l'artiste à la liste de ceux déjà créés
// note : le json retourné est de la forme : {arts:[{art_id: "id", art_name: "name"}]}
// il pourait etre plus simple : {art_id: "id", art_name: "name"}
// la ligne suivante serait alors simplement :
artists.lastAddedArtist = lastArtist.arts[0];
// on dessine les artistes
artists.drawArtists();
}
},
// ecriture d'un message d'erreur
writeError: function(message) {
// on obtient le conteneur et on l'update avec le message
artists.getContainer().update(message);
},
// dessine les artistes du tableau artists.allArtists
drawArtists : function() {
// on construit la table
var table = new Element('table');
// header
var tr0 = new Element('tr');
var th0 = new Element('th').update("ID");
var th1 = new Element('th').update("Artiste");
var th2 = new Element('th', {colspan: '2'}).update("Action");
tr0.insert(th0).insert(th1).insert(th2);
table.insert(tr0);
// ... toujours donner les noms les plus significatifs possibles, ca aide toujours 6 mois après
var artist = artists.lastAddedArtist;
var tr1 = new Element('tr');
var td1 = new Element('td').update(artist.art_id);
var td2 = new Element('td').update(artist.art_name);
var td3 = new Element('td');
var imgModif = new Element('img', {src: 'modifier.png', alt: 'modifier', title: 'modifier'});
var imgModifLink = new Element('a',{"class":'imgModif',id: 'modif', href:''});
imgModifLink.observe("click", modifArtist.drawArtModifier);
td3.insert(imgModifLink);
imgModifLink.insert(imgModif);
var td4 = new Element('td');
var imgSuppr = new Element('img', {src: 'supprimer.png', alt: 'supprimer', title: 'supprimer'});
var imgSupprLink = new Element('a',{"class":'imgSuppr', id: 'suppr', href:''});
imgSupprLink.observe("click", supprArtist.drawArtSupprimer);
td4.insert(imgSupprLink);
imgSupprLink.insert(imgSuppr);
tr1.insert(td1).insert(td2).insert(td3).insert(td4);
table.insert(tr1);
// on obtient le conteneur
var container = artists.getContainer();
// on enleve tout ce qu'il contient
container.childElements().invoke("remove");
// on update à vide et on insere la table
$('arts').innerHTML = "";
container.update("").insert(table);
},
// renvoie le conteneur
getContainer : function() {
// si'il est null
if(! artists.container) {
// on le crée en deux fois
var block = new Element("div", {"class": "block"});
$("Content").insert(block);
artists.container = new Element("div", {"id": "arts"});
block.insert(artists.container);
}
// on le renvoie
return artists.container;
}
};
var modifArtist = {
// artiste à modifier
art_modif: null,
// le div conteneur
container: null,
// on dessine l'artiste à modifier
drawArtModifier : function(e){
// on stop l'évènement
e.stop();
// on construit le formulaire
var form = new Element('form', {"action": "modifArtist.php", "name": "frm_modification", "id": "frm_modification", "enctype": "multipart/form-data"});
// on récupère la valeur de l'artiste
modifArtist.aEffectuer = artists.lastAddedArtist;
// on construit la table
var table = new Element('table');
var tr0 = new Element('tr');
var th0 = new Element('th').update("Modifier Artiste");
var td0 = new Element('td');
var txt = new Element('input', {type: "text", "name": "art_name"});
txt.value = modifArtist.aEffectuer.art_name;
td0.insert(txt);
tr0.insert(th0).insert(td0);
table.insert(tr0);
var tr1 = new Element('tr');
var th1 = new Element('th');
var td1 = new Element('td');
var sbt = new Element('input', {type: "submit", "value": "Modifier", "id": "do"});
sbt.observe("click", modifArtist.postArtistModif);
td1.insert(sbt);
var btn = new Element('input', {type: "button", "value": "Annuler"});
btn.observe("click", function(){
document.location.href="art_nat2.php";
});
td1.insert(btn);
tr1.insert(th1).insert(td1);
table.insert(tr1);
form.insert(table);
// on obtient le conteneur
var container = modifArtist.getContainer();
// on enleve tout ce qu'il contient
container.childElements().invoke("remove");
// on update à vide et on insere la table
$('arts').innerHTML = "";
container.update("").insert(table);
},
// renvoie le conteneur
getContainer : function() {
// si'il est null
if(! modifArtist.container) {
// on le crée en deux fois
var block = new Element("div", {"class": "blockmodif"});
$("Content").insert(block);
modifArtist.container = new Element("div", {"id": "arts"});
block.insert(modifArtist.container);
}
// on le renvoie
return modifArtist.container;
}
// l'artiste à modifier
artAModifier: null,
// pst un artiste en ajax au script php. l'url est l'action du formulaire
postArtistModif: function(e){
// on stop l'événement
e.stop();
// fonction d'init
init: function(){
var sbmModifButton = $("do");
var frmModif = $("frm_modification");
if(sbmModifButton && frmModif) {
sbmModifButton.observe("click", modifArtist.postArtistModif);
}
},
// poste un artiste en ajax au script php. L'url est l'action du formulaire
postArtistModif: function(e) {
// on stoppe l'évènement
e.stop();
// nouvelle requête ajax
new Ajax.Request($("frm_modification").action, {
method: "post",
parameters: $("frm_modification").serialize(true),
// on externalise le call back pour plus de lisibilité
onSuccess: modifArtist.artModif_callBack
});
},
// call back retour ajax
artModif_callBack: function(response) {
// on evalue le JSON
var artModif = response.responseText.evalJSON();
// si le résultat est de type de string, c'est un message d'erreur
if(Object.isString(artModif)) {
// on affiche une erreur
modifArtist.writeError(artModif);
}
else {
// reinit du formulaire
$("frm_modification").reset();
//on récupère l'artiste à modifier
modifArtist.artAModifier = artists.lastArtist.arts[0];
// on dessine l'artiste à modifier
modifArtist.drawArtistModif();
}
},
// ecriture d'un message d'erreur
writeError: function(message) {
// on obtient le conteneur et on l'update avec le message
modifArtist.getContainer().update(message);
},
// On dessine le tableau de l'artiste à modifier
drawArtistModif: function() {
// on construit la table
var table = new Element('table');
// header
var tr0 = new Element('tr');
var th0 = new Element('th').update("ID");
var th1 = new Element('th').update("Artiste");
var th2 = new Element('th', {colspan:'2'}).update("Action");
tr0.insert(th0).insert(th1).insert(th2);
table.insert(tr0);
var artModification = modifArtist.artAModifier;
var tr1 = new Element('tr');
var td1 = new Element('td').update(artModification.art_id);
var td2 = new Element('td').update(artModification.art_name);
var td3 = new Element('td');
var imgModif = new Element('img', {src: 'modifier.png', alt: 'modifier', title: 'modifier'});
var imgModifLink = new Element('a',{"class":'imgModif',id: 'modif', href:''});
imgModifLink.observe("click", modifArtist.drawArtModifier);
td3.insert(imgModifLink);
imgModifLink.insert(imgModif);
var td4 = new Element('td');
var imgSuppr = new Element('img', {src: 'supprimer.png', alt: 'supprimer', title: 'supprimer'});
var imgSupprLink = new Element('a',{"class":'imgSuppr', id: 'suppr', href:''});
imgSupprLink.observe("click", supprArtist.drawArtSupprimer);
td4.insert(imgSupprLink);
imgSupprLink.insert(imgSuppr);
tr1.insert(td1).insert(td2).insert(td3).insert(td4);
table.insert(tr1);
// on obtient le conteneur
var container = modifArtist.getContainer();
// on enleve tout ce qu'il contient
container.childElements().invoke("remove");
// on update à vide et on insère la table
$('arts').innerHTML = "";
container.update("").insert(table);
},
// renvoie le conteneur
getContainer: function() {
// s'il est null
if(! modifArtist.container) {
// on le créé en deux fois
var block = new Element("div", {"class": "blockmodif"});
$("Content").insert(block);
modifArtist.container = new Element("div", {"id": "arts"});
block.insert(modifArtist.container);
}
// on le renvoie
return modifArtist.container;
}
};
var supprArtist = {
// le div conteneur
container: null,
url: null,
// fonction d'init
init: function(){
supprArtist.url = "supprArtist.php";
},
// on dessine l'artiste à supprimer
drawArtSupprimer : function(e){
// on stop l'évènement
e.stop();
// on récupère la valeur de l'artiste à modifier
supprArtist.aEffectuer = artists.lastAddedArtist;
// on construit le span de modification
message = "Vous êtes sur le point de supprimer: ";
var spn = new Element('span',{"style":"font-weight: bold"}).update(message + supprArtist.aEffectuer.art_name);
// on construit le formulaire de suppression
var frm_suppr = new Element('form', {"action": supprArtist.url, "name": "monForm", "id": "monForm", "enctype": "multipart/form-data"});
var sbt_suppr = new Element('input', {type: "submit", "value": "Supprimer", "id": "go"});
sbt_suppr.observe("click", function(){
document.location.href="supprArtist.php";
});
frm_suppr.insert(sbt_suppr);
var btn_suppr = new Element('input', {type: "button", "value": "Annuler"});
btn_suppr.observe("click", function(){
document.location.href="art_nat2.php";
});
frm_suppr.insert(btn_suppr);
// on obtient le conteneur
var container = supprArtist.getContainer();
// on enleve tout ce qu'il contient
container.childElements().invoke("remove");
// on update à vide et on insere la table
$('arts').innerHTML = "";
container.update("").insert(spn).insert(frm_suppr);
},
// renvoie le conteneur
getContainer : function() {
// si'il est null
if(! modifArtist.container) {
// on le crée en deux fois
var block_suppr = new Element("div", {"class": "blocksuppr"});
$("Content").insert(block_suppr);
supprArtist.container = new Element("div", {"id": "arts"});
block_suppr.insert(supprArtist.container);
}
// on le renvoie
return supprArtist.container;
}
};
document.observe('dom:loaded', function() {
artists.init();
modifArtist.init();
supprArtist.init();
}); |