Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2375] Re-factor the slug preview to use the module system
- Loading branch information
Showing
4 changed files
with
46 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,58 @@ | ||
(function ($, __, window) { | ||
var target = $('#field-title'); | ||
var slug = $('#field-name').slug(); | ||
var override = false; | ||
this.ckan.module('slug-preview-target', function (mod) { | ||
mod.subscribe('slug-preview-created', function (preview) { | ||
// Append the preview string after the target input. | ||
mod.el.after(preview); | ||
}); | ||
|
||
// Once the preview box is modified stop watching it. | ||
mod.subscribe('slug-preview-modified', function () { | ||
mod.el.off('.slug-preview'); | ||
}); | ||
|
||
// Watch for updates to the target field and update the hidden slug field | ||
// triggering the "change" event manually. | ||
mod.el.on('keyup.slug-preview', function (event) { | ||
mod.publish('slug-target-changed', this.value); | ||
//slug.val(this.value).trigger('change'); | ||
}); | ||
}); | ||
|
||
this.ckan.module('slug-preview-slug', function (mod, options, __) { | ||
var slug = mod.el.slug(); | ||
var parent = slug.parents('.control-group'); | ||
var preview; | ||
|
||
if (!(target.length && parent.length)) { | ||
if (!(parent.length)) { | ||
return; | ||
} | ||
|
||
// Leave the slug field visible | ||
if (!parent.hasClass('error')) { | ||
preview = parent.slugPreview({ | ||
prefix: 'demo.datahub.io/dataset/', | ||
placeholder: '<dataset>', | ||
prefix: options.prefix, | ||
placeholder: options.placeholder, | ||
trans: { | ||
edit: __('Edit') | ||
} | ||
}); | ||
|
||
// Append the preview string after the target input. | ||
target.after(preview); | ||
mod.publish('slug-preview-created', preview[0]); | ||
} | ||
|
||
// Watch for updates to the target field and update the hidden slug field | ||
// triggering the "change" event manually. | ||
target.keyup(function (event) { | ||
slug.val(this.value).trigger('change'); | ||
mod.subscribe('slug-target-changed', function (value) { | ||
slug.val(value).trigger('change'); | ||
}); | ||
|
||
// 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) { | ||
target.off('keyup'); | ||
mod.publish('slug-preview-modified', preview[0]); | ||
} | ||
}); | ||
})(this.jQuery, this.ckan.trans, this); | ||
}, { | ||
prefix: '', | ||
placeholder: '<slug>' | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters