Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var vg_currentWeb;
- var vg_folderUrl = '/vGoalFiles/';
- var vg_currentWebUrl;
- var vg_notification = null;
- var vg_clientContext;
- var vg_userTypesList;
- function initialize() {
- vg_clientContext = new SP.ClientContext();
- var siteColl = vg_clientContext.get_site();
- vg_currentWeb = siteColl.get_rootWeb();
- vg_userTypesList = vg_currentWeb.get_lists().getByTitle('vGoal User Types');
- vg_clientContext.load(vg_currentWeb);
- vg_clientContext.executeQueryAsync(Function.createDelegate(this, getSiteCollectionUrl), null);
- }
- function getSiteCollectionUrl() {
- var w = vg_currentWeb.get_serverRelativeUrl();
- if (w.toString().slice(-1) === "/") {
- w = w.substr(0, w.length - 1);
- }
- if (w.toString().substr(0, 1) === "/") {
- w = w.substr(1, w.length - 1);
- }
- var prot = window.location.protocol.toString();
- if (prot.slice(-1) === ":") {
- prot = prot.substr(0, prot.length - 1);
- }
- vg_folderUrl = prot + "://" + window.location.host + "/" + w + '/vGoalFiles/';
- vg_currentWebUrl = prot + "://" + window.location.host + "/" + w;
- addScriptsIfNeeded();
- addCustomCss('css/vGoal.css');
- addCustomCss('css/jquery.qtip.min.css');
- addCustomCss('css/jquery.dataTables.css');
- loadScript(vg_folderUrl + "jquery.qtip.min.js");
- loadScript(vg_folderUrl + "jquery.dataTables.js");
- jQuery(document).ready(function () {
- jQuery('.vGoalTabs').tabs();
- });
- jQuery(document).ready(setUserTypesTooltip);
- }
- function setUserTypesTooltip() {
- var url = vg_currentWebUrl + "/_vti_bin/listdata.svc/VGoalUserTypes";
- url += '?$select=Id,Team1Score,Team2Score,CreatedBy/Name,MatchId,Points';
- url += '&$expand=CreatedBy';
- url += '&$filter=MatchId%20eq%20';
- jQuery('[vgDetailsId]').each(function () {
- var localUrl = url + jQuery(this).attr('vgDetailsId');
- jQuery(this).qtip(
- {
- content: {
- text: '<img class="throbber" src="' + vg_folderUrl + 'css/images/throbber.gif" alt="Loading..." />',
- ajax: {
- url: localUrl,
- dataType: 'json',
- once: false,
- success: function (json) {
- var content = "";
- if (json.d != undefined && json.d.results != undefined && json.d.results.length > 0) {
- content += "<ul>";
- var r = json.d.results;
- for (var i = 0; i < r.length; i++) {
- content += "<li>" + r[i].Team1Score + " : " + r[i].Team2Score + " by " + r[i].CreatedBy.Name;
- if (r[i].Points != null) {
- content += " (" + r[i].Points + ")";
- }
- content += "</li>";
- }
- content += "</ul>";
- } else {
- content += "No types yet. Be the first one!";
- }
- this.set('content.text', content);
- }
- }
- },
- position: {
- my: 'left center',
- at: 'right center',
- viewport: jQuery(window),
- effect: false
- },
- show: {
- solo: true
- },
- style: {
- classes: 'ui-tooltip-userTypes ui-tooltip-light ui-tooltip-shadow'
- }
- });
- });
- }
- function hideSummaryView() {
- jQuery('.vg-summary').css('display', 'none');
- jQuery('.vg-summaryImgShow').css('display', '');
- jQuery('.vg-summaryImgHide').css('display', 'none');
- }
- function buildSummaryView() {
- jQuery('.vg-summary').css('display', '');
- jQuery('.vg-summaryImgShow').css('display', 'none');
- jQuery('.vg-summaryImgHide').css('display', '');
- var url = vg_currentWebUrl + "/_vti_bin/listdata.svc/VGoalPointsSummary";
- url += '?$select=Title,Points,Typer/Name&$expand=Typer&$orderby=Points';
- jQuery('#vg-summaryTable').dataTable({
- "bProcessing": true,
- "sAjaxSource": url,
- "bDestroy": true,
- "fnServerData": function (sSource, aoData, fnCallback) {
- $.ajax({
- "dataType": 'json',
- "type": "GET",
- "url": url,
- "data": aoData,
- "success": function (json) {
- var content = '{ "aaData": [ ';
- if (json.d != undefined && json.d.results != undefined && json.d.results.length > 0) {
- var r = json.d.results;
- var j = 1;
- for (var i = r.length - 1; i >= 0; i--) {
- content += '["' + j + '","' + r[i].Typer.Name.replace('\\', ' ') + '","' + r[i].Points + '"],';
- j++;
- }
- content = content.substr(0, content.length - 1);
- }
- content += " ] }";
- fnCallback(jQuery.parseJSON(content));
- }
- });
- }
- });
- }
- function summaryTyableDataCallBack(json) {
- var i = json;
- }
- function loadScript(url) {
- var xmlhttp = new XMLHttpRequest();
- xmlhttp.open('GET', url, false);
- xmlhttp.send();
- eval(xmlhttp.responseText);
- var s = xmlhttp.responseText.split(/\n/);
- var r = /^function\s*([a-z_]+)/i;
- for (var i = 0; i < s.length; i++) {
- var m = r.exec(s[i]);
- if (m != null)
- window[m[1]] = eval(m[1]);
- }
- }
- function addScriptsIfNeeded() {
- try {
- jQuery('.test');
- } catch (e) {
- loadScript(vg_folderUrl + 'jQuery.js');
- }
- try {
- jQuery('.test').dialog().show();
- jQuery('.test').tabs().show();
- } catch (e) {
- loadScript(vg_folderUrl + 'jQuery-ui.js');
- loadScript(vg_folderUrl + 'jquery.scrollabletab.js');
- // Load template css file
- addCustomCss('css/jQuery-ui.css');
- }
- }
- function addCustomCss(url) {
- var oLink = document.createElement('link');
- oLink.href = vg_folderUrl + url;
- oLink.rel = 'stylesheet';
- oLink.type = 'text/css';
- if (document.body != null && document.body != undefined) {
- document.body.appendChild(oLink);
- }
- else {
- jQuery(document).ready(function () {
- document.body.appendChild(oLink);
- });
- }
- }
- function updateUserGuess(matchId, date) {
- if (new Date().getTime() - Date.parse(date) > 0) {
- SP.UI.Notify.addNotification("<font color='#FF0000'>Sorry, you can't make your types on this match anymore...</font> ", false);
- return;
- }
- showNofication("Looking for last user guess...");
- var working = jQuery("#vGoalBetDiv").contents();
- if (working.length == 0) {
- var temp = jQuery("#vGoalBetInnerDiv").parent();
- working = jQuery(temp).contents();
- }
- var ref = jQuery("[vgInputPanel='" + matchId + "']").contents();
- jQuery("#vGoalBetDiv").append(ref);
- jQuery("[vgInputPanel='" + matchId + "']").append(working);
- var url = vg_currentWebUrl + "/_vti_bin/listdata.svc/VGoalUserTypes";
- url += '?$select=Id,Team1Score,Team2Score,CreatedById,MatchId';
- url += '&$filter=((MatchId eq ' + matchId + ') and (CreatedById eq ' + _spUserId + '))';
- var userType = null;
- try {
- jQuery.ajaxSetup({ "async": false, "cache": false });
- jQuery('#vGoalSendGuessImg').unbind('click');
- jQuery("#vGoalCancelGuessImg").unbind('click');
- var rx = jQuery.getJSON(url);
- var js = rx.response;
- if (js == undefined) {
- js = rx.responseText; //ie!
- }
- var guesses = jQuery.parseJSON(js);
- jQuery("#vGoalCancelGuessImg").click(function() {
- hideVGoalBetDiv(matchId);
- removeNofication();
- });
- if (guesses.d != undefined && guesses.d.results != undefined && guesses.d.results.length > 0) {
- userType = guesses.d.results[0];
- jQuery('#vGoalLeftTeamSelect').val(userType.Team1Score);
- jQuery('#vGoalRightTeamSelect').val(userType.Team2Score);
- jQuery('#vGoalSendGuessImg').click(function () {
- sendGuessToSharepoint(matchId, userType.Id);
- });
- }
- else {
- jQuery('#vGoalLeftTeamSelect').val(0);
- jQuery('#vGoalRightTeamSelect').val(0);
- jQuery('#vGoalSendGuessImg').click(function () {
- sendGuessToSharepoint(matchId, 0);
- });
- }
- } finally {
- jQuery.ajaxSetup({ "async": true, "cache": true });
- }
- removeNofication();
- }
- function sendGuessToSharepoint(matchId, guessId) {
- showNofication("Updating user guess...");
- var oListItem = null;
- if (guessId == 0) {
- var itemCreateInfo = new SP.ListItemCreationInformation();
- oListItem = vg_userTypesList.addItem(itemCreateInfo);
- }
- else {
- oListItem = vg_userTypesList.getItemById(guessId);
- }
- oListItem.set_item('Title', 'Guess');
- var newLookupField = new SP.FieldLookupValue();
- newLookupField.set_lookupId(matchId);
- oListItem.set_item('Match', newLookupField);
- oListItem.set_item('Team_x0020_1_x0020_Score', jQuery('#vGoalLeftTeamSelect').val());
- oListItem.set_item('Team_x0020_2_x0020_Score', jQuery('#vGoalRightTeamSelect').val());
- oListItem.set_item('Points', 0);
- oListItem.update();
- vg_clientContext.load(oListItem);
- hideVGoalBetDiv(matchId);
- vg_clientContext.executeQueryAsync(Function.createDelegate(this, onUpdateUserTypeSucceeded), Function.createDelegate(this, onUpdateUserTypeFailed));
- }
- function onUpdateUserTypeSucceeded() {
- removeNofication();
- }
- function onUpdateUserTypeFailed(sender, args) {
- removeNofication();
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
- function showNofication(info) {
- if (vg_notification == null) {
- vg_notification = SP.UI.Notify.addNotification("<font color='#AA0000'>" + info + "</font> <img src='/_Layouts/Images/kpiprogressbar.gif' align='absmiddle'> ", true);
- }
- }
- function removeNofication() {
- if (vg_notification != null) {
- SP.UI.Notify.removeNotification(vg_notification);
- vg_notification = null;
- }
- }
- function hideVGoalBetDiv(matchId) {
- var working = jQuery("#vGoalBetDiv").contents(); //empty content;
- var ref = jQuery("[vgInputPanel='" + matchId + "']").contents(); //input form
- jQuery("#vGoalBetDiv").append(ref);
- jQuery("[vgInputPanel='" + matchId + "']").append(working);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement