-
Notifications
You must be signed in to change notification settings - Fork 2k
/
application.js
946 lines (850 loc) · 28.9 KB
/
application.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
(function ($) {
$(document).ready(function () {
CKAN.Utils.setupUserAutocomplete($('input.autocomplete-user'));
CKAN.Utils.setupAuthzGroupAutocomplete($('input.autocomplete-authzgroup'));
CKAN.Utils.setupPackageAutocomplete($('input.autocomplete-dataset'));
CKAN.Utils.setupTagAutocomplete($('input.autocomplete-tag'));
$('input.autocomplete-format').live('keyup', function(){
CKAN.Utils.setupFormatAutocomplete($(this));
});
CKAN.Utils.setupMarkdownEditor($('.markdown-editor'));
// set up ckan js
var config = {
endpoint: CKAN.SITE_URL + '/'
};
var client = new CKAN.Client(config);
// serious hack to deal with hacky code in ckanjs
CKAN.UI.workspace = {
client: client
};
var isFrontPage = $('body.index.home').length > 0;
if (isFrontPage) {
CKAN.Utils.setupWelcomeBanner($('.js-welcome-banner'));
}
var isDatasetView = $('body.package.read').length > 0;
if (isDatasetView) {
// Show extract of notes field
CKAN.Utils.setupDatasetViewNotesExtract();
}
var isResourceView = $('body.package.resource_read').length > 0;
if (isResourceView) {
CKANEXT.DATAPREVIEW.loadPreviewDialog(preload_resource);
}
var isDatasetNew = $('body.package.new').length > 0;
if (isDatasetNew) {
// Set up magic URL slug editor
CKAN.Utils.setupUrlEditor('package');
$('#save').val(CKAN.Strings.addDataset);
$("#title").focus();
}
var isGroupNew = $('body.group.new').length > 0;
if (isGroupNew) {
// Set up magic URL slug editor
CKAN.Utils.setupUrlEditor('group');
$('#save').val(CKAN.Strings.addGroup);
$("#title").focus();
}
// Buttons with href-action should navigate when clicked
$('input.href-action').click(function(e) {
e.preventDefault();
window.location = ($(e.target).attr('action'));
});
var isDatasetEdit = $('body.package.edit').length > 0;
if (isDatasetEdit) {
CKAN.Utils.setupUrlEditor('package',readOnly=true);
// Selectively enable the upload button
var storageEnabled = $.inArray('storage',CKAN.plugins)>=0;
if (storageEnabled) {
$('li.js-upload-file').show();
}
// Set up hashtag nagivigation
CKAN.Utils.setupDatasetEditNavigation();
var _dataset = new CKAN.Model.Dataset(preload_dataset);
var $el=$('form#dataset-edit');
var view=new CKAN.View.DatasetEditForm({
model: _dataset,
el: $el
});
view.render();
}
var isGroupEdit = $('body.group.edit').length > 0;
if (isGroupEdit) {
CKAN.Utils.setupUrlEditor('group',readOnly=true);
}
});
}(jQuery));
var CKAN = CKAN || {};
CKAN.Utils = function($, my) {
my.flashMessage = function(msg, category) {
if (!category) {
category = 'info';
}
var messageDiv = $('<div />').html(msg).addClass(category).hide();
$('.flash-messages').append(messageDiv);
messageDiv.show(1200);
};
// Animate the appearance of an element by expanding its height
my.animateHeight = function(element, animTime) {
if (!animTime) animTime = 350;
element.show();
var finalHeight = element.height();
element.height(0);
element.animate({height:finalHeight}, animTime);
}
my.bindInputChanges = function(input, callback) {
input.keyup(callback);
input.keydown(callback);
input.keypress(callback);
input.change(callback);
};
my.setupWelcomeBanner = function(banner) {
var cookieName = 'ckan_killtopbar';
var isKilled = ($.cookie(cookieName)!=null);
if (!isKilled) {
banner.show();
// Bind to the close button
banner.find('.js-kill-button').live('click', function() {
$.cookie(cookieName, 'true', { expires: 365 });
banner.hide();
});
}
};
my.setupUrlEditor = function(slugType,readOnly) {
// Page elements to hook onto
var titleInput = $('.js-title');
var urlText = $('.js-url-text');
var urlSuffix = $('.js-url-suffix');
var urlInput = $('.js-url-input');
var validMsg = $('.js-url-is-valid');
if (titleInput.length==0) throw "No titleInput found.";
if (urlText.length==0) throw "No urlText found.";
if (urlSuffix.length==0) throw "No urlSuffix found.";
if (urlInput.length==0) throw "No urlInput found.";
if (validMsg.length==0) throw "No validMsg found.";
var api_url = CKAN.SITE_URL + '/api/2/util/is_slug_valid';
// (make length less than max, in case we need a few for '_' chars to de-clash slugs.)
var MAX_SLUG_LENGTH = 90;
var titleChanged = function() {
var lastTitle = "";
var regexToHyphen = [ new RegExp('[ .:/_]', 'g'),
new RegExp('[^a-zA-Z0-9-_]', 'g'),
new RegExp('-+', 'g')];
var regexToDelete = [ new RegExp('^-*', 'g'),
new RegExp('-*$', 'g')];
var titleToSlug = function(title) {
var slug = title;
$.each(regexToHyphen, function(idx,regex) { slug = slug.replace(regex, '-'); });
$.each(regexToDelete, function(idx,regex) { slug = slug.replace(regex, ''); });
slug = slug.toLowerCase();
if (slug.length<MAX_SLUG_LENGTH) {
slug=slug.substring(0,MAX_SLUG_LENGTH);
}
return slug;
};
// Called when the title changes
return function() {
var title = titleInput.val();
if (title == lastTitle) return;
lastTitle = title;
slug = titleToSlug(title);
urlInput.val(slug);
urlInput.change();
};
}();
var urlChanged = function() {
var timer = null;
var checkSlugValid = function(slug) {
$.ajax({
url: api_url,
data: 'type='+slugType+'&slug=' + slug,
dataType: 'jsonp',
type: 'get',
jsonpCallback: 'callback',
success: function (data) {
if (data.valid) {
validMsg.html('<span style="font-weight: bold; color: #0c0">'+CKAN.Strings.urlIsAvailable+'</span>');
} else {
validMsg.html('<span style="font-weight: bold; color: #c00">'+CKAN.Strings.urlIsNotAvailable+'</span>');
}
}
});
}
return function() {
slug = urlInput.val();
urlSuffix.html('<span>'+slug+'</span>');
if (timer) clearTimeout(timer);
if (slug.length<2) {
validMsg.html('<span style="font-weight: bold; color: #444;">'+CKAN.Strings.urlIsTooShort+'</span>');
}
else {
validMsg.html('<span style="color: #777;">'+CKAN.Strings.checking+'</span>');
timer = setTimeout(function () {
checkSlugValid(slug);
}, 200);
}
};
}();
if (readOnly) {
slug = urlInput.val();
urlSuffix.html('<span>'+slug+'</span>');
}
else {
var editLink = $('.js-url-editlink');
editLink.show();
// Hook title changes to the input box
my.bindInputChanges(titleInput, titleChanged);
my.bindInputChanges(urlInput, urlChanged);
// Set up the form
urlChanged();
editLink.live('click',function(e) {
e.preventDefault();
$('.js-url-viewmode').hide();
$('.js-url-editmode').show();
urlInput.select();
urlInput.focus();
});
}
}
// Attach dataset autocompletion to provided elements
//
// Requires: jquery-ui autocomplete
my.setupPackageAutocomplete = function(elements) {
elements.autocomplete({
minLength: 0,
source: function(request, callback) {
var url = '/dataset/autocomplete?q=' + request.term;
$.ajax({
url: url,
success: function(data) {
// atm is a string with items broken by \n and item = title (name)|name
var out = [];
var items = data.split('\n');
$.each(items, function(idx, value) {
var _tmp = value.split('|');
var _newItem = {
label: _tmp[0],
value: _tmp[1]
};
out.push(_newItem);
});
callback(out);
}
});
}
, select: function(event, ui) {
var input_box = $(this);
input_box.val('');
var parent_dd = input_box.parent('dd');
var old_name = input_box.attr('name');
var field_name_regex = /^(\S+)__(\d+)__(\S+)$/;
var split = old_name.match(field_name_regex);
var new_name = split[1] + '__' + (parseInt(split[2]) + 1) + '__' + split[3]
input_box.attr('name', new_name)
input_box.attr('id', new_name)
parent_dd.before(
'<input type="hidden" name="' + old_name + '" value="' + ui.item.value + '">' +
'<dd>' + ui.item.label + '</dd>'
);
}
});
};
// Attach tag autocompletion to provided elements
//
// Requires: jquery-ui autocomplete
my.setupTagAutocomplete = function(elements) {
elements
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 1,
source: function(request, callback) {
// here request.term is whole list of tags so need to get last
var _realTerm = request.term.split(',').pop().trim();
var url = CKAN.SITE_URL + '/api/2/util/tag/autocomplete?incomplete=' + _realTerm;
$.getJSON(url, function(data) {
// data = { ResultSet: { Result: [ {Name: tag} ] } } (Why oh why?)
var tags = $.map(data.ResultSet.Result, function(value, idx) {
return value.Name;
});
callback(
$.ui.autocomplete.filter(tags, _realTerm)
);
});
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = this.value.split(',');
// remove the current input
terms.pop();
// add the selected item
terms.push( " "+ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( " " );
this.value = terms.join( "," );
return false;
}
});
};
// Attach tag autocompletion to provided elements
//
// Requires: jquery-ui autocomplete
my.setupFormatAutocomplete = function(elements) {
elements.autocomplete({
minLength: 1,
source: function(request, callback) {
var url = CKAN.SITE_URL + '/api/2/util/resource/format_autocomplete?incomplete=' + request.term;
$.getJSON(url, function(data) {
// data = { ResultSet: { Result: [ {Name: tag} ] } } (Why oh why?)
var formats = $.map(data.ResultSet.Result, function(value, idx) {
return value.Format;
});
callback(formats);
});
}
});
};
// Attach user autocompletion to provided elements
//
// Requires: jquery-ui autocomplete
my.setupUserAutocomplete = function(elements) {
elements.autocomplete({
minLength: 2,
source: function(request, callback) {
var url = CKAN.SITE_URL + '/api/2/util/user/autocomplete?q=' + request.term;
$.getJSON(url, function(data) {
$.each(data, function(idx, userobj) {
var label = userobj.name;
if (userobj.fullname) {
label += ' [' + userobj.fullname + ']';
}
userobj.label = label;
userobj.value = userobj.name;
});
callback(data);
});
}
});
};
// Attach authz group autocompletion to provided elements
//
// Requires: jquery-ui autocomplete
my.setupAuthzGroupAutocomplete = function(elements) {
elements.autocomplete({
minLength: 2,
source: function(request, callback) {
var url = CKAN.SITE_URL + '/api/2/util/authorizationgroup/autocomplete?q=' + request.term;
$.getJSON(url, function(data) {
$.each(data, function(idx, userobj) {
var label = userobj.name;
userobj.label = label;
userobj.value = userobj.name;
});
callback(data);
});
}
});
};
my.setupMarkdownEditor = function(markdownEditor) {
// Markdown editor hooks
markdownEditor.find('button, div.markdown-preview').live('click', function(e) {
e.preventDefault();
var $target = $(e.target);
// Extract neighbouring elements
var markdownEditor=$target.closest('.markdown-editor')
markdownEditor.find('button').removeClass('depressed');
var textarea = markdownEditor.find('.markdown-input');
var preview = markdownEditor.find('.markdown-preview');
// Toggle the preview
if ($target.is('.js-markdown-preview')) {
$target.addClass('depressed');
raw_markdown=textarea.val();
preview.html("<em>"+CKAN.Strings.loading+"<em>");
$.post(CKAN.SITE_URL + "/api/util/markdown", { q: raw_markdown },
function(data) { preview.html(data); }
);
preview.width(textarea.width())
preview.height(textarea.height())
textarea.hide();
preview.show();
} else {
markdownEditor.find('.js-markdown-edit').addClass('depressed');
textarea.show();
preview.hide();
textarea.focus();
}
return false;
});
};
// If notes field is more than 1 paragraph, just show the
// first paragraph with a 'Read more' link that will expand
// the div if clicked
my.setupDatasetViewNotesExtract = function() {
var notes = $('#dataset div.notes');
if(notes.find('p').length > 1){
var extract = notes.children(':eq(0)');
var remainder = notes.children(':gt(0)');
notes.html($.tmpl(CKAN.Templates.datasetNotesField));
notes.find('#notes-extract').html(extract);
notes.find('#notes-remainder').html(remainder);
notes.find('#notes-remainder').hide();
notes.find('#dataset-notes-toggle a').click(function(event){
notes.find('#dataset-notes-toggle a').toggle();
var remainder = notes.find('#notes-remainder')
if ($(event.target).hasClass('more')) {
remainder.slideDown();
}
else {
remainder.slideUp();
}
})
}
};
// Show/hide fieldset sections from the edit dataset form.
my.setupDatasetEditNavigation = function() {
function showSection(sectionToShowId) {
$('.dataset-edit-form fieldset').hide();
$('.dataset-edit-form fieldset#'+sectionToShowId).show();
$('.dataset-edit-nav li a').removeClass('active');
$('.dataset-edit-nav li a[href=#section-'+sectionToShowId+']').addClass('active');
window.location.hash = 'section-'+sectionToShowId;
}
// Set up initial form state
// Prefix="#section-"
var initialSection = window.location.hash.slice(9) || 'basic-information';
showSection(initialSection);
// Adjust form state on click
$('.dataset-edit-nav li a').live('click', function(e) {
var $el = $(e.target);
// Prefix="#section-"
var showMe = $el.attr('href').slice(9);
showSection(showMe);
return false;
});
};
return my;
}(jQuery, CKAN.Utils || {});
CKAN.View.DatasetEditForm = Backbone.View.extend({
initialize: function() {
var resources = this.model.get('resources');
var $form = this.el;
var changesMade = function() {
var boundToUnload = false;
return function() {
if (!boundToUnload) {
CKAN.Utils.flashMessage(CKAN.Strings.youHaveUnsavedChanges,'notice');
boundToUnload = true;
window.onbeforeunload = function () {
return CKAN.Strings.youHaveUnsavedChanges;
};
}
}
}();
$form.find('input').live('change', function(e) {
$target = $(e.target);
// Entering text in the 'add' box does not represent a change
if ($target.closest('.resource-add').length==0) {
changesMade();
}
});
resources.bind('add', changesMade);
resources.bind('remove', changesMade);
$form.submit(function() {
// Don't stop us leaving
window.onbeforeunload = null;
});
// Table for editing resources
var $el = this.el.find('.js-resource-editor');
this.resourceList=new CKAN.View.ResourceEditList({
collection: resources,
el: $el
});
// Tabbed view for adding resources
var $el = this.el.find('.resource-add');
this.addView=new CKAN.View.ResourceAddTabs({
collection: resources,
el: $el
});
this.addView.render();
this.resourceList.render();
},
});
CKAN.View.ResourceEditList = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'addResource', 'removeResource');
this.collection.bind('add', this.addResource);
this.collection.bind('remove', this.removeResource);
this.collection.each(this.addResource);
},
nextIndex: function() {
var maxId=-1;
this.el.find('input').each(function(idx,input) {
var splitName=$(input).attr('name').split('__');
if (splitName.length>1) {
var myId = parseInt(splitName[1])
maxId = Math.max(myId, maxId);
}
});
return maxId+1;
},
addResource: function(resource) {
var position = this.nextIndex();
// Create a row from the template
var $tr = $('<tr />');
$tr.html($.tmpl(
CKAN.Templates.resourceEntry,
{ resource: resource.toTemplateJSON(),
num: position
}
));
$tr.find('.js-resource-edit-expanded').hide();
this.el.append($tr);
resource.view_tr = $tr;
// == Inner Function: Toggle the expanded options set == //
var toggleOpen = function(triggerEvent) {
if (triggerEvent) triggerEvent.preventDefault();
var animTime = 350;
var expandedTable = $tr.find('.js-resource-edit-expanded');
var finalHeight = expandedTable.height();
var icon = 'closed';
if (expandedTable.is(':visible')) {
expandedTable.animate(
{height:0},
animTime,
function() {
expandedTable.height(finalHeight);
expandedTable.hide();
}
);
}
else {
expandedTable.show();
expandedTable.height(0);
// Transition to its true height
expandedTable.animate({height:finalHeight}, animTime);
$tr.find('.js-resource-edit-name').focus();
icon = 'open';
}
$tr.find('.js-resource-edit-toggle').css("background-image", "url('/images/icons/arrow-"+icon+".gif')");
};
// == Inner Function: Delete the row == //
var collection = this.collection;
var deleteResource = function(triggerEvent) {
if (triggerEvent) triggerEvent.preventDefault();
confirmMessage = CKAN.Strings.deleteThisResourceQuestion;
resourceName = resource.attributes.name || CKAN.Strings.noNameBrackets;
confirmMessage = confirmMessage.replace('%name%', resourceName);
if (confirm(confirmMessage)) {
collection.remove(resource);
}
};
// == Inner Functions: Update the name as you type == //
var setName = function(newName) {
$link = $tr.find('.js-resource-edit-toggle');
newName = newName || ('<em>'+CKAN.Strings.noNameBrackets+'</em>');
// Need to structurally modify the DOM to force a re-render of text
$link.html('<ema>'+newName+'</span>');
};
var nameBoxChanged = function(e) {
setName($(e.target).val());
}
// Trigger animation
if (resource.isNew()) {
toggleOpen();
}
var nameBox = $tr.find('input.js-resource-edit-name');
CKAN.Utils.bindInputChanges(nameBox,nameBoxChanged);
$tr.find('.js-resource-edit-toggle').click(toggleOpen);
$tr.find('.js-resource-edit-delete').click(deleteResource);
// Initialise name
setName(resource.attributes.name);
},
removeResource: function(resource) {
if (resource.view_tr) {
resource.view_tr.remove();
delete resource.view_tr;
}
},
});
CKAN.View.ResourceAddTabs = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render', 'addNewResource', 'reset');
},
render: function() {
},
events: {
'click button': 'clickButton',
'click input[name=reset]': 'reset'
},
reset: function() {
this.el.find('button').removeClass('depressed');
if (this.subView != null) {
this.subView.remove();
this.subView = null;
}
return false;
},
clickButton: function(e) {
e.preventDefault();
var $target = $(e.target);
if ($target.is('.depressed')) {
this.reset();
}
else {
this.reset();
$target.addClass('depressed');
var $subPane = $('<div />').addClass('subpane');
this.el.append($subPane);
var tempResource = new CKAN.Model.Resource({});
tempResource.bind('change', this.addNewResource);
// Open sub-pane
if ($target.is('.js-upload-file')) {
this.subView = new CKAN.View.ResourceUpload({
el: $subPane,
model: tempResource,
// TODO: horrible reverse depedency ...
client: CKAN.UI.workspace.client
});
}
else if ($target.is('.js-link-file') || $target.is('.js-link-api')) {
this.subView = new CKAN.View.ResourceAddLink({
el: $subPane,
model: tempResource,
mode: ($target.is('.js-link-file'))? 'file' : 'api',
// TODO: horrible reverse depedency ...
client: CKAN.UI.workspace.client
});
}
this.subView.render();
}
},
addNewResource: function(tempResource) {
// Deep-copy the tempResource we had bound to
var resource=new CKAN.Model.Resource(tempResource.toJSON());
this.collection.add(resource);
this.reset();
}
});
CKAN.View.ResourceAddLink = Backbone.View.extend({
initialize: function(options) {
_.bindAll(this, 'render');
this.mode = options.mode;
},
render: function() {
if (this.mode=='file') {
var tmpl = $.tmpl(CKAN.Templates.resourceAddLinkFile);
}
else if (this.mode=='api') {
var tmpl = $.tmpl(CKAN.Templates.resourceAddLinkApi);
}
$(this.el).html(tmpl);
return this;
},
events: {
'submit form': 'setResourceInfo',
},
setResourceInfo: function(e) {
e.preventDefault();
var urlVal=this.el.find('input[name=url]').val();
this.model.set({url: urlVal, resource_type: this.mode})
}
});
(function ($) {
var my = {};
my.jsonpdataproxyUrl = 'http://jsonpdataproxy.appspot.com/';
my.dialogId = 'ckanext-datapreview';
my.$dialog = $('#' + my.dialogId);
// **Public: Loads a data preview**
//
// Fetches the preview data object from the link provided and loads the
// parsed data from the webstore displaying it in the most appropriate
// manner.
//
// link - Preview button.
//
// Returns nothing.
my.loadPreviewDialog = function(resourceData) {
my.$dialog.html('<h4>Loading ... <img src="http://assets.okfn.org/images/icons/ajaxload-circle.gif" class="loading-spinner" /></h4>');
function initializeDataExplorer(dataset) {
var dataExplorer = new recline.View.DataExplorer({
el: my.$dialog
, model: dataset
, config: {
readOnly: true
}
});
// will have to refactor if this can get called multiple times
Backbone.history.start();
}
// 4 situations
// a) have a webstore_url
// b) csv or xls (but not webstore)
// c) can be treated as plain text
// d) none of the above but worth iframing (assumption is
// that if we got here (i.e. preview shown) worth doing
// something ...)
resourceData.formatNormalized = my.normalizeFormat(resourceData.format);
resourceData.url = my.normalizeUrl(resourceData.url);
if (resourceData.formatNormalized === '') {
var tmp = resourceData.url.split('/');
tmp = tmp[tmp.length - 1];
tmp = tmp.split('?'); // query strings
tmp = tmp[0];
var ext = tmp.split('.');
if (ext.length > 1) {
resourceData.formatNormalized = ext[ext.length-1];
}
}
if (resourceData.webstore_url) {
var backend = new recline.Model.BackendWebstore({
url: resourceData.webstore_url
});
recline.Model.setBackend(backend);
var dataset = backend.getDataset();
initializeDataExplorer(dataset);
}
else if (resourceData.formatNormalized in {'csv': '', 'xls': ''}) {
var backend = new recline.Model.BackendDataProxy({
url: resourceData.url
, type: resourceData.formatNormalized
});
recline.Model.setBackend(backend);
var dataset = backend.getDataset();
initializeDataExplorer(dataset);
}
else if (resourceData.formatNormalized in {
'rdf+xml': '',
'owl+xml': '',
'xml': '',
'n3': '',
'n-triples': '',
'turtle': '',
'plain': '',
'atom': '',
'tsv': '',
'rss': '',
'txt': ''
}) {
// HACK: treat as plain text / csv
// pass url to jsonpdataproxy so we can load remote data (and tell dataproxy to treat as csv!)
var _url = my.jsonpdataproxyUrl + '?type=csv&url=' + resourceData.url;
my.getResourceDataDirect(_url, function(data) {
my.showPlainTextData(data);
});
}
else if (resourceData.formatNormalized in {'html':'', 'htm':''}
|| resourceData.url.substring(0,23)=='http://docs.google.com/') {
// we displays a fullscreen dialog with the url in an iframe.
my.$dialog.empty();
var el = $('<iframe></iframe>');
el.attr('src', resourceData.url);
el.attr('width', '100%');
el.attr('height', '100%');
my.$dialog.append(el);
}
else {
// Cannot reliably preview this item - with no mimetype/format information,
// can't guarantee it's not a remote binary file such as an executable.
var _msg = $('<p class="error">We are unable to preview this type of resource: ' + resourceData.formatNormalized + '</p>');
my.showError({
title: 'Unable to preview'
, message: _msg
});
}
};
// Public: Requests the formatted resource data from the webstore and
// passes the data into the callback provided.
//
// preview - A preview object containing resource metadata.
// callback - A Function to call with the data when loaded.
//
// Returns nothing.
my.getResourceDataDirect = function(url, callback) {
// $.ajax() does not call the "error" callback for JSONP requests so we
// set a timeout to provide the callback with an error after x seconds.
var timeout = 5000;
var timer = setTimeout(function error() {
callback({
error: {
title: 'Request Error',
message: 'Dataproxy server did not respond after ' + (timeout / 1000) + ' seconds'
}
});
}, timeout);
// have to set jsonp because webstore requires _callback but that breaks jsonpdataproxy
var jsonp = '_callback';
if (url.indexOf('jsonpdataproxy') != -1) {
jsonp = 'callback';
}
// We need to provide the `cache: true` parameter to prevent jQuery appending
// a cache busting `={timestamp}` parameter to the query as the webstore
// currently cannot handle custom parameters.
$.ajax({
url: url,
cache: true,
dataType: 'jsonp',
jsonp: jsonp,
success: function(data) {
clearTimeout(timer);
callback(data);
}
});
};
// Public: Displays a String of data in a fullscreen dialog.
//
// data - An object of parsed CSV data returned by the webstore.
//
// Returns nothing.
my.showPlainTextData = function(data) {
if(data.error) {
my.showError(data.error);
} else {
var content = $('<pre></pre>');
for (var i=0; i<data.data.length; i++) {
var row = data.data[i].join(',') + '\n';
content.append(my.escapeHTML(row));
}
my.$dialog.html(content);
}
};
my.showError = function (error) {
var _html = '<strong>' + $.trim(error.title) + '</strong><br />' + $.trim(error.message);
my.$dialog.html(_html);
};
my.normalizeFormat = function(format) {
var out = format.toLowerCase();
out = out.split('/');
out = out[out.length-1];
return out;
};
my.normalizeUrl = function(url) {
if (url.indexOf('https') === 0) {
return 'http' + url.slice(5);
} else {
return url;
}
}
// Public: Escapes HTML entities to prevent broken layout and XSS attacks
// when inserting user generated or external content.
//
// string - A String of HTML.
//
// Returns a String with HTML special characters converted to entities.
my.escapeHTML = function (string) {
return string.replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&')
.replace(/</g, '<').replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\//g,'/');
};
// Export the CKANEXT object onto the window.
$.extend(true, window, {CKANEXT: {}});
CKANEXT.DATAPREVIEW = my;
}(jQuery));