Advertisement
stuppid_bot

Untitled

Apr 22nd, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.     var frames = document.getElementsByClassName('frame'),
  3.         frameId = 0;
  4.        
  5.     getFrame = function(id) {
  6.         id = id || 0;
  7.         gid('heading').innerHTML = frames[id].getAttribute('data-heading');
  8.         gid('back').style.display = id ? 'inline' : 'none';
  9.  
  10.         var i = frames.length;
  11.  
  12.         while (i--) {
  13.             frames[i].style.display = i == id ? 'block' : 'none';
  14.         }
  15.     }
  16.    
  17.     nextFrame = function() {
  18.         getFrame(++frameId);
  19.     };
  20.    
  21.     prevFrame = function() {
  22.         getFrame(--frameId);
  23.     }
  24. })();
  25.  
  26. function setUploadError(message) {
  27.     gid('uploadError').innerHTML = message ? message : '';
  28. }
  29.  
  30. function imagePreview(data) {
  31.     var scale = 600,
  32.         img = new Image;
  33.     img.src = data;
  34.    
  35.     img.onload = function() {
  36.         var proportion = this.width / this.height,
  37.             cnv = gid('preview'),
  38.             ctx = cnv.getContext('2d');
  39.         console.log('resizing');
  40.         console.log('before: width=' + this.width + ', height=' + this.height);
  41.        
  42.         if (this.width > scale) {
  43.             this.height = (this.width = scale) / proportion;
  44.         }      
  45.         else if (this.height > scale) {
  46.             this.width = (this.height = scale) * proportion;
  47.         }
  48.        
  49.         console.log('after: width=' + this.width + ', height=' + this.height);
  50.         cnv.width = this.width;
  51.         cnv.height = this.height;
  52.         ctx.drawImage(this, 0, 0, this.width, this.height);
  53.         nextFrame();
  54.     }
  55.    
  56.     gid('uploadForm').reset();
  57.     setUploadError();
  58. }
  59.  
  60. function createMem(type, img) {
  61.     var cnv = gid('mem'),
  62.         ctx = cnv.getContext('2d'),
  63.         // отступ от края
  64.         x1 = 40,
  65.         // толщина белой линии
  66.         x2 = 3,
  67.         // промежуток между линией и изображением
  68.         x3 = 2,
  69.         x4 = x1 + x2 + x3,
  70.         x5 = x4 * 2,
  71.         x6 = x2 * 2 +  x3 * 2,
  72.         x7 = x1 + x2,
  73.         x8 = x3 * 2;  
  74.     cnv.width = img.width;
  75.     cnv.height = img.height;
  76.     img.width -= x5;
  77.     img.height -= x5;
  78.     ctx.fillStyle = '#000000';
  79.     ctx.fillRect(0, 0, cnv.width, cnv.height);
  80.     ctx.fillStyle = '#ffffff';
  81.     ctx.fillRect(x1, x1, img.width + x6, img.height + x6);
  82.     ctx.fillStyle = '#000000';
  83.     ctx.fillRect(x7, x7, img.width + x8, img.height + x8);
  84.     ctx.drawImage(img, x4, x4, img.width, img.height);
  85.     nextFrame();
  86. }
  87.  
  88. this.addEventListener('load', function() {
  89.     gid('uploadForm').onsubmit = function() {
  90.         var u = ( gid('uploadUrl').value = gid('uploadUrl').value.trim() );
  91.        
  92.         if (u) {
  93.             var o = url.parse(u);
  94.            
  95.             if (!o) {
  96.                 setUploadError('Это не ссылка');
  97.             }        
  98.             else if (o.scheme && o.scheme != 'http') {
  99.                 setUploadError('Протокол ' + o.scheme + ' не поддерживается');
  100.             }
  101.             else if (!o.host) {
  102.                 setUploadError('Не указан хост');
  103.             }
  104.             else if (!o.path || o.path == '/') {
  105.                 setUploadError('Не указан путь до файла');
  106.             }
  107.             else {            
  108.                 ajax.post('get_image.php', {hostname: o.host, port: o.port || 80, uri: o.path + (o.query ? '?' + o.query : '')}, {
  109.                     done: function(response) {
  110.                         if (response.substr(0, 4) != 'data') {
  111.                             return setUploadError(response);
  112.                         }
  113.                        
  114.                         imagePreview(response);
  115.                     },
  116.                    
  117.                     fail: function() {
  118.                         this.reset()
  119.                         setUploadError('Сервис временно недоступен');
  120.                     }
  121.                 });
  122.             }
  123.         }
  124.        
  125.         return false;
  126.     };
  127.    
  128.     gid('uploadImage').onchange = function(event) {
  129.         var file = event.target.files[0];
  130.        
  131.         if ( !new RegExp('^image/(' + conf.allowedImageTypes.join('|') + ')$').test(file.type) ) {
  132.             return setUploadError('Неправильный тип файла');
  133.         }
  134.  
  135.         var reader = new FileReader;
  136.  
  137.         reader.onload = function(event) {
  138.             imagePreview(event.target.result);
  139.         };
  140.  
  141.         reader.readAsDataURL(file);
  142.     };
  143.    
  144.     gid('back').onclick = prevFrame;
  145.    
  146.     gid('previewForm').onsubmit = function() {
  147.         var img = new Image;
  148.         img.src = gid('preview').toDataURL();
  149.        
  150.         img.onload = function() {
  151.             createMem( gid('memType').options[ gid('memType').selectedIndex ].value, this );
  152.         };
  153.        
  154.         return false;
  155.     };
  156.    
  157.     gid('copyright').innerHTML = '&copy; All rights reserved <a href="/" target="_blank">' + location.host.toUpperCase() + '</a>, 2013.';  
  158.     VK.init(getFrame);
  159. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement