stuppid_bot

Untitled

Apr 18th, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function runApp() {
  2.     var steps = document.getElementsByClassName('step'),
  3.         lastStep = steps.length - 1,
  4.         step = 0;
  5.    
  6.     function showStep() {
  7.         $('stepTitle').innerHTML = step == lastStep ? 'Завершение' : 'Шаг ' + (step + 1) + ': ' + ['Загрузка', 'Редактирование'][step];
  8.         $('prevStep').style.display = step ? 'inline' : 'none';
  9.         var i = lastStep;
  10.                
  11.         while (i >= 0) {
  12.             steps[i].style.display = step == i-- ? 'block' : 'none';
  13.         }
  14.     }
  15.    
  16.     function prevStep() {
  17.         if (step) {
  18.             --step;
  19.             showStep();
  20.         }
  21.     }
  22.    
  23.     function clean(str) {
  24.         return str.trim().replace(/\s+/g, ' ');
  25.     }
  26.    
  27.     function showPreview() {
  28.         if ($('uploadForm')['data-image']) {
  29.             var img = new Image,
  30.                 canvas = $('preview'),
  31.                 context = canvas.getContext('2d'),
  32.                 scale = 360,
  33.                 background = '#000',
  34.                 color = '#fff',
  35.                 font = 'Calibri, Arial, sans-serif',
  36.                 titleFontSize = 32,
  37.                 descrFontSize = 24,
  38.                 i1 = 20,
  39.                 i2 = 80,
  40.                 i3 = 15,
  41.                 i4 = 17,
  42.                 i5 = i1 * 1.5,
  43.                 i6 = 50,
  44.                 i7 = i5 + i6,
  45.                 i8 = 40;
  46.                
  47.             function drawBackground() {
  48.                 var dx = img.width < scale ? scale - img.width : 0;
  49.                 canvas.width = dx + i1 * 2 + img.width;
  50.                 canvas.height = i1 * 2 + i2 + img.height;
  51.                 context.fillStyle = background;
  52.                 context.fillRect(0, 0, canvas.width, canvas.height);
  53.                 context.fillStyle = color;
  54.                 context.fillRect(i3 + dx / 2, i3, img.width + (i1 - i3) * 2, img.height + (i1 - i3) * 2);
  55.                 context.fillStyle = background;
  56.                 context.fillRect(i4 + dx / 2, i4, img.width + (i1 - i4) * 2, img.height + (i1 - i4) * 2);
  57.                 context.drawImage(img, i1 + dx / 2, i1, img.width, img.height);
  58.             }
  59.            
  60.             function drawText(text, size, dy) {
  61.                 context.font = size + 'px ' + font;
  62.                 context.fillStyle = color;
  63.                 context.textAlign = 'center';
  64.                 context.fillText(clean(text), canvas.width / 2, dy);
  65.             }
  66.        
  67.             img.onload = function() {
  68.                 var proportion = img.width / img.height;
  69.                
  70.                 if (proportion < 0.51 || proportion > 1.94) {
  71.                     $('uploadForm')['data-image'] = '';
  72.                     return $('uploadError').innerHTML = 'Неправильные пропорции изображения';
  73.                 }
  74.                
  75.                 img.width = scale * proportion;
  76.                 img.height = scale;
  77.                 drawBackground();
  78.                 // canvas.toDataURL();
  79.                 step = 1;
  80.                 showStep();
  81.             }
  82.  
  83.             img.src = $('uploadForm')['data-image'];
  84.             $('uploadForm')['data-image'] = '';
  85.             $('uploadForm').reset();        
  86.             $('editForm').reset();
  87.            
  88.             $('imgTitle').onchange = $('imgDescr').onchange = function() {
  89.                 this.value = clean(this.value);
  90.             }
  91.  
  92.             $('imgTitle').onkeypress = $('imgTitle').onkeyup = function() {
  93.                 context.fillStyle = background;
  94.                 context.fillRect(0, img.height + i5, canvas.width, i6);
  95.                 drawText(this.value.toUpperCase(), titleFontSize, img.height + i5 + titleFontSize);
  96.             }
  97.  
  98.             $('imgDescr').onkeypress = $('imgDescr').onkeyup = function() {
  99.                 context.fillStyle = background;
  100.                 context.fillRect(0, img.height + i7, canvas.width, i8);
  101.                 drawText(this.value, descrFontSize, img.height + i7 + descrFontSize);
  102.             }
  103.            
  104.             $('backgroundColor').onchange = $('fontColor').onchange = function() {
  105.                 if (this.id == 'backgroundColor') {
  106.                     background = this.value;
  107.                 }
  108.                 else if (this.id == 'fontColor') {
  109.                     color = this.value;
  110.                 }
  111.                
  112.                 // перерисовываем все
  113.                 drawBackground();
  114.                 drawText($('imgTitle').value.toUpperCase(), titleFontSize, img.height + i5 + titleFontSize);
  115.                 drawText($('imgDescr').value, descrFontSize, img.height + i7 + descrFontSize);
  116.             }
  117.            
  118.             $('editForm').onsubmit = function() {
  119.                 // alert(1);  
  120.                 ajax.post('save_image.php', {data: canvas.toDataURL()}, {done: function(data) {
  121.                         //
  122.                 }})
  123.                
  124.                 return false;
  125.             }
  126.         }      
  127.     }
  128.    
  129.     $('prevStep').onclick = prevStep;
  130.     $('allowedImageTypes').innerHTML = app.allowedImageTypes.join(', ');
  131.     $('minImageSize').innerHTML = formatSize(app.minImageSize, 'si');
  132.     $('maxImageSize').innerHTML = formatSize(app.maxImageSize, 'si');
  133.    
  134.     $('imgURL').onchange = function() {
  135.         ajax.post('get_image.php', {url: this.value.trim()}, {
  136.             done: function(data) {
  137.                 if (data.substr(0, 5) == 'error') {
  138.                     $('uploadForm')['data-image'] = '';
  139.                     return $('uploadError').innerHTML = data.substr(6);                        
  140.                 }
  141.                
  142.                 $('uploadForm')['data-image'] = data;
  143.                 $('uploadError').innerHTML = '';
  144.             },
  145.            
  146.             fail: function(code, text) {
  147.                 $('uploadForm')['data-image'] = '';
  148.                 $('uploadError').innerHTML = code + ' ' + text;
  149.             }
  150.         });
  151.     }
  152.    
  153.     $('imgUpload').onchange = function(event) {
  154.         var file = event.target.files[0],
  155.             error;
  156.        
  157.         if ( !new RegExp('^image/(' + app.allowedImageTypes.join('|') + ')$').test(file.type) ) {
  158.             error = app.someErrors.invalidFileType;
  159.         }
  160.         else if (file.size < app.minImageSize) {
  161.             error = app.someErrors.imageTooSmall;
  162.         }
  163.         else if (file.size > app.maxImageSize) {
  164.             error = app.someErrors.imageTooBig;
  165.         }
  166.        
  167.         if (error) {
  168.             $('uploadForm')['data-image'] = '';
  169.             return $('uploadError').innerHTML = error;
  170.         }
  171.        
  172.         var reader = new FileReader;
  173.        
  174.         reader.onload = function(event) {
  175.             $('uploadForm')['data-image'] = event.target.result;
  176.             $('uploadError').innerHTML = '';
  177.         }
  178.        
  179.         reader.readAsDataURL(file);
  180.     }
  181.    
  182.     $('uploadForm').onsubmit = function() {
  183.         if (this['data-image']) {
  184.             showPreview();
  185.         }
  186.         else {
  187.             $('uploadError').innerHTML = 'Не выбрано изображение';
  188.         }
  189.                
  190.         return false;
  191.     }    
  192.    
  193.     $('copyright').innerHTML = app.copyright;
  194.     showStep();
  195. }
  196.  
  197. this.addEventListener('load', runApp);
Advertisement
Add Comment
Please, Sign In to add comment