Advertisement
Guest User

BoxManager

a guest
Apr 28th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. (function(api, $, undefined){
  3.  
  4.  
  5.     var api = {},
  6.  
  7.         dom = {},
  8.  
  9.         data = {
  10.  
  11.             clientId: "m025a55gtov17txux1v2vbzjjhph2b6n",
  12.             clientSecret: "s7GIpGI4Kt0dE76SlGYzpasBxIShQPDg",
  13.             boxAuthorizationCode: "",
  14.             appState: "authenticate",
  15.             access_token: ""
  16.  
  17.         },
  18.  
  19.         setupAppState = function(){
  20.  
  21.             var authCode = getUrlParameter("code");
  22.             var isAuthCodeDefined = authCode !== null;
  23.  
  24.             if(isAuthCodeDefined){
  25.                 data.boxAuthorizationCode = authCode;
  26.                 data.appState = "authorize";
  27.             }
  28.  
  29.             // console.log(dom.appWrapper.attr("data-state"));
  30.             dom.appWrapper.attr("data-state", data.appState);
  31.  
  32.         },
  33.  
  34.         setupDom = function(){
  35.  
  36.             dom.document = $(document);
  37.             dom.appWrapper = $("#appWrapper");
  38.  
  39.             // console.log(dom);
  40.  
  41.         },
  42.  
  43.         addEventHandlers = function(){
  44.  
  45.             dom.document.on("click", "#start", function(){
  46.  
  47.                 window.location = "https://api.box.com/oauth2/authorize?client_id=" + data.clientId + "&response_type=code";
  48.  
  49.             });
  50.  
  51.             dom.document.on("click", "#continue", function(){
  52.  
  53.                 authorizeUser();
  54.  
  55.             });
  56.             // dom.document.on("click", "#submit", function(){
  57.             //     createFolders();
  58.             // });
  59.  
  60.         },
  61.  
  62.         getUrlParameter = function(name){
  63.  
  64.             return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
  65.        
  66.         },
  67.  
  68.         authorizeUser = function(){    
  69. /*        var XHR = new XMLHttpRequest();
  70.         var sendData = {
  71.  
  72.                     grant_type : 'authorization_code',
  73.                     code : data.boxAuthorizationCode,
  74.                     client_id : data.clientId,
  75.                     client_secret : data.clientSecret
  76.                 }
  77.  
  78.        
  79.  
  80.           // We define what will happen if the data is successfully sent
  81.           XHR.addEventListener('load', function(event) {
  82.             alert('Yeah! Data sent and response loaded.');
  83.           });
  84.  
  85.           // We define what will happen in case of error
  86.           XHR.addEventListener('error', function(event) {
  87.             alert('Oups! Something goes wrong.');
  88.           });
  89.  
  90.           // We setup our request
  91.           XHR.open('POST', 'https://www.box.com/api/oauth2/token');
  92.  
  93.           // We add the required HTTP header to handle a form data POST request
  94.           XHR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  95.          
  96.           XHR.setRequestHeader('Authorization', 'Bearer $token');
  97.           // And finally, We send our data.
  98.           XHR.send(sendData);
  99. */
  100.  
  101.          var results = $.ajax({
  102.                
  103.                 // The URL to process the request
  104.                 url : 'https://www.box.com/api/oauth2/token',
  105.                 type : 'POST',
  106.                 data : {
  107.                     grant_type : 'authorization_code',
  108.                     code : data.boxAuthorizationCode,
  109.                     client_id : data.clientId,
  110.                     client_secret : data.clientSecret
  111.                 },
  112.                 beforeSend: function (xhr) {
  113.       xhr.setRequestHeader("Authorization", "Bearer $token")
  114.     },
  115.                 dataType: "json",
  116.                 success: function(response) {
  117.                    //console.log(response);
  118.                    console.log(response.access_token);
  119.                    data.access_token = response.access_token;
  120.                    tokenGranted();
  121.                 }
  122.  
  123.             });
  124.              
  125.             return results.responseText;
  126.  
  127.         },
  128.         //If token request is successful, show "theForm" to allow users to enter Customer FolderName and Project Name
  129.         tokenGranted = function(){
  130.             alert("token granted!");
  131.             data.appState = "success";
  132.             dom.appWrapper.attr("data-state", data.appState);
  133.  
  134.         },
  135.         //POST Request to Box.com to create folders
  136.         createFolders = function(){
  137.             var customerFolder, projectName;
  138.             customerFolder = $("#customerFolder").val();
  139.             projectName = $("#projectName").val();
  140.             //Validation check to make sure user doesn't enter a blank field
  141.             console.log(customerFolder);
  142.             console.log(projectName);
  143.             var sendData = {
  144.                     name : customerFolder,
  145.                     parent : {"id": "0"}                   
  146.             }
  147.             $.ajax({
  148.                 // More info at http://developers.box.com/docs
  149.                 // The URL to process the request
  150.                 type : 'POST',
  151.                 url : 'https://api.box.com/2.0/folders',
  152.                 //headers: {},
  153.                 contentType: 'application/json',
  154.                 crossDomain: true,
  155.                 data : JSON.stringify(sendData),
  156.                 beforeSend: function (xhr) {
  157.                 xhr.setRequestHeader("Authorization", "Bearer " + data.access_token)
  158.                 },
  159.                 dataType: "json",
  160.                 success: function(response) {
  161.                    console.log(response);                  
  162.                 }
  163.             });
  164.  
  165.            
  166.             // Return false to prevent submission:
  167.             return false;
  168.         },
  169.  
  170.         myPrivateMethod = function(){
  171.  
  172.             alert("you called me!");
  173.  
  174.         },
  175.  
  176.         init = function(){
  177.  
  178.             setupDom();
  179.  
  180.             setupAppState();
  181.  
  182.             addEventHandlers();
  183.  
  184.             document.getElementById('theForm').onsubmit = createFolders;
  185.  
  186.         };
  187.  
  188.        
  189.  
  190.     api.myPublicMethod = function(){
  191.  
  192.         alert("you called me!");
  193.  
  194.     };
  195.  
  196.  
  197.     $(init);
  198.  
  199.  
  200. }(window.boxManager = window.boxManager || {}, jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement