Namespace the JavaScript functions
authorstan <stan@735d13b6-9817-0410-8766-e36946ffe9aa>
Mon, 11 Feb 2013 20:26:44 +0000 (20:26 +0000)
committerstan <stan@735d13b6-9817-0410-8766-e36946ffe9aa>
Mon, 11 Feb 2013 20:26:44 +0000 (20:26 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/sr_freecap/trunk@71071 735d13b6-9817-0410-8766-e36946ffe9aa

ChangeLog
Classes/ViewHelpers/AudioViewHelper.php
Classes/ViewHelpers/ImageViewHelper.php
Resources/Public/JavaScript/freeCap.js

index 48d0c8e..63df0af 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2013-02-11  Stanislas Rolland  <typo3@sjbr.ca>
 
        * Update the manual
+       * Namespace the JavaScript functions
 
 2013-02-07  Stanislas Rolland  <typo3@sjbr.ca>
 
index 6fb4538..8c737e5 100644 (file)
@@ -88,13 +88,13 @@ class AudioViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelpe
                                $value = '<input type="image" alt="' . $translator->render('click_here_accessible') . '"'
                                        . ' title="' . $translator->render('click_here_accessible') . '"'
                                        . ' src="' . $siteURL . str_replace(PATH_site, '', \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($settings['accessibleOutputImage'])) . '"'
-                                       . ' onclick="playCaptcha(\'' . $fakeId . '\', \'' . $audioURL . '\', \'' . $translator->render('noPlayMessage') . '\');return false;" style="cursor: pointer;"'
+                                       . ' onclick="' . $this->extensionName . '.playCaptcha(\'' . $fakeId . '\', \'' . $audioURL . '\', \'' . $translator->render('noPlayMessage') . '\');return false;" style="cursor: pointer;"'
                                        . $this->getClassAttribute('image-accessible', $suffix) . ' />'
                                        . '<span' . $this->getClassAttribute('accessible') . ' id="tx_srfreecap_captcha_playAudio_' . $fakeId . '"></span>';
                        } else {
                                $value = '<span id="tx_srfreecap_captcha_playLink_' . $fakeId . '"'
                                        . $this->getClassAttribute('accessible-link', $suffix) . '>' . $translator->render('click_here_accessible_before_link') 
-                                       . '<a onClick="playCaptcha(\'' . $fakeId.'\', \'' . $audioURL . '\', \'' . $translator->render('noPlayMessage') . '\');" style="cursor: pointer;" title="' . $translator->render('click_here_accessible') . '">'
+                                       . '<a onClick="' . $this->extensionName . '.playCaptcha(\'' . $fakeId.'\', \'' . $audioURL . '\', \'' . $translator->render('noPlayMessage') . '\');" style="cursor: pointer;" title="' . $translator->render('click_here_accessible') . '">'
                                        . $translator->render('click_here_accessible_link') . '</a>'
                                        . $translator->render('click_here_accessible_after_link') . '</span>'
                                        . '<span ' . $this->getClassAttribute('accessible', $suffix) . ' id="tx_srfreecap_captcha_playAudio_'  . $fakeId . '"></span>';
index 962196c..3a49c7d 100644 (file)
@@ -103,7 +103,7 @@ class ImageViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelpe
                        . ' src="' . htmlspecialchars($imageUrl) . '"'
                        . ' alt="' . $translator->render('altText') . ' "/>'
                        . '<span' . $this->getClassAttribute('cant-read', $suffix) . '>' . $translator->render('cant_read1')
-                       . ' <a href="#" onclick="this.blur();newFreeCap(\'' . $fakeId . '\', \'' . $translator->render('noImageMessage').'\');return false;">'
+                       . ' <a href="#" onclick="this.blur();' . $this->extensionName . '.newImage(\'' . $fakeId . '\', \'' . $translator->render('noImageMessage').'\');return false;">'
                        . $translator->render('click_here') . '</a>'
                        . $translator->render('cant_read2') . '</span>';
                
index 85f6521..90e9853 100644 (file)
  * Javascript functions for TYPO3 extension freeCap CAPTCHA (sr_freecap)
  *
  */
-
-/*
- * Loads a new freeCap image
- *
- * @param      string          id: identifier used to uniiquely identify the image
- *
- * @return     void
- */
-function newFreeCap(id, noImageMessage) {
-       if (document.getElementById) {
-                       // extract image name from image source (i.e. cut off ?randomness)
-               var theImage = document.getElementById('tx_srfreecap_captcha_image_' + id);
-               var parts = theImage.src.split('&set');
-               theImage.src = parts[0] + '&set=' + Math.round(Math.random()*100000);
-       } else {
-               alert(noImageMessage ? noImageMessage : 'Sorry, we cannot autoreload a new image. Submit the form and a new image will be loaded.');
-       }
-}
-
-/*
- * Plays the audio captcha
- *
- * @param      string          id: identifier used to uniquely identify the wav file
- * @param      string          wavURL: url of the wave file generating script
- *
- * @return     void
- *
- * Note: In order for this to work with IE8, [SYS][cookieDomain] must be set using the TYPO3 Install Tool
- */
-function playCaptcha(id, wavURL, noPlayMessage) {
-       if (document.getElementById) {
-               var theAudio = document.getElementById('tx_srfreecap_captcha_playAudio_' + id);
-               var url = wavURL + '&nocache=' + Math.random();
-               while (theAudio.firstChild) {
-                       theAudio.removeChild(theAudio.firstChild);
-               }
-               var audioElement = document.createElement('audio');
-               if (audioElement.canPlayType && (audioElement.canPlayType('audio/x-wav') === 'maybe' || audioElement.canPlayType('audio/x-wav') === 'probably')) {
-                       audioElement.setAttribute('id', 'tx_srfreecap_captcha_playAudio_audio' + id);
-                       audioElement.setAttribute('autoplay', 'autoplay');
-                       var sourceElement = document.createElement('source');
-                       sourceElement.setAttribute('type', 'audio/x-wav');
-                       sourceElement.setAttribute('src', url);
-                       audioElement.appendChild(sourceElement);
-                       theAudio.appendChild(audioElement);
-               } else {
-                               // In IE, Windows Media Player should be the default player for audio WAVE
-                       var objectElement = document.createElement('object');
-                       objectElement.setAttribute('id', 'tx_srfreecap_captcha_playAudio_object' + id);
-                       objectElement.setAttribute('type', 'audio/x-wav');
-                       objectElement.setAttribute('data', url);
-                       objectElement.style.height = 0;
-                       objectElement.style.width = 0;
-                       try {
-                               objectElement.innerHTML = '<a href="' + url + '">' + (noPlayMessage ? noPlayMessage : 'Sorry, we cannot play the word of the image.') + '</a>';
-                       } catch (e) {
-                                       // IE8 does not allow any element other than param as child of object
-                               objectElement.setAttribute('altHTML', '<a href="' + url + '">' + (noPlayMessage ? noPlayMessage : 'Sorry, we cannot play the word of the image.') + '</a>');
+(function () {
+       SrFreecap = {
+               /*
+                * Loads a new freeCap image
+                *
+                * @param       string          id: identifier used to uniiquely identify the image
+                *
+                * @return      void
+                */
+               newImage: function (id, noImageMessage) {
+                       if (document.getElementById) {
+                                       // extract image name from image source (i.e. cut off ?randomness)
+                               var theImage = document.getElementById('tx_srfreecap_captcha_image_' + id);
+                               var parts = theImage.src.split('&set');
+                               theImage.src = parts[0] + '&set=' + Math.round(Math.random()*100000);
+                       } else {
+                               alert(noImageMessage ? noImageMessage : 'Sorry, we cannot autoreload a new image. Submit the form and a new image will be loaded.');
                        }
-                       theAudio.appendChild(objectElement);
-                       var parameters = {
-                               src: url,
-                               autoplay: true,
-                               autoStart: true,
-                               controller: false
-                       };
-                       for (var parameter in parameters) {
-                               if (parameters.hasOwnProperty(parameter)) {
-                                       var paramElement = document.createElement('param');
-                                       paramElement.setAttribute('value', parameters[parameter]);
-                                       paramElement.setAttribute('name', parameter);
-                                       paramElement = objectElement.appendChild(paramElement);
+               },
+               
+               /*
+                * Plays the audio captcha
+                *
+                * @param       string          id: identifier used to uniquely identify the wav file
+                * @param       string          wavURL: url of the wave file generating script
+                *
+                * @return      void
+                *
+                * Note: In order for this to work with IE8, [SYS][cookieDomain] must be set using the TYPO3 Install Tool
+                */
+               playCaptcha: function (id, wavURL, noPlayMessage) {
+                       if (document.getElementById) {
+                               var theAudio = document.getElementById('tx_srfreecap_captcha_playAudio_' + id);
+                               var url = wavURL + '&nocache=' + Math.random();
+                               while (theAudio.firstChild) {
+                                       theAudio.removeChild(theAudio.firstChild);
+                               }
+                               var audioElement = document.createElement('audio');
+                               if (audioElement.canPlayType && (audioElement.canPlayType('audio/x-wav') === 'maybe' || audioElement.canPlayType('audio/x-wav') === 'probably')) {
+                                       audioElement.setAttribute('id', 'tx_srfreecap_captcha_playAudio_audio' + id);
+                                       audioElement.setAttribute('autoplay', 'autoplay');
+                                       var sourceElement = document.createElement('source');
+                                       sourceElement.setAttribute('type', 'audio/x-wav');
+                                       sourceElement.setAttribute('src', url);
+                                       audioElement.appendChild(sourceElement);
+                                       theAudio.appendChild(audioElement);
+                               } else {
+                                               // In IE, Windows Media Player should be the default player for audio WAVE
+                                       var objectElement = document.createElement('object');
+                                       objectElement.setAttribute('id', 'tx_srfreecap_captcha_playAudio_object' + id);
+                                       objectElement.setAttribute('type', 'audio/x-wav');
+                                       objectElement.setAttribute('data', url);
+                                       objectElement.style.height = 0;
+                                       objectElement.style.width = 0;
+                                       try {
+                                               objectElement.innerHTML = '<a href="' + url + '">' + (noPlayMessage ? noPlayMessage : 'Sorry, we cannot play the word of the image.') + '</a>';
+                                       } catch (e) {
+                                                       // IE8 does not allow any element other than param as child of object
+                                               objectElement.setAttribute('altHTML', '<a href="' + url + '">' + (noPlayMessage ? noPlayMessage : 'Sorry, we cannot play the word of the image.') + '</a>');
+                                       }
+                                       theAudio.appendChild(objectElement);
+                                       var parameters = {
+                                               src: url,
+                                               autoplay: true,
+                                               autoStart: true,
+                                               controller: false
+                                       };
+                                       for (var parameter in parameters) {
+                                               if (parameters.hasOwnProperty(parameter)) {
+                                                       var paramElement = document.createElement('param');
+                                                       paramElement.setAttribute('value', parameters[parameter]);
+                                                       paramElement.setAttribute('name', parameter);
+                                                       paramElement = objectElement.appendChild(paramElement);
+                                               }
+                                       }
                                }
+                       } else {
+                               alert(noPlayMessage ? noPlayMessage : 'Sorry, we cannot play the word of the image.');
                        }
                }
-       } else {
-               alert(noPlayMessage ? noPlayMessage : 'Sorry, we cannot play the word of the image.');
        }
-}
+})();