Guest User

invite.js

a guest
Aug 16th, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. +function($) {
  2.   $(function() {
  3.     var invites = function() {
  4.       /**
  5.        * @param {Object} element
  6.        * @return {undefined}
  7.        */
  8.       function init(element) {
  9.         var list = element.find(".invite-card .card-info .hint time");
  10.         /** @type {number} */
  11.         var y = Date.now();
  12.         /** @type {number} */
  13.         var resolution = 1E3;
  14.         /** @type {number} */
  15.         var total = 60;
  16.         /** @type {number} */
  17.         var max = 60 * total;
  18.         /** @type {number} */
  19.         var base = 24 * max;
  20.         /** @type {number} */
  21.         var i = 0;
  22.         for (;i < list.length;i++) {
  23.           var x = list.eq(i).attr("data-time");
  24.           if ("undefined" != typeof x) {
  25.             x -= 0;
  26.             /** @type {number} */
  27.             list[i].targetTime = parseInt(x + y / resolution);
  28.             clearInterval(list[i].counting);
  29.             (function(pos) {
  30.               /** @type {number} */
  31.               list[pos].counting = setInterval(function() {
  32.                 /** @type {number} */
  33.                 var n = parseInt(list[pos].targetTime - Date.now() / resolution);
  34.                 /** @type {number} */
  35.                 n = 0 >= n ? 0 : n;
  36.                 /** @type {number} */
  37.                 var str = n % total;
  38.                 n -= str;
  39.                 /** @type {string} */
  40.                 str = ("00" + str).substr(-2) + "s";
  41.                 /** @type {number} */
  42.                 var m = parseInt(n % max / total);
  43.                 n -= m;
  44.                 /** @type {string} */
  45.                 m = ("00" + m).substr(-2) + "m ";
  46.                 /** @type {number} */
  47.                 var num = parseInt(n % base / max);
  48.                 n -= num;
  49.                 /** @type {string} */
  50.                 num = ("00" + num).substr(-2) + "h ";
  51.                 /** @type {number} */
  52.                 var y = parseInt(n / base);
  53.                 /** @type {string} */
  54.                 y = ("00" + y).substr(-2) + "d ";
  55.                 list.eq(pos).text(y + num + m + str);
  56.               }, 300);
  57.             })(i);
  58.           }
  59.         }
  60.       }
  61.       var element = $(document).find(".my-invites, .share-invites, .invites, .js-off-canvas-invites");
  62.       element.find(".invite-card .card-action .share-it");
  63.       return init(element), $(document).on("click", ".invite-card .card-action .share-it", function(event) {
  64.         event.preventDefault();
  65.         event.stopPropagation();
  66.         var submit = $(this);
  67.         var record = submit.parents(".invite-card");
  68.         var compiled = record.clone();
  69.         compiled.find(".card").removeClass("has-action").find(".card-action").remove();
  70.         element.find(".misc .invite-card-placeholder").html(compiled);
  71.         element.find(".misc").show();
  72.       }), element.find(".misc .attach-msg").on("click", function(types) {
  73.         types.preventDefault();
  74.         $(this).hide();
  75.         $("#attachedMsg").parents(".form-group").eq(0).removeClass("hidden");
  76.       }), element.find(".misc button[type=reset]").on("click", function() {
  77.         element.find(".misc .attach-msg").show();
  78.         $("#attachedMsg").parents(".form-group").eq(0).addClass("hidden");
  79.         element.find(".misc").hide();
  80.       }), element.on("inputValidated.oneplus", ".js-try-again", function() {
  81.         var href = ($(this), $("#invite_section_one").val() + "-" + $("#invite_section_two").val() + "-" + $("#invite_section_three").val() + "-" + $("#invite_section_four").val());
  82.         /** @type {string} */
  83.         var nameAnchor = window.location.href;
  84.         /** @type {string} */
  85.         window.location.href = nameAnchor.split("/claim")[0] + "/claim/" + href;
  86.       }), element.on("paste", "#invite_section_one", function(dataAndEvents) {
  87.         var $target = $(this);
  88.         $target.attr("maxlength", 1E3);
  89.         setTimeout(function() {
  90.           var parts = $target.val();
  91.           parts = parts.split("-");
  92.           $target.val("");
  93.           $("#invite_section_one").val(parts[0] ? parts[0].substr(0, 4) : "");
  94.           $("#invite_section_two").val(parts[1] ? parts[1].substr(0, 4) : "");
  95.           $("#invite_section_three").val(parts[2] ? parts[2].substr(0, 4) : "");
  96.           $("#invite_section_four").val(parts[3] ? parts[3].substr(0, 4) : "").focus();
  97.           $target.attr("maxlength", 4);
  98.         }, 0);
  99.         $target.parents("form").find(".form-control").removeClass("invalid");
  100.       }), element.on("input", ".claim-invite input", function(dataAndEvents) {
  101.         var $target = $(this);
  102.         if ($target.val().length >= 4) {
  103.           var set = $target.parents(".claim-invite").find(":input");
  104.           var inputEl = set.eq(set.index(this) + 1);
  105.           inputEl.val("");
  106.           inputEl.focus();
  107.         }
  108.       }), element.on("keydown", ".claim-invite input", function(event) {
  109.         var $target = $(this);
  110.         if ($target.val().length <= 0 && 8 === event.keyCode) {
  111.           var set = $target.parents(".claim-invite").find(":input");
  112.           var submenu = set.eq(set.index(this) - 1);
  113.           event.preventDefault();
  114.           submenu.focus();
  115.         }
  116.       }), element.on("inputValidated.oneplus", ".js-send-code", function(dataAndEvents) {
  117.         var invite = ($(this), $("#invite_section_one").val() + "-" + $("#invite_section_two").val() + "-" + $("#invite_section_three").val() + "-" + $("#invite_section_four").val());
  118.         if (modules) {
  119.           if (modules.misc) {
  120.             if (modules.misc.load) {
  121.               modules.misc.load.start();
  122.             }
  123.           }
  124.         }
  125.         $.ajax({
  126.           type : "GET",
  127.           url : ONEPLUS_NET.REQUEST.CLAIM_INVITE,
  128.           data : {
  129.             invite : invite
  130.           },
  131.           dataType : "json",
  132.           /**
  133.            * @param {?} data
  134.            * @return {undefined}
  135.            */
  136.           success : function(data) {
  137.             if (modules) {
  138.               if (modules.misc) {
  139.                 if (modules.misc.load) {
  140.                   modules.misc.load.finish();
  141.                 }
  142.               }
  143.             }
  144.             if (0 === data.ret) {
  145.               window.location.reload();
  146.             } else {
  147.               if (modules.misc) {
  148.                 if (modules.misc.dialog) {
  149.                   modules.misc.dialog.show(data.errMsg);
  150.                 }
  151.               }
  152.             }
  153.           },
  154.           timeout : 3E4,
  155.           /**
  156.            * @param {?} textStatus
  157.            * @param {?} jqXHR
  158.            * @param {?} origError
  159.            * @return {undefined}
  160.            */
  161.           error : function(textStatus, jqXHR, origError) {
  162.             if (modules) {
  163.               if (modules.misc) {
  164.                 if (modules.misc.load) {
  165.                   modules.misc.load.finish();
  166.                 }
  167.               }
  168.             }
  169.           }
  170.         });
  171.       }), element.on("inputValidated.oneplus", ".js-btn-register-for-invite", function(dataAndEvents) {
  172.         var $target = $(this);
  173.         if (modules) {
  174.           if (modules.misc) {
  175.             if (modules.misc.load) {
  176.               modules.misc.load.start();
  177.             }
  178.           }
  179.         }
  180.         $.ajax({
  181.           type : "POST",
  182.           url : ONEPLUS_NET.REQUEST.CLAIM_INVITE,
  183.           data : {
  184.             email : $target.parents("form").find("[name=email]").val()
  185.           },
  186.           dataType : "jsonp",
  187.           jsonp : "jsonp",
  188.           /**
  189.            * @param {?} data
  190.            * @return {undefined}
  191.            */
  192.           success : function(data) {
  193.             if (modules) {
  194.               if (modules.misc) {
  195.                 if (modules.misc.load) {
  196.                   modules.misc.load.finish();
  197.                 }
  198.               }
  199.             }
  200.             if (0 === data.ret) {
  201.               $target.addClass("has-feedback");
  202.               etTimeout(function() {
  203.                 $target.removeClass("has-feedback");
  204.               }, 2E3);
  205.             } else {
  206.               if (modules.misc) {
  207.                 if (modules.misc.dialog) {
  208.                   modules.misc.dialog.show(data.errMsg);
  209.                 }
  210.               }
  211.             }
  212.           },
  213.           timeout : 3E4,
  214.           /**
  215.            * @param {?} textStatus
  216.            * @param {?} jqXHR
  217.            * @param {?} origError
  218.            * @return {undefined}
  219.            */
  220.           error : function(textStatus, jqXHR, origError) {
  221.             if (modules) {
  222.               if (modules.misc) {
  223.                 if (modules.misc.load) {
  224.                   modules.misc.load.finish();
  225.                 }
  226.               }
  227.             }
  228.           }
  229.         });
  230.       }), element.on("inputValidated.oneplus", ".js-share-via-email", function(dataAndEvents) {
  231.         var $target = $(this);
  232.         if (modules) {
  233.           if (modules.misc) {
  234.             if (modules.misc.load) {
  235.               modules.misc.load.start();
  236.             }
  237.           }
  238.         }
  239.         $.ajax({
  240.           type : "GET",
  241.           url : ONEPLUS_NET.REQUEST.SHARE_VIA_EMAIL,
  242.           data : {
  243.             invite : $target.parents("form").find(".invite-card .invite-code").text().replace(/ /g, "-"),
  244.             email : $("#shareEmail").val(),
  245.             message : $("#attachedMsg").val()
  246.           },
  247.           dataType : "json",
  248.           /**
  249.            * @param {?} data
  250.            * @return {undefined}
  251.            */
  252.           success : function(data) {
  253.             if (modules) {
  254.               if (modules.misc) {
  255.                 if (modules.misc.load) {
  256.                   modules.misc.load.finish();
  257.                 }
  258.               }
  259.             }
  260.             if (0 === data.ret) {
  261.               $target.addClass("has-feedback");
  262.               setTimeout(function() {
  263.                 $target.removeClass("has-feedback");
  264.               }, 2E3);
  265.             } else {
  266.               if (modules.misc) {
  267.                 if (modules.misc.dialog) {
  268.                   modules.misc.dialog.show(data.errMsg);
  269.                 }
  270.               }
  271.             }
  272.           },
  273.           timeout : 3E4,
  274.           /**
  275.            * @param {?} textStatus
  276.            * @param {?} jqXHR
  277.            * @param {?} origError
  278.            * @return {undefined}
  279.            */
  280.           error : function(textStatus, jqXHR, origError) {
  281.             if (modules) {
  282.               if (modules.misc) {
  283.                 if (modules.misc.load) {
  284.                   modules.misc.load.finish();
  285.                 }
  286.               }
  287.             }
  288.           }
  289.         });
  290.       }), {
  291.         AUTHOR : "Michael",
  292.         VERSION : "1.0.0",
  293.         LAST_MODIFIED : "2015-07-16",
  294.         $element : element,
  295.         /** @type {function (Object): undefined} */
  296.         countingDown : init
  297.       };
  298.     }();
  299.     window.modules = window.modules || {};
  300.     window.modules.invites = invites;
  301.   });
  302. }(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment