Guest User

Untitled

a guest
Jun 21st, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function MENU() {
  2.     var id,menuName, selected, menuLevel, goingBack;
  3.  
  4.     this.opened = false;
  5.  
  6.     debug = function(msg) {
  7.         $("#ogrpErrDiv").prepend("<br />Menu Debug: " + msg);
  8.     };
  9.  
  10.     init = function() {
  11.         id = 0;
  12.         menuName = "";
  13.         menuLevel = -1;
  14.         selected = -1;
  15.         goingBack = false;
  16.  
  17.         buildMainMenu();
  18.     };
  19.  
  20.     buildMainMenu = function() {
  21.         $("body").append(
  22.             $("<div>",{"id":"mainMenu","class":"mainMenu"}).css("display","none").append(
  23.                 $("<h1>",{"id":"menuHeader","class":"menuHeader"})
  24.             ).append(
  25.                 $("<div>",{"id":"menuChoices","class":"menuChoices"})
  26.             ).append(
  27.                 $("<div>",{"id":"menuDescription","class":"menuDescription"}).append(
  28.                     $("<div>",{"id":"menuDescriptionContent","class":"menuDescriptionContent"})
  29.                 )
  30.             )
  31.         );
  32.     };
  33.  
  34.     getChoicesCount = function() {
  35.         return $("#menuChoices").find(".menuChoice").length;
  36.     };
  37.  
  38.     getChoiceText = function() {
  39.         return $("#menuChoices").find(".MenuChoiceSelected")[0].innerHTML;
  40.     };
  41.  
  42.     getChoiceDesc = function() {
  43.         var selectedChoiceEl = $("#menuChoices").find(".MenuChoiceSelected")[0];
  44.  
  45.         return ($(selectedChoiceEl).attr("data-desc")) ? $(selectedChoiceEl).data("desc"):"";
  46.     };
  47.  
  48.     scrollToMenuOption = function() {
  49.         if($("#menuChoices").find(".MenuChoiceSelected").length) {
  50.             var scrollto = $($("#menuChoices").find(".MenuChoiceSelected"));
  51.             var container = $("#menuChoices");
  52.  
  53.             if(scrollto.offset().top < container.offset().top || scrollto.offset().top + scrollto.height() >= container.offset().top+container.height()) {
  54.                 container.scrollTop(scrollto.offset().top - container.offset().top + container.scrollTop());
  55.             }
  56.         }
  57.     };
  58.  
  59.     setHeaderColor = function(headerColor) {
  60.         $("#menuHeader").css("backgroundColor",headerColor);
  61.     };
  62.  
  63.     this.open = function(data) {
  64.         menuName = data.menudata.name;
  65.         var choices = data.menudata.choices;
  66.         var css = (data.menudata.css) ? data.menudata.css:false;
  67.         var headerColor = (css.header_color) ? css.header_color:false;
  68.  
  69.         $("#menuChoices").empty();
  70.         $("#menuHeader").html(menuName);
  71.  
  72.         setHeaderColor(headerColor);
  73.  
  74.         if(!this.opened) {
  75.             this.opened = true;
  76.             goingBack = false;
  77.             menuLevel = 0;
  78.         } else if(!goingBack) {
  79.             ++menuLevel;
  80.         } else {
  81.             goingBack = false;
  82.         }
  83.  
  84.         $("#mainMenu").show();
  85.  
  86.         $.each(choices,function(i,c) {
  87.             $("#menuChoices").append(
  88.                 $("<div>",{"class":"menuChoice", "data-desc":c[1]}).html(getChoiceContents(c[0]))
  89.             );
  90.         });
  91.  
  92.         this.setSelected(0);
  93.     };
  94.  
  95.     getChoiceContents = function(choice) {
  96.         if(choice.substr(0,1) === "<") {
  97.             var bgColor = $(choice).data("bgcolor");
  98.             return $("<div>").html($(choice).css("background-color",bgColor));
  99.         } else {
  100.             return choice;
  101.         }
  102.     };
  103.  
  104.     this.close = function() {
  105.         if(this.opened) {
  106.             goingBack = true;
  107.             --menuLevel;
  108.  
  109.             if(menuLevel < 1) {
  110.                 this.opened = false;
  111.                 $("#mainMenu").hide();
  112.             }
  113.  
  114.             if(this.onClose) {
  115.                 this.onClose();
  116.             }
  117.         }
  118.     };
  119.  
  120.     this.setSelected = function(i) {
  121.         selected = i;
  122.         $(".menuChoice").removeClass("MenuChoiceSelected");
  123.  
  124.         if(selected < 0) {
  125.             selected = getChoicesCount()-1;
  126.         } else if(selected >= getChoicesCount()) {
  127.             selected = 0;
  128.         }
  129.  
  130.         if(selected >= 0 && (selected < getChoicesCount())) {
  131.             $("#menuChoices div.menuChoice:nth-child(" + (selected + 1) + ")").addClass("MenuChoiceSelected");
  132.         }
  133.  
  134.         scrollToMenuOption();
  135.  
  136.         if(getChoiceDesc().length) {
  137.             $("#menuDescriptionContent").html(getChoiceDesc());
  138.             $("#menuDescription").show();
  139.         } else {
  140.             $("#menuDescription").hide();
  141.         }
  142.     };
  143.  
  144.     this.moveUp = function() {
  145.         if(this.opened) {
  146.             this.setSelected(selected-1);
  147.         }
  148.     };
  149.  
  150.     this.moveDown = function() {
  151.         if(this.opened) {
  152.             this.setSelected(selected+1);
  153.         }
  154.     };
  155.  
  156.     this.valid = function(mod) {
  157.         if(selected >= 0 && selected < getChoicesCount()) {
  158.             if(this.onValid && this.opened) {
  159.                 this.onValid(getChoiceText(), mod);
  160.             }
  161.         }
  162.     };
  163.  
  164.     init();
  165. }
Add Comment
Please, Sign In to add comment