stuppid_bot

Untitled

Apr 17th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. this.addEventListener('load', function() {
  2.     var steps = document.getElementsByClassName('step'),
  3.         lastStep = steps.length - 1,
  4.         step = 0,
  5.         size = 360;
  6.    
  7.     function showStep() {
  8.         $('stepTitle').innerHTML = step == lastStep ? 'Завершение' : 'Шаг ' + (step + 1) + ': ' + ['Загрузка', 'Редактирование'][step];
  9.         $('prevStep').style.display = step ? 'inline' : 'none';
  10.         var i = lastStep;
  11.                
  12.         while (i >= 0) {
  13.             steps[i].style.display = step == i-- ? 'block' : 'none';
  14.         }
  15.     }
  16.    
  17.     function prevStep() {
  18.         if (step) {
  19.             --step;
  20.             showStep();
  21.         }
  22.     }
  23.    
  24.     function clean(str) {
  25.         return str.trim().replace(/\s+/g, ' ');
  26.     }
  27.    
  28.     function showPreview(data) {
  29.         var img = new Image;
  30.        
  31.         img.onload = function() {
  32.             var proportion = img.width / img.height;          
  33.             img.width = size * proportion;
  34.             var dx = img.width < size ? size - img.width : 0;
  35.             img.height = size;
  36.             // убираем ошибки
  37.             $('uploadError').innerHTML = '';
  38.             $('uploadForm').reset();
  39.             $('editForm').reset();
  40.             // рисуем дем
  41.             var canvas = $('preview');
  42.             canvas.width = img.width + 40 + dx;
  43.             canvas.height = img.height + 120;
  44.             var ctx = canvas.getContext('2d');
  45.             ctx.fillStyle = '#000';
  46.             ctx.fillRect(0, 0, canvas.width, canvas.height);
  47.             ctx.fillStyle = '#fff';
  48.             ctx.fillRect(15 + dx / 2, 15, canvas.width - 15 * 2 - dx, canvas.height - 15 * 2 - 40 * 2);
  49.             ctx.fillStyle = '#000';
  50.             ctx.fillRect(17 + dx / 2, 17, canvas.width - 17 * 2 - dx, canvas.height - 17 * 2 - 40 * 2);
  51.             ctx.drawImage(img, 20 + dx / 2, 20, img.width, img.height);
  52.  
  53.             $('imgTitle').onchange = $('imgDescr').onchange = function() {
  54.                 this.value = clean(this.value);
  55.             }
  56.  
  57.             $('imgTitle').onkeypress = $('imgTitle').onkeyup = function() {
  58.                 var fontSize = 32;
  59.                 ctx.fillStyle = '#000';
  60.                 ctx.fillRect(0, img.height + 30, canvas.width, 50);
  61.                 ctx.font = fontSize + 'px Tahoma, sans-serif';
  62.                 ctx.fillStyle = '#fff';
  63.                 ctx.textAlign = 'center';
  64.                 ctx.fillText(clean(this.value).toUpperCase(), canvas.width / 2, img.height + 30 + fontSize);
  65.             }
  66.  
  67.             $('imgDescr').onkeypress = $('imgDescr').onkeyup = function() {
  68.                 var fontSize = 24;
  69.                 ctx.fillStyle = '#000';
  70.                 ctx.fillRect(0, img.height + 80, canvas.width, canvas.height);
  71.                 ctx.font = fontSize + 'px Tahoma, sans-serif';              
  72.                 ctx.fillStyle = '#fff';
  73.                 ctx.textAlign = 'center';
  74.                 ctx.fillText(clean(this.value), canvas.width / 2, img.height + 80 + fontSize);
  75.             }
  76.  
  77.             // canvas.toDataURL();
  78.             step = 1;
  79.             showStep();
  80.         }
  81.        
  82.         img.src = data;
  83.     }
  84.    
  85.     $('prevStep').onclick = prevStep;
  86.     $('allowedImageTypes').innerHTML = app.allowedImageTypes.join(', ');
  87.     $('minImageSize').innerHTML = formatSize(app.minImageSize, 'si');
  88.     $('maxImageSize').innerHTML = formatSize(app.maxImageSize, 'si');
  89.    
  90.     $('imageURL').onchange = function() {
  91.         var req = new XMLHttpRequest;
  92.         req.open('POST', 'get_image.php');
  93.  
  94.         req.onreadystatechange = function() {
  95.             if (req.readyState == 4) {
  96.                 if (req.status == 200) {
  97.                     if (req.responseText.substr(0, 1) == 0) {
  98.                         return $('uploadError').innerHTML = req.responseText.substr(1);                        
  99.                     }
  100.  
  101.                     return showPreview(req.responseText);
  102.                 }
  103.  
  104.                 $('uploadError').innerHTML = req.status + ' ' + req.statusText;
  105.             }                
  106.         }
  107.  
  108.         req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  109.         req.send( 'u=' + encodeURIComponent(this.value) );
  110.     }
  111.    
  112.     $('imageUpload').onchange = function(event) {
  113.         var file = event.target.files[0], error;
  114.        
  115.         if ( !new RegExp('^image/(' + app.allowedImageTypes.join('|') + ')$').test(file.type) ) {
  116.             error = app.errors.invalidFileType;
  117.         }
  118.         else if (file.size < app.minImageSize) {
  119.             error = app.errors.imageTooSmall;
  120.         }
  121.         else if (file.size > app.maxImageSize) {
  122.             error = app.errors.imageTooBig;
  123.         }
  124.        
  125.         if (error) {
  126.             return $('uploadError').innerHTML = error;
  127.         }
  128.        
  129.         var reader = new FileReader;
  130.        
  131.         reader.onload = function(event) {
  132.             showPreview(event.target.result);
  133.         }
  134.        
  135.         reader.readAsDataURL(file);
  136.     }    
  137.    
  138.     $('copyright').innerHTML = app.copyright;
  139.     showStep();
  140. });
Advertisement
Add Comment
Please, Sign In to add comment