jslint
[Portfolio.git] / skins / cart_widgets.js
1 /*
2 * 2009 Benoit Pin - MINES ParisTech
3 * http://plinn.org
4 * Licence GPL
5 */
6
7
8 var CartWidget;
9
10 (function(){
11
12 CartWidget = function(slide, url) {
13 this.slide = slide;
14
15 var req = new XMLHttpRequest();
16 url = url + '?ajax=1';
17 req.open("GET", url, false);
18 showProgressImage();
19 req.send(null);
20 hideProgressImage();
21
22 if (req.status !== 200){
23 alert(req.status);
24 return;
25 }
26 var doc = req.responseXML.documentElement;
27 var wdgtNode = this.wdgtNode = getCopyOfNode(doc);
28 slide.appendChild(wdgtNode);
29
30 var descriptions = this.descriptions = [];
31 var divs = wdgtNode.getElementsByTagName('div');
32 var d, i;
33 for (i=0; i<divs.length; i++) {
34 d = divs[i];
35 if (d.className === 'ppt-description') {
36 descriptions[d.getAttribute('name')] = d;
37 }
38 }
39
40 var form = this.form = this.wdgtNode.getElementsByTagName('form')[0];
41 var itemSelector = this.itemSelector = form.elements[0];
42 var fm = this.fm = new FormManager(form);
43 var thisCart = this;
44 fm.onBeforeSubmit = function(fm, evt){return thisCart.onBeforeSubmit(fm, evt);};
45 fm.onResponseLoad = function(req){return thisCart.loadResponse(req);};
46
47 descriptions[itemSelector.value].style.visibility = 'visible';
48 descriptions[itemSelector.value].style.display='block';
49 this.selectedItem = itemSelector.value;
50
51 addListener(itemSelector, 'change', function(evt){thisCart.selectItem(evt);});
52 };
53
54 CartWidget.prototype.selectItem = function(evt) {
55 this.descriptions[this.selectedItem].style.visibility = 'hidden';
56 this.descriptions[this.selectedItem].style.display = 'none';
57 var name = this.itemSelector.value;
58
59 this.descriptions[name].style.visibility = 'visible';
60 this.descriptions[name].style.display='block';
61
62 this.selectedItem = name;
63 };
64
65 CartWidget.prototype.onBeforeSubmit = function(fm, evt) {
66 if (fm.submitButton.name === 'cancel') {
67 this.onCancel();
68 return 'cancelSubmit';
69 }
70 };
71
72 CartWidget.prototype.loadResponse = function(req) {
73 var doc = req.responseXML.documentElement;
74 switch(doc.nodeName) {
75 case 'confirm':
76 var slide = this.slide;
77 slide.removeChild(this.wdgtNode);
78
79 var text = doc.firstChild.nodeValue;
80 var confirm = document.createElement('div');
81 confirm.className = 'confirm-message';
82 confirm.innerHTML = text;
83 slide.appendChild(confirm);
84
85 var duration = parseInt(doc.getAttribute('duration'), 10) * 1000;
86 var thisCart = this;
87
88 setTimeout(function(){
89 slide.removeChild(confirm);
90 thisCart.onAfterConfirm();
91 }
92 ,duration
93 );
94
95 break;
96
97 case 'error' :
98 alert(doc.firstChild.nodeValue);
99 break;
100 }
101 };
102
103
104 CartWidget.prototype.onCancel = function() {
105 this.wdgtNode.parentNode.removeChild(this.wdgtNode);
106 };
107
108 CartWidget.prototype.onAfterConfirm = function(){};
109
110 }());