Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Constructor of our Preview object
  3.  * @param $field [HTMLObject]
  4.  * @constructor
  5.  */
  6. function Preview($field, App)
  7. {
  8.     this.$canvas = $field;
  9.     this.App = App;
  10. }
  11.  
  12. /**
  13.  * this will render our preview
  14.  */
  15. Preview.prototype.render = function()
  16. {
  17.     $("body").ConvertSVG();
  18. };
  19.  
  20. Preview.prototype.clearCanvas = function()
  21. {
  22.  
  23.     this.$canvas.html("");
  24. };
  25.  
  26. /**
  27.  * this will initialize our preview by injecting data into our canvas
  28.  */
  29. Preview.prototype.initialize = function()
  30. {
  31.     this.$wrapper = $("<div/>",{
  32.         class : 'svg preview-wrapper'
  33.     });
  34.  
  35.     this.$leftWall = $("<div/>",{
  36.         class : 'wall-left wall'
  37.     });
  38.  
  39.     this.$rightWall = $("<div/>",{
  40.         class : 'wall-right wall'
  41.     });
  42.  
  43.     this.$radiatorStart = $("<div/>",{
  44.         class : 'radiator-start radiator'
  45.     });
  46.  
  47.     this.$radiatorEnd = $("<div/>",{
  48.         class : 'radiator-end radiator'
  49.     });
  50.  
  51.     this.$radiatorCenter = $("<div/>",{
  52.         class : 'radiator-center-wrapper radiator repeatable'
  53.     });
  54.  
  55.     this.$conversionLeft = $("<div/>",{
  56.         class : 'radiator conversion conversion-left'
  57.     });
  58.  
  59.     this.$webbingRepeat = $("<div>",{
  60.         class: "radiator webbing radiator-repeat"
  61.     });
  62.  
  63.     this.$cornerRepeat = $("<div/>",{
  64.         class: "radiator-corner-repeat"
  65.     });
  66.  
  67.     this.injectImages();
  68.     this.calculateCenterBlocks();
  69.     this.spawnRadiator();
  70.     this.$wrapper.append(this.$leftWall, this.$radiatorStart, this.$radiatorCenter, this.$radiatorEnd, this.$rightWall);
  71.     this.$canvas.append(this.$wrapper);
  72.  
  73.     $("body").ConvertSVG();
  74. };
  75.  
  76. Preview.prototype.showCurWebbing = function ()
  77. {
  78.     // Clear radiator start html
  79.     this.$radiatorStart.html("");
  80.  
  81.     // Backup
  82.     this.setBackground(this.$radiatorStart, '/wp-content/plugins/product-customizer/img/module/conversion-left.svg').css({
  83.         'background-position': 'right',
  84.         'background-repeat':   'no-repeat'
  85.     });
  86.  
  87.     this.setBackground(this.$radiatorEnd, '/wp-content/plugins/product-customizer/img/module/conversion-right.svg').css({
  88.         'background-position'   :   'left',
  89.         'background-repeat'     :   'no-repeat'
  90.     });
  91.  
  92.     // Add class
  93.     this.$radiatorCenter.addClass("conversion-visible");
  94.  
  95.     // Check if we have already an webbing url
  96.     // Else we need to set the default webbing
  97.     if (!app.cart.currentRadiator.webbings.url)
  98.         var url = app.cart.currentRadiator.webbings.url;
  99.     else
  100.         var url = "/wp-content/plugins/product-customizer/img/module/webbing-00.svg";
  101.  
  102.     this.setBackground(this.$radiatorCenter, url).css({});
  103. };
  104.  
  105. Preview.prototype.showCurConversion = function ()
  106. {
  107.     // //check for existance
  108.     // if(app.preview.$radiatorCenter.html() != "")
  109.     // {
  110.     //     //console.log("bestaat al");
  111.     // }
  112.     // else
  113.     // {
  114.     // }
  115.  
  116.     this.$webbingWrapper = $("<div/>", {
  117.         class: 'webbing-wrapper repeat webbing-repeat radiator-repeat',
  118.     });
  119.  
  120.  
  121.     this.setBackground(this.$webbingWrapper, app.cart.currentRadiator.corners.url);
  122.     this.$radiatorCenter.html(this.$webbingWrapper);
  123.  
  124. };
  125.  
  126. Preview.prototype.setBackground = function ($object, url)
  127. {
  128.     // if ($object !== undefined)
  129.     return $object.css('background-image', 'url(' + url + ')');
  130. };
  131.  
  132. /**
  133.  * This changes the size of our radiator on the canvas Without animation, useful for when spawning first time
  134.  */
  135. Preview.prototype.spawnRadiator = function()
  136. {
  137.     this.$radiatorCenter.css('width', this.futureWidth);
  138. };
  139.  
  140. /**
  141.  * Injects images
  142.  */
  143. Preview.prototype.injectImages = function()
  144. {
  145.     this.$radiatorStart.html("<img class='svg' src='/wp-content/plugins/product-customizer/img/module/radiator-left.png'>");
  146.     this.$radiatorCenter.html("<img class='svg' src='/wp-content/plugins/product-customizer/img/module/radiator-repeat.png'>");
  147.     this.$radiatorEnd.html("<img class='svg' src='/wp-content/plugins/product-customizer/img/module/radiator-right.png'>");
  148. };
  149. /**
  150.  * Calculates and returns the right amount of center blocks for the radiator.
  151.  */
  152. Preview.prototype.calculateCenterBlocks = function()
  153. {
  154.     // Get the length of the radiator
  155.     var length = this.App.cart.currentRadiator.measures.inner.length;
  156.  
  157.     // Get amount of blocks to repeat, max set to 4.
  158.     this.blocks = Math.round(Math.min(Math.max(1, length / 100), 4));
  159.     this.futureWidth = 150 * this.blocks;
  160.     this.animateWidth();
  161. };
  162.  
  163.  
  164. Preview.prototype.animateWidth = function()
  165. {
  166.     this.setWallInterval();
  167.     this.$radiatorCenter.animate({width: this.futureWidth}, 200, $.proxy(this.stopWallInterval,this));
  168. };
  169.  
  170. Preview.prototype.setWallInterval = function()
  171. {
  172.     this.interval = setInterval(this.calculateWalls, 1);
  173. };
  174.  
  175. Preview.prototype.stopWallInterval = function()
  176. {
  177.     clearInterval(this.interval);
  178. };
  179.  
  180. /**
  181.  * this will refresh all data and measures.
  182.  */
  183. Preview.prototype.update = function()
  184. {
  185.  
  186. };
  187.  
  188. /**
  189.  * this will start animating towards our new situation.
  190.  * @param animationtype
  191.  */
  192. Preview.prototype.animate = function(animationType)
  193. {
  194.     return true || false;
  195. };
  196.  
  197. Preview.prototype.WallHack = function(id)
  198. {
  199.     switch(id)
  200.     {
  201.         case 'Muur aan de linker kant':
  202.             $(".wall").hide();
  203.             $(".wall-left").show();
  204.  
  205.             app.situation = "left";
  206.             break;
  207.         case 'Muur aan de rechter kant':
  208.             $(".wall").hide();
  209.             $(".wall-right").show();
  210.             app.situation = "right";
  211.  
  212.             break;
  213.         case 'Muur aan de beide kanten':
  214.             $(".wall").show();
  215.             app.situation = "both";
  216.             break;
  217.         case 'Geen muur':
  218.             $(".wall").hide();
  219.             app.situation = "none";
  220.             break;
  221.         default:
  222.             $(".wall").hide();
  223.             break;
  224.     }
  225. };
  226.  
  227. Preview.prototype.showConversion = function() {
  228.  
  229.     // return false;
  230.     this.$radiatorStart.html("<img class='svg conversion' src='/wp-content/plugins/product-customizer/img/module/conversion-left.svg'>");
  231.     this.$radiatorEnd.html("<img class='svg conversion' src='/wp-content/plugins/product-customizer/img/module/conversion-right.svg'>");
  232.     this.$radiatorCenter.html(this.injectConversion(this.blocks))
  233.     this.$radiatorStart.addClass('conversion-left');
  234.     this.$radiatorEnd.addClass('conversion-right');
  235.     this.$radiatorCenter.addClass('conversion-repeat');
  236.     $("body").ConvertSVG();
  237. };
  238.  
  239. Preview.prototype.injectConversion = function(I)
  240. {
  241.     // return false;
  242.     var string = "";
  243.     for(i = I; i!=0;i--)
  244.     {
  245.         string += "<img class='svg conversion' src='/wp-content/plugins/product-customizer/img/module/conversion-repeat.svg'>";
  246.     }
  247.     return string;
  248. };
  249.  
  250. Preview.prototype.injectWebbing = function(webbing)
  251. {
  252.     this.webbing = webbing;
  253.  
  254.     console.log("De webbing is:", webbing);
  255.  
  256.     var string = "";
  257.     sI = 0;
  258.  
  259.     webbingArray = [];
  260.  
  261.     for(i = this.blocks; i != 0; i--)
  262.     {
  263.         leftOffset = 9+(150*sI);
  264.         sI++;
  265.  
  266.         //console.log(webbing);
  267.         webbingArray[sI] = $("<img/>", {
  268.             class: 'svg webbing',
  269.             style: 'left : '+leftOffset+'px',
  270.             src:   '/wp-content/plugins/product-customizer/img/module/' + webbing + '.svg'
  271.         });
  272.  
  273.         //redundant
  274.         string += "<img class='svg webbing' style='left:" + leftOffset + "px' src='/wp-content/plugins/product-customizer/img/module/" + webbing + ".svg' >"
  275.     }
  276.  
  277.     $(".radiator-center-wrapper").ConvertSVG({
  278.         preload:        true,
  279.         preloadObjects: webbingArray,
  280.     });
  281. };
  282.  
  283. Preview.prototype.switchCorner = function(corner)
  284. {
  285.  
  286. };
  287.  
  288. Preview.prototype.calculateWalls = function()
  289. {
  290.     // Get width from elements
  291.     var previewRadiator = $(".preview-wrapper").width();
  292.     var previewBanner = $("#preview-canvas").width();
  293.  
  294.     var sum = (previewBanner - previewRadiator);
  295.     sum = (sum / 2 + 40);
  296.  
  297.     if (app.situation)
  298.     {
  299.         switch(app.situation)
  300.         {
  301.             case "left":
  302.                 $(".wall-left").css({width: sum});
  303.                 break;
  304.             case "right":
  305.                 $(".wall-right").css({width: sum});
  306.                 break;
  307.             case "both":
  308.                 $(".wall-left, .wall-right").css({width: sum});
  309.                 break;
  310.         }
  311.     }
  312. };
  313.  
  314. Preview.prototype.rebuildPreview = function()
  315. {
  316.     this.WallHack(app.cart.currentRadiator.situation.name);
  317.  
  318.     if(app.stepPointer > 2)
  319.     {
  320.         this.showCurWebbing();
  321.         this.showCurConversion();
  322.     }
  323. };
  324.  
  325. Preview.prototype.temp = function()
  326. {
  327.     this.clearCanvas();
  328.  
  329.     this.showConversion();
  330. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement