Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FMPSystems.Position = extendClass(FMPSystems, {
  2.     // function which initializes the events on the provided element.
  3.     // the function requires plain javascript element (not jQuery) as parameter
  4.     init: function(el){
  5.         this.containerElement = el;
  6.         this.boundCursorPosition = this.cursorPosition.bind(this);
  7.         this.boundClickHandler = this.handleClick.bind(this);
  8.  
  9.         el.addEventListener('mousemove', this.boundCursorPosition);
  10.         el.addEventListener('click', this.boundClickHandler);
  11.     },
  12.  
  13.     // function which removes all the events and handlers from the element.
  14.     disable: function(){
  15.         var el = this.containerElement,
  16.             player = el.getElementsByClassName('player')[0];
  17.  
  18.         // handle the case in which the disable method is called before initalization
  19.         // if this is the case, throw an error displaying the problem
  20.         if(el){
  21.             el.removeEventListener('mousemove', this.boundCursorPosition);
  22.             el.removeEventListener('click', this.boundClickHandler);
  23.             player ? player.remove() : '';
  24.         }
  25.         else{
  26.             throw new Error('The step was not initalized, the disable function can be called only after initialization.');
  27.         }
  28.     },
  29.  
  30.     // function to reset data
  31.     reset: function(){
  32.         var el = this.containerElement,
  33.             player = el.getElementsByClassName('player')[0];
  34.  
  35.         // handle the case in which the disable method is called before initalization
  36.         // if this is the case, throw an error displaying the problem
  37.         if(el){
  38.             player ? player.remove() : '';
  39.         }
  40.         else{
  41.             throw new Error('The step was not initalized, the reset function can be called only after initialization.');
  42.         }
  43.     },
  44.  
  45.     // save the mouse position in an object in order to have access to the values
  46.     // in other methods as well
  47.     mousePosition: {},
  48.  
  49.     // function which updates the 'mousePosition' object with the new values while the
  50.     // user changes the mouse position
  51.     // the function expects the event object as parameter
  52.     cursorPosition: function(e){
  53.         var el = this.containerElement,
  54.             offset = el.getBoundingClientRect();
  55.  
  56.         // get the cursor position relative to the scroll position of the page
  57.         this.mousePosition = {
  58.             mouseX: e.pageX - offset.left || e.clientX - offset.left,
  59.             mouseY: e.pageY - offset.top || e.clientY - offset.top
  60.         };
  61.     },
  62.  
  63.     // function which handles the click event on the desired element.
  64.     // it creates the player element and adds the required coordinates.
  65.     handleClick: function(){
  66.         var el = this.containerElement,
  67.             player = el.getElementsByClassName('player')[0];
  68.  
  69.         // if the player element is already created remove it and create a new player element
  70.         player ? player.remove() : '';
  71.         this.createPlayerElement();
  72.         this.addCoordinates();
  73.     },
  74.  
  75.     // function which creates the player 'div' and adds it to the DOM in the
  76.     // required position
  77.     createPlayerElement: function(){
  78.         var player = document.createElement('div'),
  79.             el = this.containerElement,
  80.         // get the scroll position in order to precisely calculate the elements position
  81.             scrollOffset = {
  82.                 top: window.pageYOffset || document.documentElement.scrollTop,
  83.                 left: window.pageXOffset || document.documentElement.scrollLeft
  84.             };
  85.  
  86.         player.className = 'player';
  87.         player.style.display = 'none';
  88.  
  89.         el.appendChild(player);
  90.  
  91.         // calculate the position of the element in a relative manner, in order for when the user resizes the browser
  92.         // to keep the distances proportional
  93.         $(player).css({
  94.             top: (this.mousePosition.mouseY - $(player).width() / 2 - scrollOffset.top) / el.offsetHeight * 100 + '%',
  95.             left: (this.mousePosition.mouseX - $(player).height() / 2 - scrollOffset.left) / el.offsetWidth * 100 + '%',
  96.             display: 'block'
  97.         });
  98.     },
  99.  
  100.     // creates the coordinates used to populate the data-attributes of the player element
  101.     // the coordinates ranges are: x - from -1 to 1; y - from 0 to 1
  102.     createCoordinates: function(){
  103.         var coordinates = {},
  104.             el = this.containerElement,
  105.             scrollOffset = {
  106.                 top: window.pageYOffset || document.documentElement.scrollTop,
  107.                 left: window.pageXOffset || document.documentElement.scrollLeft
  108.             };
  109.  
  110.         // create the coordinates, x - coordinate ranges from -1 to 1; y - coordinate ranges from 0 to 1
  111.         coordinates.X = Math.round((this.mousePosition.mouseX - scrollOffset.left) / $(el).width() * 100);
  112.         coordinates.Y = Math.round((this.mousePosition.mouseY - scrollOffset.top) / $(el).height() * 100);
  113.  
  114.         return coordinates;
  115.     },
  116.  
  117.     // creates the data attributes and adds the correct values to them
  118.     addCoordinates: function(){
  119.         var el = this.containerElement,
  120.             player = el.getElementsByClassName('player')[0],
  121.             coordinates = this.createCoordinates();
  122.  
  123.         player.setAttribute('data-x-axis', coordinates.X);
  124.         player.setAttribute('data-y-axis', coordinates.Y);
  125.     },
  126.  
  127.     // method used for getting the coordinates data from the player element
  128.     // it returns and object with 'x' and 'y' properties which hold the given coordinates.
  129.     getData: function(){
  130.         var player = $('.player')[0];
  131.  
  132.         // if the function is called before creating any coordinates (player element) then
  133.         // throw an error which tells the user to create the element first, and then call this function
  134.         if(player){
  135.             return {
  136.                 x: parseInt(player.getAttribute('data-x-axis'), 10),
  137.                 y: parseInt(player.getAttribute('data-y-axis'), 10)
  138.             };
  139.         }
  140.         else{
  141.             throw new Error('The coordinates data were not added, add the player and try again.');
  142.         }
  143.     },
  144.  
  145.     // method used to set coordinates data onto the player element
  146.     // this method also creates the element and adds it to the page in the appropriate position
  147.     // it expects and object as parameter which holds the 'x' and 'y' properties with the appropriate values
  148.     setData: function(obj){
  149.         var player = document.createElement('div'),
  150.             existingPlayer = $('.player'),
  151.             el = this.containerElement,
  152.             xPosition = obj.x,
  153.             yPosition = obj.y;
  154.  
  155.         // if the player element already exists, remove it and create a new element with the added coordinates
  156.         if(existingPlayer.length){
  157.             existingPlayer.remove();
  158.         }
  159.  
  160.         // if the input values are out of the accepted range, then throw errors accordingly
  161.         if(obj.x > 100 || obj.x < 0){
  162.             throw new Error('The X coordinate is out of range, the range is from 0 to 100');
  163.         }
  164.  
  165.         if(obj.y > 100 || obj.y < 0){
  166.             throw new Error('The Y coordinate is out of range, the range is from 0 to 100');
  167.         }
  168.  
  169.         player.className = 'player';
  170.  
  171.         player.setAttribute('data-x-axis', obj.x);
  172.         player.setAttribute('data-y-axis', obj.y);
  173.  
  174.         el.appendChild(player);
  175.  
  176.         // position the element in respect to the input coordinates
  177.         $(player).css({
  178.             left: 'calc(' + xPosition + '% - ' + player.offsetHeight / 2 + 'px)',
  179.             top: 'calc(' + yPosition + '% - ' + player.offsetWidth / 2 + 'px)'
  180.         });
  181.     },
  182.  
  183.     /**
  184.      * load data from event
  185.      *
  186.      * @param {object} event
  187.      */
  188.     load: function(event){
  189.         var eventData = event.getData();
  190.  
  191.         if(eventData.coordinates !== undefined){
  192.             this.setData(eventData.coordinates);
  193.         }
  194.         else {
  195.             this.reset();
  196.         }
  197.     },
  198.  
  199.     /**
  200.      * save data to event, returns true if data was changed
  201.      *
  202.      * @param {object} event
  203.      *
  204.      * @returns {boolean}
  205.      */
  206.     save: function(event){
  207.         try {
  208.             var eventData = event.getData();
  209.  
  210.             //check if data unset or changed and set if so
  211.             if(eventData.coordinates === undefined || this.compareData(eventData.coordinates) === false){
  212.                 event.data.coordinates = this.getData();
  213.  
  214.                 return true;
  215.             }
  216.         }
  217.         catch(e){}
  218.  
  219.         return false;
  220.     },
  221.  
  222.     /**
  223.      * compare data with current set data, return true if identical
  224.      *
  225.      * @param {object} data
  226.      *
  227.      * @returns {boolean}
  228.      */
  229.     compareData: function(data){
  230.         var currentData = this.getData();
  231.  
  232.         return (data.x === currentData.x && data.y === currentData.y);
  233.     }
  234. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement