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