Team:TUDelft/js/script

(function () {

   $(function ($) {
       // testing variable
       var testing_protocols = false;
       // scrolling for hashtag links
       var scrollOfset = 150;
       
       var location = window.location.href;
       function scrollTo(hash, callback) {
           if (hash === '#!') {return}
           hash = hash + '-scroll';
           var offset = $(hash).offset();
           if (!offset) {
               console.log("No such element to scroll to: " + hash);
               return
           }
           console.log("Im scrolling to " + hash);
           $('html, body').animate({
               scrollTop: offset.top - scrollOfset
           }, 700, 'swing', function () {});
           callback(hash);
       }
       function openProtocol(hash) {
           console.log("Also opening the protocol: " + hash);
           var index = -1;
           $('#protocols>li').each(function (i) {
               var $this = $(this);
               var id = $this.children().first().attr('id');
               if (id === hash.slice(1)) {
                   index = i;
                   return false;
               }
           })
           if (index === -1) {
               console.log("Couldn't find the protocol to open: " + hash);
           } else {
               console.log("Opening " + hash);
               console.log(index);
               $('.collapsible').collapsible('open', index);
           }
       }
       
       function openNotebook(hash) {
           console.log("Also opening the notebook: " + hash);
           var index = -1;
           $('#notebook-wrapper>li').each(function (i) {
               var $this = $(this);
               var id = $this.children().first().attr('id');
               if (id === hash.slice(1)) {
                   index = i;
                   return false;
               }
           })
           if (index === -1) {
               console.log("Couldn't find the notebook to open: " + hash);
           } else {
               console.log("Opening " + hash);
               console.log(index);
               $('.collapsible#notebook-wrapper').collapsible('open', index);
           }
       }
       function processHash(hash) {
           if (hash) {
               if (location.toLowerCase().includes('protocols') || testing_protocols) {
                   scrollTo(hash, openProtocol);
               } else if (location.toLowerCase().includes('notebook')){
                   scrollTo(hash, openNotebook)
               } else {
                   scrollTo(hash, function () {});
               }
           }
       }
       


       processHash(window.location.hash); // init
       $(window).on('hashchange', function () {
           console.log("Activated the hashchange");
           processHash(window.location.hash);
       });
       
       $('a[href="#references"]').on('click', function(e) {
           processHash(window.location.hash);
       })
       
       $("#timeline a").each(function (i) {
           var $this = $(this);
           var hash = $this.attr('href');
           $this.on('click', function(e) {
               openNotebook(hash);
           })
           
       })
       
       $('.topper-inner > a').hover(function () {
           console.log("Hovering know");
           $('#design-init').css('opacity', '0'); 
       }, function () {
           $('#design-init').css('opacity', '1');
       });


       //timeline

// $('#timeline > div').on('click', function () { // $('#timeline .active').removeClass('active'); // $(this).addClass('active'); // })

       //navbar and a mobile friendly way to navigate
       $(".dropdown-button").dropdown({
           hover: false,
           constrainWidth: false,
           belowOrigin: true
       });
       $('#menu-btn').click(function () {
           $('nav').addClass('show-nav');
       });
       $('#cross').click(function () {
           $('nav').removeClass('show-nav');
       });
       
       // notebook filter
       var module_classes = ['cas13a', 'TDP', 'detection', 'sampleprep', 'vesicles'];
       var whosActive = function () {
           var active = new Array(module_classes.length);
           $('#notebook-buttons>a').each(function (index) {
               var $this = $(this);
               active[index] = hasClass($this, 'active');
           })
           return active;
       };
       var hasClass = function ($el, className) {
           return $el.attr('class').split(' ').some(
               el => el === className)
       };
       var display = function (active) {
           for (var i = 0; i < active.length; i++) {
               var $module = $("." + module_classes[i]);
               if (active[i]) {
                   $module.css("display", "block");
               } else {
                   $module.css("display", "none");
               }
           }
       };
       display(whosActive()) // initialization of notebook filter
       $('#notebook-buttons>a').each(function (index) {
           var $this = $(this);
           $this.on('click', function () {
               console.log("I clicked this");
               $this.toggleClass('active');
               display(whosActive());
           });
       });
       // A button to scroll all the way up.
       $("#up-btn").on('click', function () {
           $('html, body').animate({
               scrollTop: 0,
           }, 600);
           return false;
       });


       //toc
       //        $(".scrollspy").scrollSpy();
       //        setTimeout(function () {
       //            $("#toc").pushpin({
       //                offset: 150,
       //                top: 0,
       //                bottom: $('footer').offset().top - $('#toc').outerHeight(true)
       //            })
       //        }, 100);
       //timeline
       //        $(".scrollspy").scrollSpy(
       //            {
       //                getActiveElement: function (id) {
       //                    return "round" + (parseInt(id.replace(/\D/g, )) - 20);
       //                }
       //            }
       //        );


       //main
       //        function scaleIn() {
       //            $("scaleIn").each(function() {
       //                setTimeout(function () {
       //                    $(this).addClass('activated');
       //                }, 500)
       //            });
       //        }

// function scaleIn(el) { // console.log("im called"); // $(".scaleIn").each(function (index) { // var that = $(this); // setTimeout(function () { // that.addClass('activated') // }, 200 * index); // }) // }

       // A few materialize initilizations. Look for function on materializecss.com.
       
       $(".scrollspy").scrollSpy({
           scrollOfset: scrollOfset
       });
       $(".modal").modal();

// $('select').material_select(); // var options = [{ // selector: '#thisList', // offset: 500, // callback: function (el) { // Materialize.showStaggeredList($(el)); // } // }, { // selector: '#row1', // offset: 0, // callback: scaleIn // }]; // Materialize.scrollFire(options);

       // Dynamically add e-mail to distract spam bots
       var white = " ",
           first = "\u00A0\u00A0tudelft",
           secon = "igem",
           third = "gmail",
           forth = "com",
           all = " " + " " + first + "." + secon + "@" + third + "." + forth,
           sall = " " + " " + all,
           ma = "ma" + "il",
           mm = ma + "to" + ":";
       $("#mail-button").attr("href", mm + all.trim());
       $("#email").text(white + sall);
       // Removing empty p tages, added by the mediawiki
       $("p").filter(function () {
           return $.trim($(this).text()) === ;
       }).remove();
       

// touch friendly hover menu // $('top_menu_14, top_menu_inside ').on('touchstart', function (e) { // var $this = $(this); // if ($this.hasClass('hover')) { // return true // } else { // $this.addClass('hover'); // e.preventDefault(); // return false; // } // })


   });

}($)); // test function //function filter_notebook(select) { // console.log(select); //} // function for copying text to clipboard (used for phone number) function copyToClipboard(element) {

   var $temp = $("<input>");
   $("body").append($temp);
   $temp.val(element).select();
   document.execCommand("copy");
   $temp.remove();
   alert("You copied: " + element + ".");

}