Guest User

doogma-test.js

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