Factorisation / bugfix.
[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 = new Array();
31 var divs = wdgtNode.getElementsByTagName('div');
32 var d;
33 for (var 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 with (descriptions[itemSelector.value].style) {
48 visibility = 'visible';
49 display='block';
50 }
51 this.selectedItem = itemSelector.value;
52
53 addListener(itemSelector, 'change', function(evt){thisCart.selectItem(evt)})
54 }
55
56 CartWidget.prototype.selectItem = function(evt) {
57 with(this.descriptions[this.selectedItem].style) {
58 visibility = 'hidden';
59 display = 'none'
60 }
61 var name = this.itemSelector.value;
62
63 with (this.descriptions[name].style) {
64 visibility = 'visible';
65 display='block';
66 }
67 this.selectedItem = name;
68 };
69
70 CartWidget.prototype.onBeforeSubmit = function(fm, evt) {
71 if (fm.submitButton.name == 'cancel') {
72 this.onCancel();
73 return 'cancelSubmit';
74 }
75 };
76
77 CartWidget.prototype.loadResponse = function(req) {
78 var doc = req.responseXML.documentElement;
79 switch(doc.nodeName) {
80 case 'confirm':
81 var slide = this.slide;
82 slide.removeChild(this.wdgtNode);
83
84 var text = doc.firstChild.nodeValue;
85 var confirm = document.createElement('div');
86 confirm.className = 'confirm-message';
87 confirm.innerHTML = text;
88 slide.appendChild(confirm);
89
90 var duration = parseInt(doc.getAttribute('duration')) * 1000;
91 var thisCart = this;
92
93 setTimeout(function(){
94 slide.removeChild(confirm);
95 thisCart.onAfterConfirm();
96 }
97 ,duration
98 );
99
100 break;
101
102 case 'error' :
103 alert(doc.firstChild.nodeValue);
104 break;
105 }
106 };
107
108
109 CartWidget.prototype.onCancel = function() {
110 this.wdgtNode.parentNode.removeChild(this.wdgtNode);
111 };
112
113 CartWidget.prototype.onAfterConfirm = function(){};
114
115 })();