I’m moving my website to a new one and a personalized code stopped working and can’t figure out why.
The old page is working perfectly: https://www.waal.co/pages/grip-builder
but the new one shows up an error (Uncaught TypeError: $(…).hexGridWidget is not a function) on the second time the function rebuild is called (when you change the dropdowns).
Sorry for the mess in the code, I’m no developer 🙂 & thanks in advance.
https://i07hdzzlg4e8jppd-80712532305.shopifypreview.com/pages/grip-builder
Code bellow:
<html>
<h3>1. Enter your |surfboard| details</h3>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container">
<div>
<form class="cpanel" onsubmit="return false;">
<div class="boardetails">
<span class="control-label">Kind of board</span><br>
<select name="boards" id="boards">
<option value="short" selected>Shorboard</option>
<option value="retro">Retro</option>
<option value="minimal">Malibu</option>
<option value="long">Longboard</option>
</select>
</div>
<div class="boardetails" style="display: flex;">
<div class="controls" style="margin-right: 10px;">
<span class="control-label2">Lenght (ft):</span> <br>
<select name="col" id="columns" style="max-width: 70px;">
<option value="5">5</option>
<option value="6" selected>6</option>
<option value="7" >7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<div class="controls">
<span class="control-label2">& (inches):</span> <br>
<select name="col2" id="columns2" style="max-width: 70px;">
<option value="0">0</option>
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
</select>
</div>
</div>
<div class="boardetails">
<div class="controls">
<span class="control-label">Board width</span><br>
<select name="rows" id="rows">
<option value="17">17</option>
<option value="18">18</option>
<option value="19" selected>19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
</select>
</div>
</div>
<div class="boardetails2">
<span class="control-label">Tail pad</span><br>
<select name="tail" id="tail">
<option value="tail" selected>Yes</option>
<option value="no">No</option>
</select>
</div>
</form>
</div>
<h3>2. Highlight the |hexagons|</h3>
<div class="row">
<div style="text-align:center">
<div id="container_max">
<div id="container" class="fluid" align="left">
<img id="board_image" src="" class="fluid boardimage">
</div>
</div>
</div>
<div class="boardetails">
<div class="controls resultstext">
<span class="empty"></span>
<p style="margin-top:20px; color:#fff !important;">Click on board hexagons to select/deselect</p>
<h3 style="color:#fff !important; margin-right:20px !important;">Your setup has:
<span id="logger" style="color:#88fd42;">0</span><span class="logg"
style="color:#fff !important; margin-left:15px !important;">hexagons</span>
</h3><br>
<span id="note" style="color:#fff !important;"></span>
<div class="sqs-block button-block sqs-block-button" data-block-type="53"
id="block-yui_3_17_2_5_1484323594954_8494"
style="padding-left:0; padding-right:0; padding-top:0 !important; padding:0; margin-top:30px !important;">
<div class="sqs-block-content">
<div class="buttona" onclick="goshop2()" style="margin-top:30px;"><a
class="button">ORDER NOW</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
console.log("olalla");
// HEXGRID JS
/*global $, document*/
var linhas = 0;
var colunas = 0;
var firstTime = 1;
$.fn.hexGridWidget = function (id, radius, columns, rows, columnWidth, rowHeight, cssClass) {
'use strict';
let createSVG = function (tag) {
return $(document.createElementNS('http://www.w3.org/2000/svg', tag || 'svg'));
};
return $(this).each(function () {
let element = $(this);
const hexClick = function () {
let hex = $(this);
element.trigger($.Event('hexclick', hex.data()));
};
const height = radius * Math.sqrt(3) / 2;
const svgParent = createSVG('svg')
.attr('tabindex', 1)
.attr('id', id)
.appendTo(element)
.css({
position: 'absolute',
width: columnWidth * columns + columnWidth / 3,
height: rowHeight * rows
});
const oddKey = 1 - rows % 2;
for (let row = 0; row < rows; row++) {
linhas = linhas + 1;
for (let column = 0; column < columns; column++) {
if (colunas < column) {
colunas = colunas + 1;
}
if (column % 2 != oddKey && row + 1 == rows) continue;
let center = {
x: Math.round(column * columnWidth + columnWidth / 1.5),
y: Math.round((row + 0.5 * (1 + (column + oddKey) % 2)) * rowHeight)
};
let hexid = column+'-'+row;
createSVG('polygon').attr({
points: [
{x: -1 * radius / 2, y: -1 * height},
{x: radius / 2, y: -1 * height},
{x: radius, y: 0},
{x: radius / 2, y: height},
{x: -1 * radius / 2, y: height},
{x: -1 * radius, y: 0}
]
.map((it) => (it.x + center.x) + ',' + (it.y + center.y))
.join(' '),
'class':cssClass,
'id':hexid,
tabindex:1
})
.appendTo(svgParent).data({center:center, row:row, column:column}).on('click', hexClick).attr({'hex-row': row, 'hex-column': column});
}
}
});
};
// CALCULATOR
var hexFinal = 0;
function calculateHex() {
var result = document.getElementById('logger');
/*var radios = document.getElementsByName('group2');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
// do whatever you want with the checked radio
var tipoSurf = radios[i].value;
// only one radio can be logically checked, don't check the rest
break;
}
}
var myBox1 = document.getElementById('columns').value;
var myBox2 = document.getElementById('columns2').value;
var myBox3 = document.getElementById('rows').value;
var result = document.getElementById('logger');
var cp1 = parseFloat(myBox1/2);
var cp2 = parseFloat(myBox2/2)*0.08;
var lp = parseFloat(myBox3/2)*0.08;
switch(tipoSurf) {
case "1":
var cp = cp1+cp2;
var myResult = parseInt((3.14*cp*lp)*144);
break;
case "2":
var cp = (cp1+cp2)-0.5;
var myResult = parseInt((3.14*cp*lp)*144);
break;
case "3":
var cp = (cp1+cp2)-1.5;
var myResult = parseInt((3.14*cp*lp)*144);
break;
default:
var cp = cp1+cp2;
var myResult = parseInt((3.14*cp*lp)*144);
}
var hexTemp = myResult/40;
if (myBox1 >= 7){
hexFinal = 35 + hexTemp + 0.75*hexTemp;
} else {
hexFinal = 5 + hexTemp + 0.25*hexTemp;
}*/
//Center hexagon
var centerRow = Math.floor(linhas/2)-1;
var centerColumn = Math.floor(colunas/2)-1;
var firstrow = 0;
var umterco = Math.floor(colunas/3);
if (tailpad == 'no') {
var firstCol = 2;
} else {
var firstCol = Math.floor(colunas/3);
}
//ar firstCol = Math.floor(colunas/3);
var lastCol = Math.floor(colunas - umterco);
var linhaacesa = 1;
const pares = 1 - linhas % 2;
let helio = 0;
let r = 1;
let c = firstCol;
const hexA = [];
const hexB = [];
//for (let hx = 0; hx <= hexToBuy; hx++){
for (let rowA = 1; rowA < linhas-1; rowA++) {
for (let columnA = firstCol; columnA < lastCol; columnA++) {
if (columnA % 2 != pares && rowA + 1 == linhas-1) continue;
hexB.push(columnA+'-'+rowA);
}
}
/*if(c < lastCol) {
c += 1 ;
hexB.push(c+'-'+r);
console.log('hexagons>'+c+'-'+r);
} else {
c = firstCol;
r += 1 ;
}*/
//}
hexToBuy = hexB.length;
hexFinal = hexB.length;
result.innerHTML = parseInt(hexB.length);
for (let ah = 0; ah < hexB.length; ah++){
var hexId = hexB[ah];
var elemento = document.getElementById(hexId);
elemento.style.opacity = ".6";
let isMainPresent = elemento.classList.contains("clicked");
if (isMainPresent) {
//ja tinha
} else {
elemento.classList.toggle('clicked');
}
}
/*for (let i = 0; i < hexToBuy; i++) {
if (i > firstCol && i > lastCol) continue;
console.log(i+'-'+lastCol);
/*for (let r = 1; r < linhas-1; r++) {
var cagalhao = r;
for (let p = firstCol; p < lastCol+2; p++){
var putinha = p;
var hexId = p+'-'+cagalhao;
var elemento = document.getElementById(hexId);
console.log(lastCol)
elemento.style.opacity = ".6";
let isMainPresent = elemento.classList.contains("clicked");
if (isMainPresent) {
//ja tinha
} else {
elemento.classList.toggle('clicked');
}
}
}
}*/
}
function acendeHex(){
}
<!-- main initialisation -->
// Main constants
const padSize = 10.3; // Diameter, cm
const padGap = 1; // cm
const rowHeight = (padSize + padGap) * Math.sqrt(3) / 2; // cm
const columnWidth = (padSize + padGap) * 0.75; // cm
const padCssClass = 'hexfield';
let tailpad = 'yes';
// Formula que converte as polegadas da prancha em cm
// Formula que calcula quantos hexes cabiam na prancha, quer horizontal como vertical > isto dá o radios
// a prancha é o container ID
//entender a proporção de radius / pixels / cms
// radius is the length (in pixels) of the side of a single hexagon (effectively the radius of the circle in which the hex field will be inscribed.
// a border dos hexagonos também tem de ser variavel dos pixels / prancha
//logger diz o numero de hexagonos >>> DONE
function tipodePrancha(tipo, tail, w, h, m){
document.getElementById('board_image').setAttribute("style","margin-top: " + m + "px;");
switch (tipo + "|" + tail) {
case 'minimal|no':
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/malibu.png?v=1709121370");
tailpad = 'no';
break;
case 'minimal|tail':
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/malitail.png?v=1709121372");
tailpad = 'yes';
break;
case 'short|no':
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/short.png?v=1705075374");
tailpad = 'no';
break;
case 'short|tail':
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/shorttail.png?v=1705075374");
tailpad = 'yes';
break;
case 'retro|no':
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/retro.png?v=1705075374");
tailpad = 'no';
break;
case 'retro|tail':
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/retrotail.png?v=1705075375");
tailpad = 'yes';
break;
case 'long|no':
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/long.png?v=1705075374");
tailpad = 'no';
break;
case 'long|tail':
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/longtail.png?v=1705075374");
tailpad = 'yes';
break;
default:
document.getElementById('board_image').setAttribute("src","https://cdn.shopify.com/s/files/1/0807/1253/2305/files/short.png?v=1705075374");
}
document.getElementById('board_image').setAttribute("width",w + "px");
document.getElementById('board_image').setAttribute("height",h + "px");
console.log(w+"-----"+h);
// code block
}
let rebuild = function () {
linhas = 0;
colunas = 0;
const boardWidth = parseInt($('#rows').val()) * 2.54; // cm
const boardLength = parseInt($('#columns').val()) * 2.54 * 12 + parseInt($('#columns2').val()) * 2.54; // cm
const padsColumns = Math.ceil(boardLength / columnWidth); // count
let padsRows = Math.ceil(0.5 + boardWidth / rowHeight); // count
const viewWidth = padsColumns * columnWidth + columnWidth / 3 // cm
const viewHeight = padsRows * rowHeight // cm
const boardMarginTop = (viewHeight - boardWidth) / 2; // cm
var clientWidth = document.getElementById('container').clientWidth;
const pxScale = viewWidth/$('#container').width(); // ratio
const pxViewWidth = viewWidth / pxScale; // px
const pxViewHeight = viewHeight / pxScale; // px
console.log("--------"+pxViewHeight);
//$('#container').height(pxViewHeight);
if (pxViewHeight === 0) {
console.log("E ZERO");
const pxViewHeight = 195;
}
const pxBoardLength = boardLength / pxScale; // px
const pxBoardWidth = boardWidth / pxScale; // px
const pxBoardMarginTop = boardMarginTop / pxScale; // px
const pxRadius = padSize / (2 * pxScale); // px
const pxRowHeight = rowHeight / pxScale; // px
const pxColumnWidth = columnWidth / pxScale; // px
const tipo = $('#boards').find(":selected").val();
const tail = $('#tail').find(":selected").val();
tipodePrancha(tipo, tail, pxBoardLength, pxBoardWidth, pxBoardMarginTop);
//document.getElementById('hexMesh')?.remove();
$('#hexMesh').remove();
$('#logger').text(0);
$('#container').hexGridWidget("hexMesh", pxRadius, padsColumns, padsRows, pxColumnWidth, pxRowHeight, padCssClass)
.on('hexclick', function (e) {
//console.log("clic"+this);
//this.style.opacity = ".2";
//console.log('clicked [' + e.column + ',' + e.row + '] hex with center at [' + e.center.x + ',' + e.center.y + '] px');
});
let i=0;
let isDown = false; // Tracks status of mouse button
calculateHex();
/*$(document).mousedown(function() {
isDown = true; // When mouse goes down, set isDown to true
})
.mouseup(function() {
isDown = false; // When mouse goes up, set isDown to false
});
$('#container .hexfield').mouseover(function(){
if(isDown) { // Only change css if mouse is down
this.classList.toggle('clicked');
if (this.classList == 'hexfield clicked') {
i++;
} else {
i--;
}
$('#logger').text(i);
const divNote = document.getElementById('note');
if (i > 10 && i < 35) {
divNote.innerHTML = 'Please note: A standard shortboard pack will be less expensive. <a href="https://www.waal.co/products/shortboard-pack">Click here to buy</a>';
} else if(i > 65 && i < 100) {
divNote.innerHTML = 'Please note: A standard longboard pack will be less expensive. <a href="https://www.waal.co/products/longboard-surf-grip-pack">Click here to buy</a>';
} else {
divNote.innerHTML = "";
}
}
});*/
let totalH = Math.floor(hexFinal+i);
$('#container .hexfield').click(function () {
this.classList.toggle('clicked');
if (this.classList == 'hexfield clicked') {
i++;
this.style.opacity = ".6";
totalH++;
} else {
i--;
this.style.opacity = ".2";
totalH--;
//console.log(totalH);
}
$('#logger').text(Math.floor(hexFinal+i));
const divNote = document.getElementById('note');
if (totalH > 10 && totalH < 35) {
divNote.innerHTML = 'Please note: A standard shortboard pack will be less expensive. <span style="color:#fff;"><a href="https://www.waal.co/products/shortboard-pack">Click here to buy</a></span>';
} else if(totalH > 65 && totalH < 100) {
divNote.innerHTML = 'Please note: A standard longboard pack will be less expensive. <span style="color:#fff;"><a href="https://www.waal.co/products/longboard-surf-grip-pack">Click here to buy</a></span>';
} else {
divNote.innerHTML = "";
}
});
};
$( "#boards" ).change(rebuild);
$( "#rows" ).change(rebuild);
$( "#columns" ).change(rebuild);
$( "#columns2" ).change(rebuild);
$( "#tail" ).change(rebuild);
rebuild();
let goshop2 = function () {
let hexB = $('#logger').text();
//console.log(hexB);
window.location.href = '/cart/47876151935313:'+hexB+'?storefront=true';
}
</script>
</html>