* De-crappified JS upload dest check feature. Converted to sajax framework. Comprehen...
[lhc/web/wiklou.git] / skins / common / upload.js
1 function licenseSelectorCheck() {
2 var selector = document.getElementById("wpLicense");
3 if (selector.selectedIndex > 0 &&
4 selector.options[selector.selectedIndex].value == "" ) {
5 // Browser is broken, doesn't respect disabled attribute on <option>
6 selector.selectedIndex = 0;
7 }
8 }
9
10 function licenseSelectorFixup() {
11 // for MSIE/Mac; non-breaking spaces cause the <option> not to render
12 // but, for some reason, setting the text to itself works
13 var selector = document.getElementById("wpLicense");
14 if (selector) {
15 var ua = navigator.userAgent;
16 var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
17 if (isMacIe) {
18 for (var i = 0; i < selector.options.length; i++) {
19 selector.options[i].text = selector.options[i].text;
20 }
21 }
22 }
23 }
24
25 var wgUploadWarningObj = {
26 'responseCache' : { '' : '&nbsp;' },
27 'nameToCheck' : '',
28 'typing': false,
29 'delay': 500, // ms
30 'timeoutID': false,
31
32 'keypress': function () {
33 // Find file to upload
34 var destFile = document.getElementById('wpDestFile');
35 var warningElt = document.getElementById( 'wpDestFile-warning' );
36 if ( !destFile || !warningElt ) return ;
37
38 this.nameToCheck = destFile.value ;
39
40 // Clear timer
41 if ( this.timeoutID ) {
42 window.clearTimeout( this.timeoutID );
43 }
44 // Check response cache
45 if ( this.nameToCheck in this.responseCache ) {
46 this.setWarning(this.responseCache[this.nameToCheck]);
47 return;
48 }
49
50 this.setInnerHTML(warningElt, '..'); // TODO: pretty animated GIF
51 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
52 },
53
54 'checkNow': function (fname) {
55 if ( this.timeoutID ) {
56 window.clearTimeout( this.timeoutID );
57 }
58 this.nameToCheck = fname;
59 this.timeout();
60 },
61
62 'timeout' : function() {
63 var warningElt = document.getElementById( 'wpDestFile-warning' );
64 this.setInnerHTML(warningElt, '....'); // TODO: pretty animated GIF
65
66 // Get variables into local scope so that they will be preserved for the
67 // anonymous callback. fileName is copied so that multiple overlapping
68 // ajax requests can be supported.
69 var obj = this;
70 var fileName = this.nameToCheck;
71 sajax_do_call( 'UploadForm::ajaxGetExistsWarning', [this.nameToCheck],
72 function (result) {
73 obj.processResult(result, fileName)
74 }
75 );
76 },
77
78 'processResult' : function (result, fileName) {
79 this.setWarning(result.responseText);
80 this.responseCache[fileName] = result.responseText;
81 },
82
83 'setWarning' : function (warning) {
84 var warningElt = document.getElementById( 'wpDestFile-warning' );
85 var ackElt = document.getElementById( 'wpDestFileWarningAck' );
86 this.setInnerHTML(warningElt, warning);
87
88 // Set a value in the form indicating that the warning is acknowledged and
89 // doesn't need to be redisplayed post-upload
90 if ( warning == '' || warning == '&nbsp' ) {
91 ackElt.value = '';
92 } else {
93 ackElt.value = '1';
94 }
95 },
96
97 'setInnerHTML' : function (element, text) {
98 // Check for no change to avoid flicker in IE 7
99 if (element.innerHTML != text) {
100 element.innerHTML = text;
101 }
102 }
103 }
104
105 function fillDestFilename(id) {
106 if (!document.getElementById) {
107 return;
108 }
109 var path = document.getElementById(id).value;
110 // Find trailing part
111 var slash = path.lastIndexOf('/');
112 var backslash = path.lastIndexOf('\\');
113 var fname;
114 if (slash == -1 && backslash == -1) {
115 fname = path;
116 } else if (slash > backslash) {
117 fname = path.substring(slash+1, 10000);
118 } else {
119 fname = path.substring(backslash+1, 10000);
120 }
121
122 // Capitalise first letter and replace spaces by underscores
123 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
124
125 // Output result
126 var destFile = document.getElementById('wpDestFile');
127 if (destFile) {
128 destFile.value = fname;
129 if ( wgAjaxUploadDestCheck ) {
130 wgUploadWarningObj.checkNow(fname) ;
131 }
132 }
133 }
134
135 addOnloadHook(licenseSelectorFixup);