Mercurial > hg > cmmr2012-drupal-site
comparison core/misc/machine-name.js @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 /** | |
2 * DO NOT EDIT THIS FILE. | |
3 * See the following change record for more information, | |
4 * https://www.drupal.org/node/2815083 | |
5 * @preserve | |
6 **/ | |
7 | |
8 (function ($, Drupal, drupalSettings) { | |
9 Drupal.behaviors.machineName = { | |
10 attach: function attach(context, settings) { | |
11 var self = this; | |
12 var $context = $(context); | |
13 var timeout = null; | |
14 var xhr = null; | |
15 | |
16 function clickEditHandler(e) { | |
17 var data = e.data; | |
18 data.$wrapper.removeClass('visually-hidden'); | |
19 data.$target.trigger('focus'); | |
20 data.$suffix.hide(); | |
21 data.$source.off('.machineName'); | |
22 } | |
23 | |
24 function machineNameHandler(e) { | |
25 var data = e.data; | |
26 var options = data.options; | |
27 var baseValue = $(e.target).val(); | |
28 | |
29 var rx = new RegExp(options.replace_pattern, 'g'); | |
30 var expected = baseValue.toLowerCase().replace(rx, options.replace).substr(0, options.maxlength); | |
31 | |
32 if (xhr && xhr.readystate !== 4) { | |
33 xhr.abort(); | |
34 xhr = null; | |
35 } | |
36 | |
37 if (timeout) { | |
38 clearTimeout(timeout); | |
39 timeout = null; | |
40 } | |
41 if (baseValue.toLowerCase() !== expected) { | |
42 timeout = setTimeout(function () { | |
43 xhr = self.transliterate(baseValue, options).done(function (machine) { | |
44 self.showMachineName(machine.substr(0, options.maxlength), data); | |
45 }); | |
46 }, 300); | |
47 } else { | |
48 self.showMachineName(expected, data); | |
49 } | |
50 } | |
51 | |
52 Object.keys(settings.machineName).forEach(function (sourceId) { | |
53 var machine = ''; | |
54 var options = settings.machineName[sourceId]; | |
55 | |
56 var $source = $context.find(sourceId).addClass('machine-name-source').once('machine-name'); | |
57 var $target = $context.find(options.target).addClass('machine-name-target'); | |
58 var $suffix = $context.find(options.suffix); | |
59 var $wrapper = $target.closest('.js-form-item'); | |
60 | |
61 if (!$source.length || !$target.length || !$suffix.length || !$wrapper.length) { | |
62 return; | |
63 } | |
64 | |
65 if ($target.hasClass('error')) { | |
66 return; | |
67 } | |
68 | |
69 options.maxlength = $target.attr('maxlength'); | |
70 | |
71 $wrapper.addClass('visually-hidden'); | |
72 | |
73 if ($target.is(':disabled') || $target.val() !== '') { | |
74 machine = $target.val(); | |
75 } else if ($source.val() !== '') { | |
76 machine = self.transliterate($source.val(), options); | |
77 } | |
78 | |
79 var $preview = $('<span class="machine-name-value">' + options.field_prefix + Drupal.checkPlain(machine) + options.field_suffix + '</span>'); | |
80 $suffix.empty(); | |
81 if (options.label) { | |
82 $suffix.append('<span class="machine-name-label">' + options.label + ': </span>'); | |
83 } | |
84 $suffix.append($preview); | |
85 | |
86 if ($target.is(':disabled')) { | |
87 return; | |
88 } | |
89 | |
90 var eventData = { | |
91 $source: $source, | |
92 $target: $target, | |
93 $suffix: $suffix, | |
94 $wrapper: $wrapper, | |
95 $preview: $preview, | |
96 options: options | |
97 }; | |
98 | |
99 var $link = $('<span class="admin-link"><button type="button" class="link">' + Drupal.t('Edit') + '</button></span>').on('click', eventData, clickEditHandler); | |
100 $suffix.append($link); | |
101 | |
102 if ($target.val() === '') { | |
103 $source.on('formUpdated.machineName', eventData, machineNameHandler).trigger('formUpdated.machineName'); | |
104 } | |
105 | |
106 $target.on('invalid', eventData, clickEditHandler); | |
107 }); | |
108 }, | |
109 showMachineName: function showMachineName(machine, data) { | |
110 var settings = data.options; | |
111 | |
112 if (machine !== '') { | |
113 if (machine !== settings.replace) { | |
114 data.$target.val(machine); | |
115 data.$preview.html(settings.field_prefix + Drupal.checkPlain(machine) + settings.field_suffix); | |
116 } | |
117 data.$suffix.show(); | |
118 } else { | |
119 data.$suffix.hide(); | |
120 data.$target.val(machine); | |
121 data.$preview.empty(); | |
122 } | |
123 }, | |
124 transliterate: function transliterate(source, settings) { | |
125 return $.get(Drupal.url('machine_name/transliterate'), { | |
126 text: source, | |
127 langcode: drupalSettings.langcode, | |
128 replace_pattern: settings.replace_pattern, | |
129 replace_token: settings.replace_token, | |
130 replace: settings.replace, | |
131 lowercase: true | |
132 }); | |
133 } | |
134 }; | |
135 })(jQuery, Drupal, drupalSettings); |