-
Notifications
You must be signed in to change notification settings - Fork 2k
/
slug-preview.js
78 lines (67 loc) · 2.15 KB
/
slug-preview.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
this.ckan.module('slug-preview-target', {
initialize: function () {
var sandbox = this.sandbox;
var options = this.options;
var el = this.el;
sandbox.subscribe('slug-preview-created', function (preview) {
// Append the preview string after the target input.
el.after(preview);
});
// Once the preview box is modified stop watching it.
sandbox.subscribe('slug-preview-modified', function () {
el.off('.slug-preview');
});
// Watch for updates to the target field and update the hidden slug field
// triggering the "change" event manually.
el.on('keyup.slug-preview', function (event) {
sandbox.publish('slug-target-changed', this.value);
//slug.val(this.value).trigger('change');
});
}
});
this.ckan.module('slug-preview-slug', function (jQuery, _) {
return {
options: {
prefix: '',
placeholder: '<slug>',
i18n: {
edit: _('Edit')
}
},
initialize: function () {
var sandbox = this.sandbox;
var options = this.options;
var el = this.el;
var _ = sandbox.translate;
var slug = el.slug();
var parent = slug.parents('.control-group');
var preview;
if (!(parent.length)) {
return;
}
// Leave the slug field visible
if (!parent.hasClass('error')) {
preview = parent.slugPreview({
prefix: options.prefix,
placeholder: options.placeholder,
i18n: {
'Edit': this.i18n('edit')
}
});
// If the user manually enters text into the input we cancel the slug
// listeners so that we don't clobber the slug when the title next changes.
slug.keypress(function () {
if (event.charCode) {
sandbox.publish('slug-preview-modified', preview[0]);
}
});
sandbox.publish('slug-preview-created', preview[0]);
}
// Watch for updates to the target field and update the hidden slug field
// triggering the "change" event manually.
sandbox.subscribe('slug-target-changed', function (value) {
slug.val(value).trigger('change');
});
}
};
});