Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. /*
  4.  * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
  5.  * See LICENSE in the project root for license information.
  6.  */
  7. // The Office initialize function must be run each time a new page is loaded.
  8. Office.initialize = function (reason) {
  9.  
  10.  
  11.     var token = localStorage.getItem('token');
  12.     var company_id;
  13.     var project_id;
  14.     var sprint_id;
  15.     var id = [];
  16.     const apiUrl = "https://api.agileboard.me";
  17.     const corsAnywhereUrl = "https://cors-anywhere.herokuapp.com/";
  18.     const url = corsAnywhereUrl + apiUrl;
  19.     //const apiUrl = "https://api.cep.devpark.pl";
  20.     var description;
  21.  
  22.     var item = Office.context.mailbox.item;
  23.     var mailUrl = item.sender.emailAddress;
  24.     if (localStorage.getItem('token')) {
  25.         item.body.getAsync('html', function (result) {
  26.             if (result.status === 'succeeded') {
  27.                 description = result.value;
  28.             }
  29.         })
  30.         document.getElementById("name").value = item.subject;
  31.         token = localStorage.getItem('token');
  32.         Company()
  33.     }
  34.  
  35.  
  36.  // Logic: Company > Project > Sprint > Task
  37.     Office.onReady(info => {
  38.         if (info.host === Office.HostType.Outlook) {
  39.             document.getElementById("sideload-msg").style.display = "none";
  40.             document.getElementById("app-body").style.display = "flex";
  41.             document.getElementById("form-saving").onsubmit = ticket;
  42.             document.getElementById("form-update").onsubmit = update;
  43.             document.getElementById("form-login").onsubmit = login;
  44.             document.getElementById("logout").onclick = logout;
  45.             document.getElementById("Company").onchange = Project;
  46.             document.getElementById("Project").onchange = Sprint;
  47.             document.getElementById("Sprint").onchange = Task;
  48.             document.getElementById("select-action").onchange = setActive;
  49.             document.getElementById("select-action").onload = setActive;
  50.             $("#task-div").hide();
  51.         }
  52.     });
  53.  
  54.  
  55.     function login() {
  56.         $("#form-saving").show();
  57.  
  58.         item.body.getAsync('html', function (result) {
  59.             if (result.status === 'succeeded') {
  60.                 description = result.value;
  61.             }
  62.         })
  63.  
  64.         let email = document.getElementById("email").value;
  65.         let password = document.getElementById("password").value;
  66.  
  67.  
  68.         //Zalogowanie si� i pobranie tokena
  69.  
  70.         $.ajax({
  71.             url: apiUrl + '/auth',
  72.             dataType: 'json',
  73.             method: "POST",
  74.             headers: {
  75.                 'Access-Contol-Allow-Headers': '*',
  76.             },
  77.             data: {
  78.                 email: email,
  79.                 password: password
  80.             }
  81.         })
  82.             .done(function (res) {
  83.                 localStorage.setItem('token', res.data.token);
  84.                 token = localStorage.getItem('token');
  85.                 $(".Button:hover").css("cursor", "wait");
  86.                 $(".Button:focus").css("cursor", "wait");
  87.                 $("body").css("cursor", "wait");
  88.  
  89.                 Company();
  90.  
  91.             })
  92.             .fail(() => {
  93.                 $("#loginError").show();
  94.             });
  95.         return false;
  96.     }
  97.  
  98.     //Wyb�r firmy
  99.     function Company() {
  100.         $.ajax({
  101.             url: apiUrl + '/users/current/companies',
  102.             dataType: 'json',
  103.             headers: {
  104.                 Authorization: "Bearer " + token
  105.             }
  106.         })
  107.             .done(function (res) {
  108.                 $('#Company')
  109.                     .find('option')
  110.                     .remove()
  111.                     .end()
  112.                     .append('<option value="' + res.data[0]["id"] + '" selected>' + res.data[0]["name"] + '</option>');
  113.                 for (let i = 1; i < res.data.length; i++) {
  114.                     $('#Company')
  115.                         .append('<option value="' + res.data[i]["id"] + '" selected>' + res.data[i]["name"] + '</option>');
  116.                 }
  117.  
  118.                 $('#Company').find('option:selected').removeAttr('selected');
  119.                 company_id = localStorage.getItem('company_id');
  120.                 $("#Company option[value='" + company_id + "']").attr("selected", "selected");
  121.  
  122.                 Project();
  123.             })
  124.             .fail(() => {
  125.                 console.log("Company Error");
  126.             });
  127.         return false;
  128.     }
  129.  
  130.     //Wyb�r projektu
  131.     function Project() {
  132.         company_id = document.getElementById("Company").value;
  133.  
  134.         $.ajax({
  135.             url: apiUrl + '/projects?has_access=1&selected_company_id=' + company_id,
  136.             dataType: 'json',
  137.             headers: {
  138.                 Authorization: "Bearer " + token
  139.             }
  140.         })
  141.             .done(function (res) {
  142.                 $('#Project')
  143.                     .find('option')
  144.                     .remove()
  145.                     .end()
  146.                     .append('<option value="' + res.data[0]["id"] + '" selected>' + res.data[0]["name"] + '</option>');
  147.                 for (let i = 1; i < res.data.length; i++) {
  148.                     $('#Project')
  149.                         .append('<option value="' + res.data[i]["id"] + '" selected>' + res.data[i]["name"] + '</option>');
  150.                 }
  151.  
  152.                 $('#Project option').removeAttr('selected');
  153.                 project_id = localStorage.getItem('project_id');
  154.                 $("#Project option[value='" + project_id + "']").attr("selected", "selected");
  155.  
  156.                 Sprint()
  157.  
  158.             })
  159.             .fail(() => {
  160.                 console.log("Project error");
  161.             });
  162.         return false;
  163.     }
  164.  
  165.     //Wyb�r sprintu
  166.     function Sprint() {
  167.         project_id = document.getElementById("Project").value;
  168.         $.ajax({
  169.             url: apiUrl + '/projects/' + project_id + '/sprints?status=not-closed&selected_company_id=' + company_id,
  170.             dataType: 'json',
  171.             headers: {
  172.                 Authorization: "Bearer " + token
  173.             },
  174.         })
  175.             .done(function (res) {
  176.                 $('#Sprint')
  177.                     .find('option')
  178.                     .remove()
  179.                     .end()
  180.                     .append('<option value="' + res.data[0]["id"] + '" selected>' + res.data[0]["name"] + '</option>');
  181.                 for (let i = 1; i < res.data.length; i++) {
  182.                     $('#Sprint')
  183.                         .append('<option value="' + res.data[i]["id"] + '">' + res.data[i]["name"] + '</option>')
  184.                 }
  185.                 $('#Sprint')
  186.                     .find('option')
  187.                     .removeAttr('selected');
  188.                 sprint_id = localStorage.getItem('sprint_id');
  189.                 $("#Sprint option[value='" + sprint_id + "']").attr("selected", "selected");
  190.                 if (document.getElementById('form-login').style !== "display: none;") {
  191.                     show();
  192.                 }
  193.                 Task();
  194.             })
  195.             .fail(() => {
  196.                 console.log("Sprint error");
  197.             });
  198.         return false;
  199.     }
  200.  
  201.     //Wybor taska 05.11
  202.     function Task() {
  203.         project_id = document.getElementById("Project").value;
  204.         sprint_id = document.getElementById("Sprint").value;
  205.  
  206.         $.ajax({
  207.  
  208.             url: apiUrl + '/projects/' + project_id + '/tickets?status=not-closed&selected_company_id=' + company_id + '&sprint_id=' + sprint_id,
  209.             //url: apiUrl + '/projects/' + project_id + '/tickets?status=not-closed?selected_company_id=' + company_id + '&sprint_id=' + sprint_id,
  210.             dataType: 'json',
  211.             method: "GET",
  212.             headers: {
  213.                 Authorization: "Bearer " + token
  214.             },
  215.         })
  216.             .done(function (res) {
  217.                 console.log(res.data[0].id);
  218.                 id = localStorage.getItem("id");
  219.                 $('#Task')
  220.                     .find('option')
  221.                     .remove()
  222.                     .end()
  223.                     .append('<option value="' + res.data[0]["id"] + '" selected>' + res.data[0]["name"] + '</option>');
  224.                 // append (?)    
  225.                 for (let i = 0; i < res.data.length; i++) {
  226.                     $('#Task')
  227.                         .append('<option value="' + res.data[i]["id"] + '">' + res.data[i]["name"] + '</option>');
  228.                     localStorage.setItem("id", JSON.stringify(res.data[i].id));
  229.                 }
  230.                 $('#Task')
  231.                     .find('option')
  232.                     .removeAttr('selected');
  233.                 id = localStorage.getItem('id');
  234.                 $("#Task option[value='" + id + "']").attr("selected", "selected");
  235.                 if (document.getElementById('form-login').style !== "display: none;") {
  236.                     show();
  237.                 }
  238.             })
  239.             .fail(() => {
  240.                 console.log("Task error");
  241.             });
  242.  
  243.         return false;
  244.     }
  245.  
  246.  
  247.     //Ukrycie ekranu logowania
  248.     function show() {
  249.         $(".formLogin").hide();
  250.         $("body").css("cursor", "default");
  251.         $(".Button:hover").css("cursor", "pointer");
  252.         $(".Button:focus").css("cursor", "pointer");
  253.         return false;
  254.     }
  255.  
  256.  
  257.     //Zaktualizowanie zadania
  258.     function update() {
  259.  
  260.         console.log("Update start");
  261.         localStorage.setItem('company_id', document.getElementById("Company").value);
  262.         company_id = localStorage.getItem('company_id');
  263.         localStorage.setItem('project_id', document.getElementById("Project").value);
  264.         project_id = localStorage.getItem('project_id');
  265.         localStorage.setItem('sprint_id', document.getElementById("Sprint").value);
  266.         sprint_id = localStorage.getItem('sprint_id');
  267.         id = $('#Task').val();
  268.  
  269.          $.ajax({
  270.              method: 'POST',
  271.              url: apiUrl + '/projects/' + project_id + '/comments?selected_company_id=' + company_id,
  272.  
  273.              data: {
  274.                  project_id: project_id,
  275.                  text: mailUrl + "<hr/>" + description,
  276.                  ticket_id: $('#Task').val(),
  277.              },
  278.              headers: {
  279.                  Authorization: "Bearer " + token,
  280.              }
  281.          })
  282.             .done(() => {
  283.                 $(".success").show();
  284.                 $("#form-saving").hide();
  285.                 $("#form-updating").hide();
  286.             })
  287.  
  288.             .fail(() => {
  289.                 console.log("Update error ");
  290.             });
  291.         return false;
  292.     }
  293.  
  294.     //Stworzenie zadania
  295.     function ticket() {
  296.         localStorage.setItem('company_id', document.getElementById("Company").value);
  297.         company_id = localStorage.getItem('company_id');
  298.         localStorage.setItem('project_id', document.getElementById("Project").value);
  299.         project_id = localStorage.getItem('project_id');
  300.         localStorage.setItem('sprint_id', document.getElementById("Sprint").value);
  301.         sprint_id = localStorage.getItem('sprint_id');
  302.  
  303.  
  304.         localStorage.setItem('id', document.getElementById("Task").value);
  305.         id = localStorage.getItem('id');
  306.  
  307.         var name = document.getElementById("name").value;
  308.  
  309.        
  310.         $.ajax({
  311.             url: apiUrl + '/projects/' + project_id + '/tickets',
  312.             dataType: 'json',
  313.             method: 'post',
  314.             headers: {
  315.                 Authorization: "Bearer " + token
  316.             },
  317.             data: {
  318.                 selected_company_id: company_id,
  319.  
  320.                 name: name,
  321.                 description: mailUrl + "<hr/>" + description,
  322.                 type_id: 2,
  323.                 estimate_time: 0,
  324.                 sprint_id: sprint_id
  325.             }
  326.         })
  327.             .done(() => {
  328.                 $(".success").show();
  329.                 $("#form-saving").hide();
  330.             })
  331.  
  332.             .fail(() => {
  333.                 console.log("Ticket error ");
  334.             });
  335.         return false;
  336.     }
  337.  
  338.  
  339.     function setActive() {
  340.         $("#select-action option:selected").addClass('active');
  341.         let active = $(".active").text();
  342.         let taskDiv = $("#task-div");
  343.         let Name = $("#Name");
  344.         if (active == "Dodaj zadanie") {
  345.             taskDiv.hide();
  346.             Name.show();
  347.         }
  348.         else {
  349.             taskDiv.show();
  350.             Name.hide();
  351.         }
  352.         console.log(active);
  353.         $("#select-action option:selected").removeClass('active');
  354.     }
  355.  
  356.     //W��czenie ekranu logowania
  357.     function logout() {
  358.         $(".success").hide();
  359.  
  360.         $(".formLogin").show();
  361.         localStorage.removeItem('token');
  362.         localStorage.removeItem('company_id');
  363.         localStorage.removeItem('project_id');
  364.         localStorage.removeItem('sprint_id');
  365.         localStorage.removeItem('id');
  366.     };
  367.  
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement