Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var vg_currentWeb;
  2. var vg_folderUrl = '/vGoalFiles/';
  3. var vg_currentWebUrl;
  4. var vg_notification = null;
  5. var vg_clientContext;
  6. var vg_userTypesList;
  7.  
  8. function initialize() {
  9.     vg_clientContext = new SP.ClientContext();
  10.     var siteColl = vg_clientContext.get_site();
  11.     vg_currentWeb = siteColl.get_rootWeb();
  12.  
  13.     vg_userTypesList = vg_currentWeb.get_lists().getByTitle('vGoal User Types');
  14.  
  15.     vg_clientContext.load(vg_currentWeb);
  16.     vg_clientContext.executeQueryAsync(Function.createDelegate(this, getSiteCollectionUrl), null);
  17. }
  18.  
  19. function getSiteCollectionUrl() {
  20.     var w = vg_currentWeb.get_serverRelativeUrl();
  21.     if (w.toString().slice(-1) === "/") {
  22.         w = w.substr(0, w.length - 1);
  23.     }
  24.     if (w.toString().substr(0, 1) === "/") {
  25.         w = w.substr(1, w.length - 1);
  26.     }
  27.     var prot = window.location.protocol.toString();
  28.     if (prot.slice(-1) === ":") {
  29.         prot = prot.substr(0, prot.length - 1);
  30.     }
  31.     vg_folderUrl = prot + "://" + window.location.host + "/" + w + '/vGoalFiles/';
  32.     vg_currentWebUrl = prot + "://" + window.location.host + "/" + w;
  33.     addScriptsIfNeeded();
  34.     addCustomCss('css/vGoal.css');
  35.     addCustomCss('css/jquery.qtip.min.css');
  36.     addCustomCss('css/jquery.dataTables.css');
  37.     loadScript(vg_folderUrl + "jquery.qtip.min.js");
  38.     loadScript(vg_folderUrl + "jquery.dataTables.js");
  39.     jQuery(document).ready(function () {
  40.         jQuery('.vGoalTabs').tabs();
  41.     });
  42.     jQuery(document).ready(setUserTypesTooltip);
  43. }
  44.  
  45. function setUserTypesTooltip() {
  46.     var url = vg_currentWebUrl + "/_vti_bin/listdata.svc/VGoalUserTypes";
  47.     url += '?$select=Id,Team1Score,Team2Score,CreatedBy/Name,MatchId,Points';
  48.     url += '&$expand=CreatedBy';
  49.     url += '&$filter=MatchId%20eq%20';
  50.  
  51.     jQuery('[vgDetailsId]').each(function () {
  52.         var localUrl = url + jQuery(this).attr('vgDetailsId');
  53.         jQuery(this).qtip(
  54.             {
  55.                 content: {
  56.                     text: '<img class="throbber" src="' + vg_folderUrl + 'css/images/throbber.gif" alt="Loading..." />',
  57.                     ajax: {
  58.                         url: localUrl,
  59.                         dataType: 'json',
  60.                         once: false,
  61.                         success: function (json) {
  62.                             var content = "";
  63.                             if (json.d != undefined && json.d.results != undefined && json.d.results.length > 0) {
  64.                                 content += "<ul>";
  65.                                 var r = json.d.results;
  66.                                 for (var i = 0; i < r.length; i++) {
  67.                                     content += "<li>" + r[i].Team1Score + " : " + r[i].Team2Score + " by " + r[i].CreatedBy.Name;
  68.                                     if (r[i].Points != null) {
  69.                                         content += " (" + r[i].Points + ")";
  70.                                     }
  71.                                     content += "</li>";
  72.                                 }
  73.                                 content += "</ul>";
  74.                             } else {
  75.                                 content += "No types yet. Be the first one!";
  76.                             }
  77.                             this.set('content.text', content);
  78.                         }
  79.                     }
  80.                 },
  81.                 position: {
  82.                     my: 'left center',
  83.                     at: 'right center',
  84.                     viewport: jQuery(window),
  85.                     effect: false
  86.                 },
  87.                 show: {
  88.                     solo: true
  89.                 },
  90.                 style: {
  91.                     classes: 'ui-tooltip-userTypes ui-tooltip-light ui-tooltip-shadow'
  92.                 }
  93.             });
  94.     });
  95. }
  96.  
  97. function hideSummaryView() {
  98.     jQuery('.vg-summary').css('display', 'none');
  99.     jQuery('.vg-summaryImgShow').css('display', '');
  100.     jQuery('.vg-summaryImgHide').css('display', 'none');
  101. }
  102.  
  103. function buildSummaryView() {
  104.     jQuery('.vg-summary').css('display', '');
  105.     jQuery('.vg-summaryImgShow').css('display', 'none');
  106.     jQuery('.vg-summaryImgHide').css('display', '');
  107.  
  108.     var url = vg_currentWebUrl + "/_vti_bin/listdata.svc/VGoalPointsSummary";
  109.     url += '?$select=Title,Points,Typer/Name&$expand=Typer&$orderby=Points';
  110.     jQuery('#vg-summaryTable').dataTable({
  111.         "bProcessing": true,
  112.         "sAjaxSource": url,
  113.         "bDestroy": true,
  114.         "fnServerData": function (sSource, aoData, fnCallback) {
  115.             $.ajax({
  116.                 "dataType": 'json',
  117.                 "type": "GET",
  118.                 "url": url,
  119.                 "data": aoData,
  120.                 "success": function (json) {
  121.                     var content = '{ "aaData": [ ';
  122.                     if (json.d != undefined && json.d.results != undefined && json.d.results.length > 0) {
  123.                         var r = json.d.results;
  124.                         var j = 1;
  125.                         for (var i = r.length - 1; i >= 0; i--) {
  126.                             content += '["' + j + '","' + r[i].Typer.Name.replace('\\', ' ') + '","' + r[i].Points + '"],';
  127.                             j++;
  128.                         }
  129.                         content = content.substr(0, content.length - 1);
  130.                     }
  131.                     content += " ] }";
  132.                     fnCallback(jQuery.parseJSON(content));
  133.                 }
  134.             });
  135.         }
  136.     });
  137. }
  138.  
  139. function summaryTyableDataCallBack(json) {
  140.     var i = json;
  141. }
  142.  
  143. function loadScript(url) {
  144.     var xmlhttp = new XMLHttpRequest();
  145.     xmlhttp.open('GET', url, false);
  146.     xmlhttp.send();
  147.     eval(xmlhttp.responseText);
  148.     var s = xmlhttp.responseText.split(/\n/);
  149.     var r = /^function\s*([a-z_]+)/i;
  150.     for (var i = 0; i < s.length; i++) {
  151.         var m = r.exec(s[i]);
  152.         if (m != null)
  153.             window[m[1]] = eval(m[1]);
  154.     }
  155. }
  156.  
  157. function addScriptsIfNeeded() {
  158.     try {
  159.         jQuery('.test');
  160.     } catch (e) {
  161.         loadScript(vg_folderUrl + 'jQuery.js');
  162.     }
  163.     try {
  164.         jQuery('.test').dialog().show();
  165.         jQuery('.test').tabs().show();
  166.     } catch (e) {
  167.         loadScript(vg_folderUrl + 'jQuery-ui.js');
  168.         loadScript(vg_folderUrl + 'jquery.scrollabletab.js');
  169.         // Load template css file
  170.         addCustomCss('css/jQuery-ui.css');
  171.     }
  172. }
  173.  
  174. function addCustomCss(url) {
  175.     var oLink = document.createElement('link');
  176.     oLink.href = vg_folderUrl + url;
  177.     oLink.rel = 'stylesheet';
  178.     oLink.type = 'text/css';
  179.     if (document.body != null && document.body != undefined) {
  180.         document.body.appendChild(oLink);
  181.     }
  182.     else {
  183.         jQuery(document).ready(function () {
  184.             document.body.appendChild(oLink);
  185.         });
  186.     }
  187. }
  188.  
  189. function updateUserGuess(matchId, date) {
  190.     if (new Date().getTime() - Date.parse(date) > 0) {
  191.         SP.UI.Notify.addNotification("<font color='#FF0000'>Sorry, you can't make your types on this match anymore...</font> ", false);
  192.         return;
  193.     }
  194.     showNofication("Looking for last user guess...");
  195.  
  196.     var working = jQuery("#vGoalBetDiv").contents();
  197.     if (working.length == 0) {
  198.         var temp = jQuery("#vGoalBetInnerDiv").parent();
  199.         working = jQuery(temp).contents();
  200.     }
  201.  
  202.     var ref = jQuery("[vgInputPanel='" + matchId + "']").contents();
  203.  
  204.     jQuery("#vGoalBetDiv").append(ref);
  205.     jQuery("[vgInputPanel='" + matchId + "']").append(working);
  206.  
  207.     var url = vg_currentWebUrl + "/_vti_bin/listdata.svc/VGoalUserTypes";
  208.     url += '?$select=Id,Team1Score,Team2Score,CreatedById,MatchId';
  209.     url += '&$filter=((MatchId eq ' + matchId + ') and (CreatedById eq ' + _spUserId + '))';
  210.  
  211.     var userType = null;
  212.  
  213.     try {
  214.         jQuery.ajaxSetup({ "async": false, "cache": false });
  215.         jQuery('#vGoalSendGuessImg').unbind('click');
  216.         jQuery("#vGoalCancelGuessImg").unbind('click');
  217.         var rx = jQuery.getJSON(url);
  218.         var js = rx.response;
  219.         if (js == undefined) {
  220.             js = rx.responseText; //ie!
  221.         }
  222.         var guesses = jQuery.parseJSON(js);
  223.         jQuery("#vGoalCancelGuessImg").click(function() {
  224.             hideVGoalBetDiv(matchId);
  225.             removeNofication();
  226.         });
  227.         if (guesses.d != undefined && guesses.d.results != undefined && guesses.d.results.length > 0) {
  228.             userType = guesses.d.results[0];
  229.             jQuery('#vGoalLeftTeamSelect').val(userType.Team1Score);
  230.             jQuery('#vGoalRightTeamSelect').val(userType.Team2Score);
  231.             jQuery('#vGoalSendGuessImg').click(function () {
  232.                 sendGuessToSharepoint(matchId, userType.Id);
  233.             });
  234.         }
  235.         else {
  236.             jQuery('#vGoalLeftTeamSelect').val(0);
  237.             jQuery('#vGoalRightTeamSelect').val(0);
  238.             jQuery('#vGoalSendGuessImg').click(function () {
  239.                 sendGuessToSharepoint(matchId, 0);
  240.             });
  241.         }
  242.     } finally {
  243.         jQuery.ajaxSetup({ "async": true, "cache": true });
  244.     }
  245.  
  246.     removeNofication();
  247. }
  248.  
  249. function sendGuessToSharepoint(matchId, guessId) {
  250.     showNofication("Updating user guess...");
  251.     var oListItem = null;
  252.     if (guessId == 0) {
  253.         var itemCreateInfo = new SP.ListItemCreationInformation();
  254.         oListItem = vg_userTypesList.addItem(itemCreateInfo);
  255.     }
  256.     else {
  257.         oListItem = vg_userTypesList.getItemById(guessId);
  258.     }
  259.     oListItem.set_item('Title', 'Guess');
  260.     var newLookupField = new SP.FieldLookupValue();
  261.     newLookupField.set_lookupId(matchId);
  262.     oListItem.set_item('Match', newLookupField);
  263.     oListItem.set_item('Team_x0020_1_x0020_Score', jQuery('#vGoalLeftTeamSelect').val());
  264.     oListItem.set_item('Team_x0020_2_x0020_Score', jQuery('#vGoalRightTeamSelect').val());
  265.     oListItem.set_item('Points', 0);
  266.  
  267.     oListItem.update();
  268.  
  269.     vg_clientContext.load(oListItem);
  270.  
  271.     hideVGoalBetDiv(matchId);
  272.  
  273.     vg_clientContext.executeQueryAsync(Function.createDelegate(this, onUpdateUserTypeSucceeded), Function.createDelegate(this, onUpdateUserTypeFailed));
  274. }
  275.  
  276. function onUpdateUserTypeSucceeded() {
  277.     removeNofication();
  278. }
  279.  
  280. function onUpdateUserTypeFailed(sender, args) {
  281.     removeNofication();
  282.     alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
  283. }
  284.  
  285. function showNofication(info) {
  286.     if (vg_notification == null) {
  287.         vg_notification = SP.UI.Notify.addNotification("<font color='#AA0000'>" + info + "</font> <img src='/_Layouts/Images/kpiprogressbar.gif' align='absmiddle'> ", true);
  288.     }
  289. }
  290.  
  291. function removeNofication() {
  292.     if (vg_notification != null) {
  293.         SP.UI.Notify.removeNotification(vg_notification);
  294.         vg_notification = null;
  295.     }
  296. }
  297.  
  298. function hideVGoalBetDiv(matchId) {
  299.     var working = jQuery("#vGoalBetDiv").contents(); //empty content;
  300.     var ref = jQuery("[vgInputPanel='" + matchId + "']").contents(); //input form
  301.  
  302.     jQuery("#vGoalBetDiv").append(ref);
  303.     jQuery("[vgInputPanel='" + matchId + "']").append(working);
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement