Guest User

doogma.js

a guest
Feb 18th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.65 KB | None | 0 0
  1. /**************Doogma JS*******************/
  2. (function() {
  3. // Developer Instructions:
  4. // To Execute this script in Test environment - Add query paramater "ddtest" in Page's URL. Test script is "doogma-test.js"
  5. // To Prevent browser caching - Add query parameter "nocache=somevalue"
  6.  
  7. // If saving this file as "doogma-test.js" then set isTestScript = true, otherwise false
  8. var isTestScript = true;
  9. var productionScript = document.querySelector('script[src*="doogma.js"]');
  10.  
  11. // Make Script Singleton
  12. if(isTestScript) {
  13. var currentScript = document.querySelector('script[src*="doogma-test.js"]'); // Note: Do not use "document.currentScript" as it is not supported in IE.
  14. if(!currentScript) { // This is live script. (var isTestScript is assigned "true" by mistake, correct it in source code.)
  15. isTestScript = false;
  16. }
  17. }
  18. if(!isTestScript) {
  19. var currentScript = productionScript;
  20. if(currentScript.hasAttribute('data-dd-executed')) {
  21. return;
  22. }
  23. currentScript.setAttribute('data-dd-executed','');
  24. }
  25. // END: Make Script Singleton
  26.  
  27. var docUrl = docurl();
  28.  
  29. // Load Test version of script if query parameter contains "ddtest" and current script is not test version.
  30. if(docUrl.hasSearch('ddtest') && !isTestScript) {
  31. var testSrc = currentScript.src.replace('.js','-test.js');
  32. var noCache = docUrl.getSearch('nocache');
  33. if(noCache) {
  34. testSrc = docurl(testSrc).setSearch('nocache',noCache).href;
  35. }
  36. var testScript = document.createElement('script');
  37. testScript.setAttribute('src', testSrc);
  38. document.head.appendChild(testScript);
  39. return;
  40. }
  41. // END: Load Test version of script
  42.  
  43. // Check if Script should be disabled
  44. if(docUrl.hasSearch('nodoogma') || docUrl.hasSearch('nodoogmanav')) {
  45. return;
  46. }
  47. // END: Check if Script should be disabled
  48.  
  49. docUrl = null;
  50.  
  51. // Load jQuery in noConflict mode
  52. var dg$;
  53. var serverURL = 'https://bc.doogma.com/';
  54. var siteURL = window.location.href;
  55. var script = document.createElement('script');
  56. script.setAttribute('src', '//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js');
  57. script.addEventListener('load', function() {
  58. dg$ = $.noConflict(true);
  59. mainScript(dg$);
  60. });
  61. document.head.appendChild(script);
  62.  
  63.  
  64. // Main Script starts here
  65.  
  66. function mainScript($) {
  67. var storehash = 'storeHash=' + docurl(currentScript.src).getSearch('storeHash');
  68. var isUIReady = false;
  69. var uiModificationComplete = true;
  70. var designerDiv;
  71.  
  72. function fetchOptions(productID,storehash) {
  73. $.ajax({
  74. url: serverURL+'frontJs/fetchOptions.php'+'?productID='+productID+'&'+storehash,
  75. crossDomain: true,
  76. type: 'GET',
  77. dataType: 'json',
  78. processData: false,
  79. header: {
  80. "Access-Control-Allow-Origin": "*",
  81. },
  82. success: function(response) {
  83. if ($.trim(response)){
  84. if($('form .form-field[data-product-attribute]').length) {
  85. $('form .form-field[data-product-attribute]').each(function(index) {
  86. var _this = $(this);
  87. var name = $.trim($('label.form-label',this).text().split(':')[0]);
  88. name = $.trim(name.split('Required')[0]);
  89. name = $.trim(name.replace(/[^a-zA-Z 0-9 _ -]+/g, ""));
  90. $.each(response, function(index, value){
  91. value.optionName = $.trim(value.optionName.replace(':',''));
  92. if(name == value.optionName && value.doogmaClass) {
  93. if((_this).data('product-attribute') == 'input-text' || (_this).data('product-attribute') == 'input-number' || (_this).data('product-attribute') == 'input-checkbox' || (_this).data('product-attribute') == 'input-file') {
  94. (_this).find('input').addClass('doogma-'+value.doogmaClass)
  95. } else if((_this).data('product-attribute') == 'textarea' ) {
  96. (_this).find('textarea').addClass('doogma-'+value.doogmaClass)
  97. }
  98. }
  99. if(name == value.optionName && value.hidefieldClass == 'yes') {
  100. // Note: It is important to set type="hidden" as this is used as
  101. // a selector by some client's custom scripts to show/hid options.
  102. _this.hide().find('input').attr('type', 'hidden');
  103. }
  104. });
  105. });
  106. }
  107. if($('form .productAttributeRow').length) {
  108. $('form .productAttributeRow').each(function(index) {
  109. var _this = $(this);
  110. var name = $.trim($('.productAttributeLabel label span.name',this).text().split(':')[0]);
  111. $.each(response, function(index, value){
  112. if(name == value.optionName && value.doogmaClass) {
  113. if((_this).find('.productAttributeValue input').length) {
  114. (_this).find('input').addClass('doogma-'+value.doogmaClass);
  115. } else if((_this).find('.productAttributeValue textarea').length) {
  116. (_this).find('textarea').addClass('doogma-'+value.doogmaClass);
  117. }
  118. }
  119. if(name == value.optionName && value.hidefieldClass == 'yes') {
  120. _this.hide().find('input').attr('type', 'hidden');
  121. }
  122. });
  123. });
  124. }
  125. }
  126. }
  127. });
  128. }
  129.  
  130. function fetchOptionValues(productID,storehash) {
  131. var options = $('form .form-field[data-product-attribute]');
  132. //var pending = options.length;
  133. options.each(function(index1) {
  134. var _this = $(this);
  135. var optionName = $.trim($('label.form-label',this).text().split(':')[0]);
  136. optionName = $.trim(optionName.split('Required')[0]);
  137. $.ajax({
  138. url: serverURL+'frontJs/fetchOptionValues.php'+'?optionName='+optionName+'&'+storehash,
  139. crossDomain: true,
  140. type: 'GET',
  141. aysnc: false,
  142. dataType: 'json',
  143. processData: false,
  144. header: {
  145. "Access-Control-Allow-Origin": "*",
  146. },
  147. success: function(response) {
  148. if ($.trim(response)){
  149. $.each(response, function(index, value){
  150. if((_this).data('product-attribute') == 'swatch' || (_this).data('product-attribute') == 'set-rectangle' || (_this).data('product-attribute') == 'set-radio' || (_this).data('product-attribute') == 'product-list') {
  151. _this.find('input').each(function() {
  152. var _inthis = $(this);
  153. if(value.parentClass != '') {
  154. if(_inthis.val() == value.optionValueID) {
  155. _inthis.addClass('doogma-'+value.parentClass);
  156. if(value.parenthidefieldClass == 'yes') {
  157. _this.hide().find('input').attr('type', 'hidden');
  158. }
  159. _inthis.attr('data-doogma-value',value.doogmaClass);
  160. if(value.defaultValue == 'yes'){
  161. _inthis.attr('checked','checked');
  162. } else {
  163. _inthis.removeAttr('checked');
  164. }
  165. if(value.hidefieldClass == 'yes') {
  166. _inthis.hide().find('input').attr('type', 'hidden');
  167. }
  168. }
  169. }
  170. });
  171. } else if((_this).data('product-attribute') == 'set-select') {
  172. _this.find('select option').each(function() {
  173. var _inthis = $(this);
  174. if(value.parentClass != '') {
  175. if(_inthis.val() == value.optionValueID) {
  176. _inthis.parent('select').addClass('doogma-'+value.parentClass);
  177. if(value.parenthidefieldClass == 'yes') {
  178. _this.hide().find('input').attr('type', 'hidden');
  179. }
  180. _inthis.attr('data-doogma-value',value.doogmaClass);
  181. if(value.defaultValue == 'yes'){
  182. _inthis.parent('select').val(value.optionValueID).trigger('change');
  183. } else {
  184. _inthis.removeAttr('selected');
  185. }
  186. if(value.hidefieldClass == 'yes') {
  187. _inthis.hide().find('input').attr('type', 'hidden');
  188. }
  189. }
  190. }
  191. });
  192. }
  193. });
  194. }
  195. }
  196. });
  197. });
  198.  
  199. options = $('form .productAttributeRow');
  200. //pending = options.length;
  201. options.each(function(index1) {
  202. var _this = $(this);
  203. var optionName = $.trim($('.productAttributeLabel label span.name',this).text().split(':')[0]);
  204. $.ajax({
  205. url: serverURL+'frontJs/fetchOptionValues.php'+'?optionName='+optionName+'&'+storehash,
  206. crossDomain: true,
  207. type: 'GET',
  208. dataType: 'json',
  209. processData: false,
  210. header: {
  211. "Access-Control-Allow-Origin": "*",
  212. },
  213. success: function(response) {
  214. if ($.trim(response)){
  215. $.each(response, function(index, value){
  216. if((_this).find('.productAttributeValue ul').length) {
  217. _this.find('.productAttributeValue ul li').each(function() {
  218. var _lithis = $(this);
  219. var _inthis = $('input',this);
  220. if(value.parentClass != '') {
  221. if(_inthis.val() == value.optionValueID) {
  222. _inthis.addClass('doogma-'+value.parentClass);
  223. if(value.parenthidefieldClass == 'yes') {
  224. _this.hide().find('input').attr('type', 'hidden');
  225. }
  226. _inthis.attr('data-doogma-value',value.doogmaClass);
  227. if(value.defaultValue == 'yes'){
  228. _inthis.attr('checked','checked');
  229. _lithis.addClass('selectedValue');
  230. } else {
  231. _inthis.removeAttr('checked');
  232. }
  233. if(value.hidefieldClass == 'yes') {
  234. _inthis.hide().find('input').attr('type', 'hidden');
  235. }
  236. }
  237. }
  238. });
  239. } else if((_this).find('.productAttributeValue select').length) {
  240. _this.find('.productAttributeValue select option').each(function() {
  241. var _inthis = $(this);
  242. if(value.parentClass != '') {
  243. if(_inthis.val() == value.optionValueID) {
  244. _inthis.parent('select').addClass('doogma-'+value.parentClass);
  245. if(value.parenthidefieldClass == 'yes') {
  246. _this.hide().find('input').attr('type', 'hidden');
  247. }
  248. _inthis.attr('data-doogma-value',value.doogmaClass);
  249. if(value.defaultValue == 'yes'){
  250. _inthis.parent('select').val(value.optionValueID).trigger('change');
  251. } else {
  252. _inthis.removeAttr('selected');
  253. }
  254. if(value.hidefieldClass == 'yes') {
  255. _inthis.hide().find('input').attr('type', 'hidden');
  256. }
  257. }
  258. }
  259. });
  260. }
  261. });
  262. }
  263. }
  264. });
  265. });
  266. }
  267.  
  268. function fetchNewFeatureSetings(storehash) {
  269. $.ajax({
  270. url: serverURL+'frontJs/fetchSettings.php?'+storehash,
  271. crossDomain: true,
  272. type: 'GET',
  273. dataType: 'json',
  274. processData: false,
  275. header: {
  276. "Access-Control-Allow-Origin": "*",
  277. },
  278. success: function(response) {
  279. if ($.trim(response)) {
  280. var getData = response;
  281. if(getData.mobileImageFloat == 'yes') {
  282. var script = '<script type="text/javascript" src="//bc.doogma.com/js/mybigcommerce-doogma-test-store-float-designer.js"></script>';
  283. $('body').append(script);
  284. }
  285. }
  286. }
  287. });
  288. }
  289.  
  290. function IsThumbs() {
  291. $('body').on('click', '.productThumbs a', function(){
  292. var selVal = $(this).data('fullimagelink');
  293. $('select.doogma-alt-image1').val(selVal);
  294. if(selVal == '#customizeImage') {
  295. $('.doogma-customize-now-selected').prop('checked', true);
  296. } else {
  297. $('.doogma-customize-now-selected').prop('checked', false);
  298. }
  299. });
  300. }
  301.  
  302. function fetchProducts(productID,storehash) {
  303. $.ajax({
  304. url: serverURL+'frontJs/fetchProducts.php'+'?productID='+productID+'&'+storehash,
  305. crossDomain: true,
  306. type: 'GET',
  307. dataType: 'json',
  308. processData: false,
  309. header: {
  310. "Access-Control-Allow-Origin": "*",
  311. },
  312. success: function(response) {
  313. if ($.trim(response)){
  314. var getData = response;
  315. var designerParentDiv = '';
  316. function designerParentFun(designerParentDiv) {
  317. addDesignerElement({
  318. parentDiv: designerParentDiv,
  319. uid: getData.doogmaCode
  320. });
  321. fetchNewFeatureSetings(storehash);
  322. if(getData.showProductThumbs && getData.showProductThumbs == 'yes' && getData.image_count > 1) {
  323. if(getData.customizeImage != '') {
  324. var imgURL = getData.customizeImage;
  325. } else {
  326. var imgURL = serverURL+'upload/customize.png';
  327. }
  328.  
  329. $thumbImagesHtml = '<ul class="productThumbs">';
  330. $thumbImagesHtml +='<li><a href="javascript:void(0);" class="customizeImage" data-fullImagelink="#customizeImage" ><img src="'+imgURL+'" alt="customizeImage" width="38" /></a></li>';
  331. $.each(getData.AllImages,function(index,value){
  332. $thumbImagesHtml +='<li><a href="javascript:void(0);" data-fullImagelink="'+getData.FullsizeImages[index]+'"><img src="'+value+'" alt="thumbImages'+index+'" /></a></li>';
  333. });
  334. $thumbImagesHtml += '</ul>';
  335.  
  336. $thumbImagesHtml += '<select style="display:none;" class="doogma-alt-image1">';
  337. $thumbImagesHtml +='<option value="#customizeImage" data-doogma-value="doogma-customize-now-selected" selected>customizeImage</option>';
  338. $.each(getData.FullsizeImages,function(index,value) {
  339. var selImageDoogma = value.replace('https:','');
  340. $thumbImagesHtml +='<option value="'+value+'" data-doogma-value="'+selImageDoogma+'"> Image'+index+' </option>';
  341. });
  342. $thumbImagesHtml += '</select>';
  343. $thumbImagesHtml += '<input type="checkbox" name="doogma-customize-now-selected" class="doogma-customize-now-selected" value="true" style="display:none;" checked>';
  344.  
  345. $thumbImagesHtml += '<style> .productThumbs { display: block; margin: 10px; padding: 0; text-align: left; } .productThumbs li { display: inline-block; margin: 10px; } </style>';
  346.  
  347. $('.VisualizationContainer').append($thumbImagesHtml);
  348.  
  349. IsThumbs();
  350. } else if(getData.showProductThumbs && getData.showProductThumbs == 'yes') {
  351. $thumbImagesHtml = '<input type="checkbox" name="doogma-customize-now-selected" class="doogma-customize-now-selected" value="true" style="display:none;" checked>';
  352. $('.VisualizationContainer').append($thumbImagesHtml);
  353. }
  354. }
  355. if(getData.addDoogma == 'yes') {
  356. var imgName = getData.image_file;
  357. $('body img[src*="'+imgName+'"]').each(function(index) {
  358. if($(this).attr('src').indexOf(imgName) > -1) {
  359. if($(this).parents('[class*=product][class*=images]').length) {
  360. var _parent = $(this).parents('[class*=product][class*=images]');
  361. var designerParentDiv = _parent.addClass('VisualizationContainer')[0];
  362. designerParentFun(designerParentDiv);
  363. return false;
  364.  
  365. } else if($(this).parents('[class*=thumb][class*=product]').length) {
  366. var _parent = $(this).parents('[class*=thumb][class*=product]');
  367. var designerParentDiv = _parent.addClass('VisualizationContainer')[0];
  368. designerParentFun(designerParentDiv);
  369. return false;
  370.  
  371. } else if($(this).parents('[class*=Thumb][class*=Product][class!=ProductThumbImage]').length) {
  372. var _parent = $(this).parents('[class*=Thumb][class*=Product][class!=ProductThumbImage]');
  373. var designerParentDiv = _parent.addClass('VisualizationContainer')[0];
  374. designerParentFun(designerParentDiv);
  375. $('#fancy_outer').remove();
  376. return false;
  377. }
  378. }
  379. });
  380.  
  381. $('body form[action*="/cart.php"]').parent().addClass('NavigationContainer');
  382.  
  383. if(getData.doogmaProductId) {
  384. $('input[name=product_id]').after('<input type="hidden" class="doogma-product-id" value="'+getData.doogmaProductId+'">');
  385. }
  386.  
  387. } else {
  388. $('body .doogma-plugin').remove();
  389. }
  390. }
  391. }
  392. });
  393. }
  394.  
  395. function scrollToOption() {
  396. if($('form .form-field').length) {
  397. var options = $('form .form-field[data-product-attribute]');
  398. for(var i=0; i<options.length; i++) {
  399. var divOpt = options[i];
  400. var _this = $(divOpt);
  401. var optionName = $.trim($('label.form-label',divOpt).text().split(':')[0]);
  402. if(optionName == 'doogma-selected-heading') {
  403. var doogmaSelected = $('input',divOpt).val();
  404. for(var i=0; i<options.length; i++) {
  405. divOpt = options[i];
  406. var _inthis = $(divOpt);
  407. var seloptionName = divOpt.querySelector('[class*="doogma-"]').className.match(/doogma-\S+/)[0];
  408. if(seloptionName == doogmaSelected) {
  409. $('html,body').animate({ scrollTop: $(_inthis).offset().top}, 'slow');
  410. return;
  411. }
  412. }
  413. }
  414. }
  415. }
  416. if($('form .productAttributeRow').length) {
  417. var options = $('form .productAttributeRow');
  418. for(var i=0; i<options.length; i++) {
  419. var divOpt = options[i];
  420. var _this = $(divOpt);
  421. var optionName = $.trim($('.productAttributeLabel label span.name',divOpt).text().split(':')[0]);
  422. if( optionName == 'doogma-selected-heading') {
  423. var doogmaSelected = $('.productAttributeValue input',divOpt).val();
  424. for(var i=0; i<options.length; i++) {
  425. divOpt = options[i];
  426. var _inthis = $(divOpt);
  427. var seloptionName = divOpt.querySelector('[class*="doogma-"]').className.match(/doogma-\S+/)[0];
  428. if(seloptionName == doogmaSelected) {
  429. $('html,body').animate({ scrollTop: $(_inthis).offset().top}, 'slow');
  430. return;
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437.  
  438. function createCookie(name,value,days) {
  439. var expires = "";
  440. if (days) {
  441. var date = new Date();
  442. date.setTime(date.getTime() + (days*24*60*60*1000));
  443. expires = "; expires=" + date.toUTCString();
  444. }
  445. document.cookie = name + "=" + value + expires + "; path=/";
  446. }
  447.  
  448. function readCookie(name) {
  449. var nameEQ = name + "=";
  450. var ca = document.cookie.split(';');
  451. for(var i=0;i < ca.length;i++) {
  452. var c = ca[i];
  453. while (c.charAt(0)==' ') c = c.substring(1,c.length);
  454. if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  455. }
  456. return null;
  457. }
  458.  
  459. function eraseCookie(name) {
  460. createCookie(name,"",-1);
  461. }
  462.  
  463. function addDesignerElement(parameters) {
  464. if(typeof parameters == 'undefined') {
  465. var param = addDesignerElement.sParameters;
  466. } else {
  467. var param = addDesignerElement.sParameters = parameters;
  468. }
  469. /* if(!uiModificationComplete) {
  470. return;
  471. } */
  472. param.parentDiv.innerHTML = '';
  473.  
  474. designerDiv = document.createElement('div');
  475. designerDiv.setAttribute('class','doogma-plugin');
  476. if(param.uid) {
  477. designerDiv.setAttribute('data-uid',param.uid);
  478. }
  479. designerDiv.setAttribute('data-nav-ready', isUIReady);
  480. param.parentDiv.appendChild(designerDiv);
  481.  
  482. var script = document.createElement('script');
  483. script.setAttribute('src', '//cdne2im.doogma.com/smartmobile-v2/loader.js');
  484. param.parentDiv.appendChild(script);
  485. }
  486.  
  487. function StenciladdtoCart() {
  488. if($('.productView-details .productView-info').length) {
  489. $('.productView-details .productView-info').each(function() {
  490. if($.trim($('.productView-info-name',this).text()).indexOf('doogma-thumb') > -1) {
  491. var imageLink = $('.productView-info-value',this).text();
  492. $('.productView-image img').attr('src',imageLink);
  493. }
  494. });
  495. }
  496. }
  497.  
  498. function getPageParameters() {
  499. var el = document.querySelector('#doogma');
  500. if(el) {
  501. try {
  502. return JSON.parse(el.textContent);
  503. } catch(e) {}
  504. }
  505. return {};
  506. }
  507.  
  508. $(function() {
  509. // Delete this if condition after Shahil fixes timing issue in InkityAndCo
  510. if(window.location.hostname != 'inkityandco.com' || isTestScript) {
  511. if(getPageParameters().siteNavigationModifier) {
  512. uiModificationComplete = false;
  513. document.addEventListener('doogmaNavigationModifyComplete', function onNavModifyComplete() {
  514. document.removeEventListener('doogmaNavigationModifyComplete', onNavModifyComplete);
  515. uiModificationComplete = true;
  516. addDesignerElement();
  517. });
  518. }
  519. }
  520.  
  521. if($('input[name=product_id]').length) {
  522. var productID = $('input[name=product_id]').val();
  523. fetchProducts(productID,storehash);
  524. fetchOptions(productID,storehash);
  525. fetchOptionValues(productID,storehash);
  526. $('body').on('click', '#swipeTarget', function() {
  527. scrollToOption(productID);
  528. });
  529.  
  530. }
  531.  
  532. if($('form[name=cartForm]').length) {
  533. if($('form[name=cartForm] .CartContents').length) {
  534. $('form[name=cartForm] .CartContents tbody tr').each(function() {
  535. var _tr = $(this);
  536. if($('td',this).hasClass('ProductName')) {
  537. var _thisName = $('td.ProductName',this);
  538. if(_thisName.find('table.productAttributes').length) {
  539. _thisName.find('table.productAttributes tbody tr').each(function() {
  540. var _txt = $.trim($(this).text());
  541. if(_txt.indexOf('doogma') > -1 && _txt.indexOf('thumb') > -1 ) {
  542. var imageLink = $('td:nth-child(2)',this).text();
  543. if(_tr.find('td').hasClass('CartThumb')) {
  544. var _this = _tr.find('td.CartThumb');
  545. _this.find('img').attr('src',imageLink);
  546. }
  547. }
  548. if(_txt.indexOf('doogma') > -1 && _txt.indexOf('saved') > -1 && _txt.indexOf('design') > -1 ) {
  549. var productLink = $('td:nth-child(2)',this).text();
  550. _thisName.find(' a:first').attr('href',productLink);
  551. if(_tr.find('td').hasClass('CartThumb')) {
  552. var _this = _tr.find('td.CartThumb');
  553. _this.find('a').attr('href',productLink);
  554. }
  555. }
  556. });
  557. }
  558. }
  559. });
  560. }
  561.  
  562. if($('form[name=cartForm] .CartList').length) {
  563. $('form[name=cartForm] .CartList li').each(function() {
  564. var _tr = $(this);
  565. if($('.ProductDetails',this).length) {
  566. var _thisName = $('.ProductDetails',this);
  567. if(_thisName.find('table.productAttributes').length) {
  568. _thisName.find('table.productAttributes tbody tr').each(function() {
  569. var _txt = $.trim($(this).text());
  570. if(_txt.indexOf('doogma') > -1 && _txt.indexOf('thumb') > -1 ) {
  571. var imageLink = $('td:nth-child(2)',this).text();
  572. if(_tr.find('.ProductImage').length) {
  573. var _this = _tr.find('.ProductImage');
  574. _this.find('img').attr('src',imageLink);
  575. }
  576. }
  577. if(_txt.indexOf('doogma') > -1 && _txt.indexOf('saved') > -1 && _txt.indexOf('design') > -1 ) {
  578. var productLink = $('td:nth-child(2)',this).text();
  579. _thisName.find(' a:first').attr('href',productLink);
  580. if(_tr.find('.ProductImage').length) {
  581. var _this = _tr.find('.ProductImage');
  582. _this.find('a').attr('href',productLink);
  583. }
  584. }
  585. });
  586. }
  587. }
  588. });
  589. }
  590. }
  591.  
  592. if($('div[data-cart-content]').length) {
  593. $('div[data-cart-content] .cart-item').each(function(index) {
  594. var _tr = $(this);
  595. if($('.cart-item-name',this)) {
  596. var _thisName = $('.cart-item-name',this);
  597. _thisName.next('.cart-item-option-item').each(function() {
  598. _txt = $.trim($('.cart-item-option-label',this).text());
  599. if(_txt.indexOf('doogma') > -1 && _txt.indexOf('saved') > -1 && _txt.indexOf('design') > -1 ) {
  600. var productLink = $.trim($('.cart-item-option-value',this).text());
  601. _thisName.find('a').attr('href',productLink);
  602. }
  603. if(_txt.indexOf('doogma') > -1 && _txt.indexOf('thumb') > -1 ) {
  604. var imageLink = $.trim($('.cart-item-option-value',this).text());
  605. if(_tr.find('.cart-item-image')) {
  606. var _this = _tr.find('.cart-item-image');
  607. _this.html('<img src="'+imageLink+'" />');
  608. }
  609. }
  610. });
  611. }
  612. });
  613. $('body').on('click', 'a[data-cart-item-remove]', function() {
  614. setTimeout(function(){ location.reload(); }, 2500);
  615. });
  616. }
  617.  
  618. if($('.cart').length) {
  619. $('.cart tbody.cart-list tr').each(function(index) {
  620. var _tr = $(this);
  621. if($('td',this).hasClass('cart-item-title')) {
  622. var _thisName = $('td.cart-item-title',this);
  623. $('td.cart-item-title .definitionList-key',this).each(function() {
  624. _txt = $.trim($(this).text());
  625. if(_txt.indexOf('doogma') > -1 && _txt.indexOf('saved') > -1 && _txt.indexOf('design') > -1 ) {
  626. var productLink = $.trim($(this).next('.definitionList-value').text());
  627. _thisName.find('.cart-item-name a').attr('href',productLink);
  628. }
  629. if(_txt.indexOf('doogma') > -1 && _txt.indexOf('thumb') > -1 ) {
  630. var imageLink = $.trim($(this).next('.definitionList-value').text());
  631. if(_tr.find('td').hasClass('cart-item-figure')) {
  632. var _this = _tr.find('td.cart-item-figure');
  633. _this.html('<img src="'+imageLink+'" />');
  634. //_this.find('img').attr('src',imageLink);
  635. }
  636. }
  637. });
  638. }
  639. });
  640. $('body').on('click', '.cart-remove', function() {
  641. setTimeout(function(){ location.reload(); }, 2500);
  642. });
  643. }
  644.  
  645. // Cart JSON
  646. if(siteURL.indexOf('/cart') > -1) {
  647. host = '/api/storefront/cart?include=lineItems.digitalItems.options,lineItems.physicalItems.options';
  648. $.ajax({
  649. type: 'GET',
  650. url: host,
  651. dataType: 'json',
  652. success: function(response){
  653. var items = response[0]['lineItems']['physicalItems'];
  654. $.each(items, function(index,value) {
  655. var thumb , designLink = '';
  656. $.each(value.options, function(oIndex,oValue) {
  657. if(oValue.name == 'doogma-thumb') {
  658. thumb = oValue.value;
  659. } else if(oValue.name == 'doogma-saveddesignlink') {
  660. designLink = oValue.value;
  661. } else if(oValue.name == 'Link To Product Image') {
  662. thumb = oValue.value;
  663. } else if(oValue.name == 'Link to Design Page') {
  664. designLink = oValue.value;
  665. }
  666. });
  667. var proList = $('div[data-cart-content] .cart-item')[index];
  668. $('img',proList).attr('src',thumb);
  669. if($('a',proList).attr('href') == value.url) {
  670. $('a[href="'+value.url+'"]',proList).attr('href',designLink);
  671. }
  672. });
  673. }
  674. });
  675. }
  676.  
  677. // Customer Logged In or not On Account page
  678. function parseJwt(token) {
  679. if(token != '') {
  680. const base64HeaderUrl = token.split('.')[0];
  681. const base64Header = base64HeaderUrl.replace('-', '+').replace('_', '/');
  682. const headerData = JSON.parse(window.atob(base64Header));
  683. const base64Url = token.split('.')[1];
  684. const base64 = base64Url.replace('-', '+').replace('_', '/');
  685. const dataJWT = JSON.parse(window.atob(base64));
  686. dataJWT.header = headerData;
  687. return dataJWT;
  688. }
  689. }
  690.  
  691. function customerOrderitems(value) {
  692. host = '/api/storefront/order/'+value+'?include=lineItems.digitalItems.options,lineItems.physicalItems.options';
  693. $.ajax({
  694. type: 'GET',
  695. url: host,
  696. dataType: 'json',
  697. success: function(response1) {
  698. var items = response1['lineItems']['physicalItems'];
  699. $.each(items, function(index,value) {
  700. var thumb , designLink = '';
  701. $.each(value.options, function(oIndex,oValue) {
  702. if(oValue.name == 'doogma-thumb') {
  703. thumb = oValue.value;
  704. } else if(oValue.name == 'doogma-saveddesignlink') {
  705. designLink = oValue.value;
  706. } else if(oValue.name == 'Link to Product Image') {
  707. thumb = oValue.value;
  708. } else if(oValue.name == 'Link to Design Page') {
  709. designLink = oValue.value;
  710. }
  711. });
  712. var imageUrl = value.imageUrl.split('/');
  713. imageUrl = imageUrl[imageUrl.length-1];
  714. imageUrl = imageUrl.split('.')[0];
  715. if($('input[value="'+value.id+'"]').length) {
  716. var parentEle = $('input[value="'+value.id+'"]').parents()[1];
  717. var imgEle = $('img', parentEle);
  718. imgEle.removeAttr('class');
  719. imgEle.attr('src',thumb);
  720. } else if($('body img[src *="'+imageUrl+'"]').length) {
  721. var imgEle = $('body img[src *="'+imageUrl+'"]');
  722. imgEle.removeAttr('class');
  723. imgEle.attr('src',thumb);
  724. }
  725. });
  726. }
  727. });
  728. }
  729. // all Orders page
  730. if(siteURL.indexOf('action=view_order') > -1 || siteURL.indexOf('action=order_status') > -1) {
  731. var appClientId = "gq98apmftghdptreh2utos3hs4w0sp0";
  732. $.ajax({
  733. url: "/customer/current.jwt?app_client_id="+appClientId,
  734. crossDomain: true,
  735. type: 'GET',
  736. header: {
  737. "Access-Control-Allow-Origin": "*",
  738. },
  739. success: function(response) {
  740. const jwtDecoded = parseJwt(response);
  741. if(jwtDecoded) {
  742. $.ajax({
  743. url: serverURL+'frontJs/customerOrders.php?customerID='+jwtDecoded.customer.id+'&'+storehash,
  744. crossDomain: true,
  745. type: 'GET',
  746. dataType: 'json',
  747. processData: false,
  748. header: {
  749. "Access-Control-Allow-Origin": "*",
  750. },
  751. success: function(response) {
  752. if(response.length > 0) {
  753. $.each(response, function(index,value) {
  754. customerOrderitems(value);
  755. });
  756. }
  757. }
  758. });
  759. }
  760. }
  761. });
  762. // Single Order page
  763. if(siteURL.indexOf('order_id=') > -1) {
  764. var order_id = siteURL.split('order_id=')[1];
  765. order_id = order_id.replace( /\D+$/, '');
  766. customerOrderitems(order_id);
  767. }
  768. }
  769.  
  770. });
  771.  
  772. if($('#form-action-addToCart').length) {
  773. $('body').on('click', '#form-action-addToCart', function (){
  774. if($('form .form-field').length) {
  775. var ProName = $('.productView-title').text();
  776. $('form .form-field[data-product-attribute]').each(function() {
  777. var _this = $(this);
  778. var optionName = $.trim($('label.form-label',this).text().split(':')[0]);
  779. if( optionName == 'saved design link') {
  780. var productLink = $('input',this).val();
  781. createCookie(ProName+'link',productLink,1);
  782. }
  783. if( optionName == 'doogma-thumb') {
  784. var doogmaThumb = $('textarea',this).val();
  785. createCookie(ProName+'thumb',doogmaThumb,1);
  786. }
  787. });
  788. }
  789. setTimeout(function(){ StenciladdtoCart(); }, 3000);
  790. });
  791. }
  792.  
  793. if($('.AddCartButton').length) {
  794. $('body').on('click', '.AddCartButton', function (){
  795. if($('form .productAttributeRow').length) {
  796. var ProName = $('.ProductDetailsGrid h1').text();
  797. $('form .productAttributeRow').each(function() {
  798. var optionName = $.trim($('.productAttributeLabel label span.name',this).text().split(':')[0]);
  799. if( optionName == 'saved design link') {
  800. var productLink = $('.productAttributeValue input',this).val();
  801. createCookie('currentProductLink',productLink,1);
  802. }
  803. if( optionName == 'doogma-thumb') {
  804. var doogmaThumb = $('.productAttributeValue textarea',this).val();
  805. createCookie('currentProductThumb',doogmaThumb,1);
  806. }
  807. });
  808. }
  809. });
  810. }
  811.  
  812. $('body').on('click', '.quickview', function(){
  813. setTimeout(function(){
  814. var productID = $('input[name=product_id]').val();
  815. fetchProducts(productID,storehash);
  816. fetchOptions(productID,storehash);
  817. fetchOptionValues(productID,storehash);
  818. $('body').on('click', '#form-action-addToCart', function (){
  819. setTimeout(function(){ StenciladdtoCart(); }, 3000);
  820. });
  821. }, 1500);
  822. });
  823.  
  824. $(document).ajaxComplete(function( event, xhr, settings ) {
  825. if (settings.url.indexOf('/remote.php?w=getproductquickview') > -1) {
  826. setTimeout(function(){
  827. var productID = $('input[name=product_id]').val();
  828. fetchProducts(productID,storehash);
  829. fetchOptions(productID,storehash);
  830. fetchOptionValues(productID,storehash);
  831. }, 1500);
  832. }
  833. if (settings.url.indexOf('/cart.php') > -1) {
  834. $('.fastCartThumb img').attr('src',readCookie('currentProductThumb'));
  835. $('.fastCartThumb img').attr('width','200px');
  836. $('.fastCartItemBox div a').attr('href',readCookie('currentProductLink'));
  837. }
  838. });
  839.  
  840. $(document).ajaxStop(function(){
  841. isUIReady = true;
  842. if(designerDiv) {
  843. designerDiv.setAttribute('data-nav-ready', 'true');
  844. }
  845. productionScript.setAttribute('data-navigation-complete', '');
  846. dispatchCustomEvent(document, 'doogmaNavigationComplete');
  847. });
  848. }
  849.  
  850. // +---------------------+
  851. // | Utility Functions |
  852. // +---------------------+
  853. /**
  854. * Manipulates a URL by using "a" (Anchor) DOMElement. If input url is not absolute url then computation is done relative to document's current page.
  855. * It means if protocol and host in input url is missing then the current document protocol and host is used as protocol and host of input url.
  856. * If url starts without a forwarding slash then current page's base path is used as base of input url.
  857. *
  858. * Important: Do not set a url directly through 'href' property, instead set url through 'setUrl()' function.
  859. *
  860. * @param value {String} URL value. If it is missing then document URL is used instead.
  861. *
  862. * @returns a {DOMElement} An anchor DOMElement with extended functionalities for URL manipulation
  863. */
  864. function docurl(value) {
  865. var a = document.createElement('a');
  866. if(typeof value == 'undefined') {
  867. setUrl(document.location.href);
  868. } else {
  869. setUrl(value);
  870. }
  871. a.setUrl = setUrl;
  872. a.hasSearch = hasSearch;
  873. a.getSearch = getSearch;
  874. a.setSearch = setSearch;
  875. a.removeSearch = removeSearch;
  876. return a;
  877.  
  878.  
  879. function setUrl(value) {
  880. if(value = value.trim()) {
  881. a.setAttribute('href',value);
  882. } else {
  883. a.removeAttribute('href');
  884. }
  885. return this;
  886. }
  887.  
  888. function hasSearch(prop) {
  889. return typeof getSearch(prop)=='string';
  890. }
  891.  
  892. function getSearch(prop) {
  893. var s = a.search;
  894. if(s.length<2) {
  895. return undefined;
  896. }
  897. var m = s.match(new RegExp('[\\?&]'+prop+'(=[^&]*)?(&|$)'));
  898. if(!m) {
  899. return undefined;
  900. }
  901. m = m[0];
  902. s = m.slice(1, m[m.length-1]=='&'?-1:m.length);
  903. m = s.indexOf('=');
  904. return m == -1? '' : decodeURIComponent(s.slice(m+1));
  905. }
  906.  
  907. function setSearch(prop,val) {
  908. if(typeof val=='undefined') {
  909. var val = '';
  910. } else {
  911. val = decodeURIComponent(val)==val? encodeURIComponent(val) : val;
  912. }
  913. var s = a.search;
  914.  
  915. // search
  916. var f = new RegExp('[\\?&]'+prop+'(=[^&]*)?(&|$)').exec(s);
  917.  
  918. if(val) {
  919. prop += '=' + val;
  920. }
  921. if(f) { // replace existing
  922. var e = f.index+f[0].length;
  923. e = e==s.length? '' : s.slice(e-1);
  924. s = s.slice(0, f.index+1) + prop + e;
  925. } else { // add new
  926. if(s.length>1) {
  927. s += '&' + prop;
  928. } else {
  929. s += prop;
  930. }
  931. }
  932. a.search = s;
  933. return this;
  934. }
  935.  
  936. function removeSearch(prop) {
  937. var s = a.search;
  938. var f = new RegExp('[\\?&]'+prop+'(=[^&]*)?(&|$)').exec(s);
  939. if(!f) {
  940. return this;
  941. }
  942. var e = f.index+f[0].length;
  943. if(f.index>0 && e<s.length) {
  944. e--;
  945. }
  946. a.search = s.slice(0, f.index) + s.slice(e);
  947. return this;
  948. }
  949. }
  950.  
  951. function dispatchCustomEvent(elem, type, bubbles, cancelable, detail) {
  952. if (typeof bubbles == 'undefined')
  953. var bubbles = false;
  954. if (typeof cancelable == 'undefined')
  955. var cancelable = false;
  956. try {
  957. var evt = new CustomEvent(type, { bubbles:bubbles, cancelable:cancelable, detail:detail });
  958. } catch(e) {
  959. evt = document.createEvent('CustomEvent');
  960. evt.initCustomEvent(type, bubbles, cancelable, detail);
  961. }
  962. elem.dispatchEvent(evt);
  963. }
  964. })();
  965. /********************infothreeg@gmail.com (Suman Bansal) *********************/
Add Comment
Please, Sign In to add comment