-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxonomyTree.js
More file actions
210 lines (174 loc) · 7.4 KB
/
TaxonomyTree.js
File metadata and controls
210 lines (174 loc) · 7.4 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Copyright (c) 2015 uneidel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
function GetTaxonomyTerms(termsetId)
{
var d = $.Deferred();
SP.SOD.loadMultiple(['sp.js'], function ()
{
SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
SP.SOD.loadMultiple(['sp.taxonomy.js'], function () {
var ctx = SP.ClientContext.get_current();
taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx),
termStore = taxonomySession.getDefaultSiteCollectionTermStore(),
termSet = termStore.getTermSet(termsetId),
terms = termSet.getAllTerms();
ctx.load(terms);
ctx.executeQueryAsync(function(){ d.resolve(terms)}, function(err){d.reject("Strange things can happen.")});
});
});
return d.promise();
}
function GetGuidForCurrentPage()
{
var d = $.Deferred();
var baseUrl = window.location.protocol + "//" + window.location.host + "/" + _spPageContextInfo.siteServerRelativeUrl;
var steckbriefUrl = _spPageContextInfo.siteServerRelativeUrl + "/" + _spPageContextInfo.serverRequestPath;
$.ajax({
url: baseUrl + "/_api/web/getFileByServerRelativeUrl('" + _spPageContextInfo.siteServerRelativeUrl + "/pages/steckbrief.aspx')/ListItemAllFields",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) { d.resolve(data.d.bOrg)},
error: function (data) { d.reject("Strange things can happen. - Error:" + data.Status); }
});
return d.promise();
}
function CreateTermsTree(terms)
{
var tenum = terms.getEnumerator();
var tree = {
term: terms,
children: []
};
while(tenum.moveNext())
{
var curTerm = tenum.get_current();
var curTermPath = curTerm.get_pathOfTerm().split(';');
var children = tree.children;
for (var i =0; i < curTermPath.length;i++)
{
var foundNode = false;
for (var j = 0; j < children.length; j++)
{
if (children[j].name === curTermPath[i]) {
foundNode = true;
break;
}
}
var term = foundNode ? children[j] : { name: curTermPath[i], children: [] };
if (i === curTermPath.length - 1) {
term.term = curTerm;
term.title = curTerm.get_name();
term.guid = curTerm.get_id().toString();
term.url = curTerm.get_objectData().get_properties()["LocalCustomProperties"]["LocationUrl"];
}
// If the node did exist, let's look there next iteration
if (foundNode) {
children = term.children;
}
// If the segment of path does not exist, create it
else {
children.push(term);
// Reset the children pointer to add there next iteration
if (i !== curTermPath.length - 1) {
children = term.children;
}
}
}
}
tree = SortTermsFromTree(tree);
return tree;
}
function SortTermsFromTree(tree)
{
// Check to see if the get_customSortOrder function is defined. If the term is actually a term collection,
// there is nothing to sort.
if (tree.children.length && tree.term.get_customSortOrder) {
var sortOrder = null;
if (tree.term.get_customSortOrder()) {
sortOrder = tree.term.get_customSortOrder();
}
// If not null, the custom sort order is a string of GUIDs, delimited by a :
if (sortOrder) {
sortOrder = sortOrder.split(':');
tree.children.sort(function (a, b) {
var indexA = sortOrder.indexOf(a.guid);
var indexB = sortOrder.indexOf(b.guid);
if (indexA > indexB) {
return 1;
} else if (indexA < indexB) {
return -1;
}
return 0;
});
}
// If null, terms are just sorted alphabetically
else {
tree.children.sort(function (a, b) {
if (a.title > b.title) {
return 1;
} else if (a.title < b.title) {
return -1;
}
return 0;
});
}
}
for (var i = 0; i < tree.children.length; i++) {
tree.children[i] = SortTermsFromTree(tree.children[i]);
}
return tree;
}
function RenderTree(term)
{
var hasChildren = term.children && term.children.length;
var helperclass="";
var html="";
var html = "<li><a alt='" + term.guid + "' href='" +term.url + "' >" + term.title + "</a>";
if(hasChildren)
{
//TODO MAKE IT NICE
helperclass="parent";
html = "<li class='" + helperclass + "'><span class='open' style='cursor:pointer;padding-right:5px'>+</span><a alt='" + term.guid + "' href='" +term.url + "' >" + term.title + "</a>";
}
if (term.children && term.children.length) {
html += '<ul>';
for (var i = 0; i < term.children.length; i++) {
html += RenderTree(term.children[i]);
}
html += '</ul>';
}
return html + '</li>';
}
$(function () {
$.when(GetTaxonomyTerms('5dcba49c-ce1e-4503-bf0e-779a0df59c2b'), GetGuidForCurrentPage()).done(function(terms, termguid)
{
debugger;
var tree = CreateTermsTree(terms);
var html ='';
for (var i = 0; i < tree.children.length; i++) {
html += RenderTree(tree.children[i]);
}
$("#OrgChart").html(html);
$( '.tree li.parent > span' ).click( function( ) {
$( this ).parent().toggleClass( 'active' );
$( this ).parent().children( 'ul' ).slideToggle( 'fast' );
});
$(".tree").find("a[alt='"+ termguid.TermGuid + "']").css("font-weight", "bold").parents("ul").slideToggle("fast");
});
});