-
Notifications
You must be signed in to change notification settings - Fork 4
/
angular-sharepoint.js
108 lines (101 loc) · 4.05 KB
/
angular-sharepoint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// DIRECTIVE IDEAS
// - <spdialog>
// SERVICE IDEAS
// - perform REST get commands with selects, filters, etc.
// - perform JSOM for taxonomy & things REST can't do.
// - provide the same syntax to user so they don't care if it's REST or JSOM
(function (window, angular, undefined) {
'use strict';
var sp = angular.module('ngSP', []);
sp.service('$spUtilityServices', function () {
var self = this;
self.getQueryStringParameter = function (paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
self.getScripts = function (base, libraries, index, exec) {
if (index === libraries.length) {
exec();
}
else {
var lib = libraries[index];
index++;
$.getScript(base + lib, function () {
this.getScripts(base, libraries, index, exec);
});
}
}
self.logError = function (jqXHR, textStatus, errorThrown) {
if (jqXHR.hasOwnProperty('responseJSON')) {
console.error(jqXHR.responseJSON.error.message.value);
}
else {
console.error(jqXHR.responseText);
}
}
self.ajaxQuery = function (url) {
var d = $.Deferred();
$.ajax({
url: url,
method: "GET",
async: false,
headers: {
"Accept": "application/json; odata=verbose"
},
success: function (data) {
d.resolve(data);
},
error: function (jqXHR, textStatus, errorThrown) {
self.logError(jqXHR, textStatus, errorThrown);
}
});
return d.promise();
}
});
sp.service('$spService', ['$spConstants', '$spUtilityServices', function ($spConstants, $spUtilityServices) {
var self = this;
self.lists = {
outurl: $spConstants.appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists?" +
"@TargetSite='" + $spConstants.hostWebUrl + "'",
getByTitle:function(listtitle){
this.outurl = this.outurl.replace("lists?", "lists/getByTitle(@TargetList)?");
this.outurl += "&@TargetList='" + listtitle + "'";
return this;
},
getByBaseTemplateType: function (id) {
this.outurl = this.outurl.replace("?", "?$filter=BaseTemplate eq " + id + "&");
return this;
},
getTaskLists: function(){
return this.getByBaseTemplateType('171');
},
getItems: function(){
this.outurl = this.outurl.replace("?", "/items?");
return this;
},
getFieldByName: function (field) {
this.outurl = this.outurl.replace("?", "/fields?$select=Choices&$filter=Title eq '" + field + "'&");
return this;
},
value: function () {
var fetch = this.outurl;
this.outurl = $spConstants.appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists?" +
"@TargetSite='" + $spConstants.hostWebUrl + "'";
return $spUtilityServices.ajaxQuery(fetch);
}
};
}]);
sp.factory('$spConstants', function ($spUtilityServices) {
var obj = {};
obj.hostWebUrl = decodeURIComponent($spUtilityServices.getQueryStringParameter('SPHostUrl'));
obj.appWebUrl = decodeURIComponent($spUtilityServices.getQueryStringParameter('SPAppWebUrl'));
obj.scriptBase = obj.hostWebUrl + "/_layouts/15/";
return obj;
});
})(window, window.angular);