-
Notifications
You must be signed in to change notification settings - Fork 2
/
wpldp.js
316 lines (279 loc) · 12.8 KB
/
wpldp.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
window.wpldp = function( store, options ) {
this.options = options || {};
this.store = store;
this.resultSet = [];
Handlebars.logger.level = 0;
this.current_site_url = site_rest_url + 'ldp/v1/sites/';
this.init = function() {
jQuery('input:radio[name="tax_input[ldp_container][]"]').click(function() {
//TODO: Switch to a smooth AJAX call for changing the container, instead of reloading the page.
var form = jQuery('#post');
form.submit();
});
jQuery('input').keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
jQuery('#post').submit();
}
});
this.bindEvents();
this.loadData();
}
this.loadData = function () {
var instance = this;
this.store.list( instance.current_site_url ).then( function( sites ) {
sites.forEach( function( site ) {
this.store.list( site['@id'] ).then( function( containers ) {
console.log('TOTOT');
containers.forEach( function( container ) {
console.log('Contianer', container );
this.store.list( container['@id'] ).then( function( resources ) {
console.log( 'Resources', resources );
resources.forEach( function( resource ) {
instance.resultSet.push( resource );
console.log( instance.resultSet );
});
});
});
});
});
});
}
this.bindEvents = function() {
var instance = this;
jQuery('.remove-field-button').click( function( event ) {
instance.removeField( event );
});
jQuery('input[type=date]').datepicker();
var topics = [];
jQuery("#ldpform").on('focus', '.ldpLookup', function(event) {
jQuery(this).autocomplete({
autoFocus: true,
minlength: 3,
search: function() {
jQuery(this).addClass('sf-suggestion-search')
},
open: function() {
jQuery(this).removeClass('sf-suggestion-search')
},
select: function( event, ui ) {
var emptyFields = jQuery(this).siblings().filter(function(index) { return jQuery(this).val() == ''}).length;
},
source: function(request, callback) {
var searchResults = []
instance.resultSet.forEach( function( result ) {
if ( result['@type'] == event.target.parentNode.dataset.range ) {
searchResults.push( { 'label': result['foaf:name'], 'value': result['@id'] } );
}
});
callback( searchResults );
}
});
});
}
this.removeField = function removeField( event ) {
event.preventDefault();
event.stopPropagation();
var target_id = event.target.id.substring('remove-field-'.length);
var target_div = document.getElementById( target_id );
target_div.parentNode.removeChild( target_div );
event.target.parentNode.removeChild( event.target );
return false;
}
this.render = function render( div, objectIri, template, context, modelName, prefix ) {
var objectIri = this.store.getIri(objectIri);
var template = template ? template : this.store.mainTemplate;
var context = context || this.store.context;
var fields = modelName ? this.store.models[modelName].fields : null;
var instance = this;
this.store.get(objectIri).then(function(object) {
if (fields) {
fields.forEach( function(field) {
if (field.name) {
var propertyName = field.name;
} else if (field['data-property']) {
field.name = field['data-property'];
} else if (field['object-property']) {
field.name = field['object-property'];
}
if (prefix) {
propertyName = propertyName.replace(prefix, '');
}
if ( field.multiple == "true" ) {
if (object[field.name]) {
if ( Array.isArray(object[field.name])) {
field.fields = object[field.name];
} else {
field.fields = [ object[field.name] ];
}
} else {
field.fields = new Array();
}
} else {
field.fieldValue = object[field.name];
}
});
}
if (typeof(template) == 'string' && template.substring(0, 1) == '#') {
var element = jQuery(template);
if (element && typeof element.attr('src') !== 'undefined') {
instance.getTemplateAjax(element.attr('src'), function(template) {
jQuery(div).html(template({object: object}));
});
} else {
template = Handlebars.compile(element.html());
jQuery(div).html(template({object: object}));
}
} else {
template = Handlebars.compile(template);
jQuery(div).html(template({object: object}));
}
instance.bindEvents();
});
}
// Get handlebars templates via ajax
this.getTemplateAjax = function getTemplateAjax( path, callback ) {
var source, template;
jQuery.ajax({
url: path,
success: function (data) {
source = data;
template = Handlebars.compile(source);
if (callback) callback(template);
}
});
}
Handlebars.registerHelper("inc", function(value, options)
{
return parseInt(value) + 1;
});
// The partial definition for displaying a form field
var fieldPartialTest = "{{#this}}{{#if '@id'}}\
<button class='button remove-field-button' id='remove-field-{{parent.name}}{{inc @index}}'>-</button>\
<input data-range={{parent.range}} class='{{#ifCond parent.range 'resource'}}ldpLookup{{/ifCond}} {{#ifCond parent.hasLookup 'true'}}hasLookup{{/ifCond}}' id='{{parent.name}}{{inc @index}}' type='text' name='{{parent.name}}[]' value='{{'@id'}}' />\
{{/if}}{{/this}}";
Handlebars.registerPartial("LDPFieldTest", fieldPartialTest);
// The partial definition for displaying a form field handling array values, with possibility to add a field dynamically
var fieldDisplayPartial = "{{#if name}}<label for='{{name}}'>{{label}}</label>\
<button class='button add-field-button' id='add-field-{{name}}' onclick='return wpldp.addField(event);'>+</button>\
{{/if}}\
<div id='field-{{name}}' data-range={{range}} {{#ifCond type 'resource'}}data-ldp-lookup='true'{{/ifCond}} {{#ifCond hasLookup 'true'}}data-has-lookup='true'{{/ifCond}}>\
{{#if fields}}\
{{#each fields }}\
{{>LDPFieldTest this parent=../this}}\
{{/each}}\
{{else}} \
{{log type}}\
<input data-range={{range}} class='{{type}} {{#ifCond type 'resource'}}ldpLookup{{/ifCond}} {{#ifCond hasLookup 'true'}}hasLookup{{/ifCond}}' id='{{name}}' type='text' placeholder='{{title}}' name='{{name}}' />\
{{/if}}\
</div>";
Handlebars.registerPartial("ArrayFieldDisplay", fieldDisplayPartial);
this.addField = function addField( event ) {
event.stopPropagation();
event.preventDefault();
var target_id = event.target.id.substring('add-'.length);
var target_div = document.getElementById(target_id);
var child_count = target_div.childElementCount + 1;
var input = document.createElement('input');
input.id = target_id.substring('field-'.length) + child_count;
input.name = target_id.substring('field-'.length) + "[]";
input.type = "text";
if ( target_div.dataset && target_div.dataset.hasLookup == 'true' ) {
input.className += ' hasLookup';
} else if ( target_div.dataset && target_div.dataset.ldpLookup == 'true' ) {
input.className += ' ldpLookup';
}
var remove_button = document.createElement('button');
remove_button.id = "remove-field-" + target_id.substring('field-'.length) + child_count;
remove_button.className = "button remove-field-button";
remove_button.innerHTML = "-";
target_div.appendChild(remove_button);
target_div.appendChild(input);
this.bindEvents();
return false;
}
// The partial definition for displaying a form field
var fieldPartial = "{{#ifCond multiple 'true'}}{{> ArrayFieldDisplay }}\
{{else}}\
{{#if name}}<label for='{{name}}'>{{label}}</label>{{/if}} \
{{#ifCond type 'textarea'}} \
{{#if name}}<textarea id='{{name}}' name='{{name}}' rows='10'>{{#if fieldValue}}{{fieldValue}}{{/if}}</textarea><br/>{{/if}}\
{{else}}\
{{#ifCond type 'checkbox'}} \
{{#if name}}<input type='checkbox' name='{{name}}' id='{{name}}'/>{{/if}}\
{{else}}\
{{#ifCond type 'select'}} \
{{#if name}}<select id='{{name}}' name='{{name}}'>{{/if}}\
{{#each options}}{{> LDPOptions fieldValue='{{fieldValue}}' }}{{/each}} \
{{else}} \
{{#ifCond type 'date'}} \
{{#if name}}<input id='{{name}}' type='date' placeholder='YYYY-MM-DD' name='{{name}}' value='{{fieldValue}}' />{{/if}}\
{{else}} \
{{#ifCond type 'url'}} \
{{#if name}}<input id='{{name}}' type='url' placeholder='http://www.example.com' name='{{name}}' value='{{fieldValue}}' />{{/if}}\
{{else}} \
{{#ifCond type 'email'}} \
{{#if name}}<input id='{{name}}' type='email' placeholder='[email protected]' name='{{name}}' value='{{fieldValue}}' />{{/if}}\
{{else}} \
{{#ifCond type 'resource'}} \
<input id='{{name}}' type='url' placeholder='http://www.example.com/ldp/resource/my-resource/' name='{{name}}' value='{{fieldValue}}' />\
{{else}} \
{{#if name}}<input id='{{name}}' type='text' placeholder='{{title}}' name='{{name}}' value='{{fieldValue}}' />{{/if}}\
{{/ifCond}}\
{{/ifCond}}\
{{/ifCond}}\
{{/ifCond}}\
{{/ifCond}}\
{{/ifCond}}\
{{/ifCond}}\
{{/ifCond}}";
Handlebars.registerPartial("LDPField", fieldPartial);
// The partial definition for displaying an option field inside a select
var optionPartial = "{{#ifCond value fieldValue}} \
<option value='{{value}}' selected>{{name}}</option>\
{{else}}\
<option value='{{value}}'>{{name}}</option>\
{{/ifCond}}";
Handlebars.registerPartial("LDPOptions", optionPartial);
var formTemplate = Handlebars.compile(
"<form data-container='{{container}}' onSubmit='return store.handleSubmit(event);'> \
{{#each fields}}{{> LDPField }}{{/each}} \
<input type='submit' value='Post' /> \
</form>");
this.registerPartialFromFile = function registerPartialFromFile( partialName, partialTemplatePath ) {
jQuery.ajax({
url: partialTemplatePath,
success: function (data) {
Handlebars.registerPartial(partialName, data);
}
});
}
Handlebars.registerHelper("ldpeach", function(array, tagName, options) {
var id = "ldp-"+Math.round(new Date().getTime() + (Math.random() * 10000));
var objects = Array.isArray(array) ? array : [array];
objects.forEach(function(object) {
this.store.get(object, this.store.context).then(function(object) {
jQuery('#'+id).append(options.fn(object));
}.bind(this));
}.bind(this));
return '<'+ tagName +' id="'+id+'"></' + tagName + '>';
}.bind(this));
Handlebars.registerHelper('ldplist', function(obj) {
return obj['ldp:contains'];
});
Handlebars.registerHelper('ifCond', function(value, tester, options) {
if (value == tester) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
Handlebars.registerHelper('if', function(conditional, options) {
if(conditional) {
return options.fn(this);
}
});
Handlebars.registerHelper('form', function(context, options) {
return formTemplate(this.store.models[context]);
}.bind(this));
};