##// END OF EJS Templates
deform: add deform.js to compiled scripts.js
dan -
r524:a0b14eeb default
parent child Browse files
Show More
@@ -0,0 +1,194 b''
1 /*
2 * Register a top-level callback to the deform.load() function
3 * this will be called when the DOM has finished loading. No need
4 * to include the call at the end of the page.
5 */
6
7 $(document).ready(function(){
8 deform.load();
9 });
10
11
12 var deform_loaded = false;
13
14 var deform = {
15 callbacks: [],
16
17 addCallback: function (oid, callback) {
18 deform.callbacks.push([oid, callback]);
19 },
20
21 clearCallbacks: function () {
22 deform.callbacks = [];
23 },
24
25 load: function() {
26 $(function() {
27 if (!deform_loaded) {
28 deform.processCallbacks();
29 deform.focusFirstInput();
30 deform_loaded = true;
31 }});
32 },
33
34
35 processCallbacks: function () {
36 $(deform.callbacks).each(function(num, item) {
37 var oid = item[0];
38 var callback = item[1];
39 callback(oid);
40 }
41 );
42 deform.clearCallbacks();
43 },
44
45 addSequenceItem: function (protonode, before) {
46 // - Clone the prototype node and add it before the "before" node.
47 // Also ensure any callbacks are run for the widget.
48
49 // In order to avoid breaking accessibility:
50 //
51 // - Find each tag within the prototype node with an id
52 // that has the string ``deformField(\d+)`` within it, and modify
53 // its id to have a random component.
54 // - For each label referencing an change id, change the label's
55 // for attribute to the new id.
56
57 var fieldmatch = /deformField(\d+)/;
58 var namematch = /(.+)?-[#]{3}/;
59 var code = protonode.attr('prototype');
60 var html = decodeURIComponent(code);
61 var $htmlnode = $(html);
62 var $idnodes = $htmlnode.find('[id]');
63 var $namednodes = $htmlnode.find('[name]');
64 var genid = deform.randomString(6);
65 var idmap = {};
66
67 // replace ids containing ``deformField`` and associated label for=
68 // items which point at them
69
70 $idnodes.each(function(idx, node) {
71 var $node = $(node);
72 var oldid = $node.attr('id');
73 var newid = oldid.replace(fieldmatch, "deformField$1-" + genid);
74 $node.attr('id', newid);
75 idmap[oldid] = newid;
76 var labelselector = 'label[for=' + oldid + ']';
77 var $fornodes = $htmlnode.find(labelselector);
78 $fornodes.attr('for', newid);
79 });
80
81 // replace names a containing ```deformField`` like we do for ids
82
83 $namednodes.each(function(idx, node) {
84 var $node = $(node);
85 var oldname = $node.attr('name');
86 var newname = oldname.replace(fieldmatch, "deformField$1-" + genid);
87 $node.attr('name', newname);
88 });
89
90 $htmlnode.insertBefore(before);
91
92 $(deform.callbacks).each(function(num, item) {
93 var oid = item[0];
94 var callback = item[1];
95 var newid = idmap[oid];
96 if (newid) {
97 callback(newid);
98 }
99 });
100
101 deform.clearCallbacks();
102 var old_len = parseInt(before.attr('now_len')||'0', 10);
103 before.attr('now_len', old_len + 1);
104 // we added something to the dom, trigger a change event
105 var e = jQuery.Event("change");
106 $('#deform').trigger(e);
107 },
108
109 appendSequenceItem: function(node) {
110 var $oid_node = $(node).closest('.deform-seq');
111 var $proto_node = $oid_node.find('.deform-proto').first();
112 var $before_node = $oid_node.find('.deform-insert-before').last();
113 var min_len = parseInt($before_node.attr('min_len')||'0', 10);
114 var max_len = parseInt($before_node.attr('max_len')||'9999', 10);
115 var now_len = parseInt($before_node.attr('now_len')||'0', 10);
116 var orderable = parseInt($before_node.attr('orderable')||'0', 10);
117
118 if (now_len < max_len) {
119 deform.addSequenceItem($proto_node, $before_node);
120 deform.processSequenceButtons($oid_node, min_len, max_len,
121 now_len + 1, orderable);
122 }
123 return false;
124 },
125
126 removeSequenceItem: function(clicked) {
127 var $item_node = $(clicked).closest('.deform-seq-item');
128 var $oid_node = $item_node.closest('.deform-seq');
129 var $before_node = $oid_node.find('.deform-insert-before').last();
130 var min_len = parseInt($before_node.attr('min_len')||'0', 10);
131 var max_len = parseInt($before_node.attr('max_len')||'9999', 10);
132 var now_len = parseInt($before_node.attr('now_len')||'0', 10);
133 var orderable = parseInt($before_node.attr('orderable')||'0', 10);
134 if (now_len > min_len) {
135 $before_node.attr('now_len', now_len - 1);
136 $item_node.remove();
137 deform.processSequenceButtons($oid_node, min_len, max_len,
138 now_len-1, orderable);
139 }
140 // we removed something from the dom, trigger a change event
141 var e = jQuery.Event("change");
142 $('#deform').trigger(e);
143 return false;
144 },
145
146 processSequenceButtons: function(oid_node, min_len, max_len, now_len,
147 orderable) {
148 orderable = !!orderable; // convert to bool
149 var has_multiple = now_len > 1;
150 var $ul = oid_node.find('.deform-seq-container').not(oid_node.find('.deform-seq-container .deform-seq-container'));
151 var $lis = $ul.find('.deform-seq-item').not($ul.find('.deform-seq-container .deform-seq-item'));
152 var show_closebutton = now_len > min_len;
153 var show_addbutton = now_len < max_len;
154 $lis.find('.deform-close-button').not($lis.find('.deform-seq-container .deform-close-button')).toggle(show_closebutton);
155 oid_node.find('.deform-seq-add').not(oid_node.find('.deform-seq-container .deform-seq-add')).toggle(show_addbutton);
156 $lis.find('.deform-order-button').not($lis.find('.deform-seq-container .deform-order-button')).toggle(orderable && has_multiple);
157 },
158
159 focusFirstInput: function (el) {
160 el = el || document.body;
161 var input = $(el).find(':input')
162 .filter('[id ^= deformField]')
163 .filter('[type != hidden]')
164 .first();
165 if (input) {
166 var raw = input.get(0);
167 if (raw) {
168 if (raw.type === 'text' || raw.type === 'file' ||
169 raw.type == 'password' || raw.type == 'text' ||
170 raw.type == 'textarea') {
171 if (!input.hasClass("hasDatepicker")) {
172 input.focus();
173 }
174 }
175 }
176 }
177 },
178
179 randomString: function (length) {
180 var chr='0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
181 chr = chr.split('');
182
183 if (! length) {
184 length = Math.floor(Math.random() * chr.length);
185 }
186
187 var str = '';
188 for (var i = 0; i < length; i++) {
189 str += chr[Math.floor(Math.random() * chr.length)];
190 }
191 return str;
192 }
193
194 };
@@ -20,6 +20,7 b' module.exports = function(grunt) {'
20 20 '<%= dirs.js.src %>/moment.js',
21 21 '<%= dirs.js.src %>/appenlight-client-0.4.1.min.js',
22 22 '<%= dirs.js.src %>/i18n_utils.js',
23 '<%= dirs.js.src %>/deform.js',
23 24
24 25 // Plugins
25 26 '<%= dirs.js.src %>/plugins/jquery.pjax.js',
@@ -83,7 +83,6 b" c.template_context['visual']['default_re"
83 83 <![endif]-->
84 84 <script language="javascript" type="text/javascript" src="${h.asset('js/rhodecode/routes.js', ver=c.rhodecode_version_hash)}"></script>
85 85 <script language="javascript" type="text/javascript" src="${h.asset('js/scripts.js', ver=c.rhodecode_version_hash)}"></script>
86 <script language="javascript" type="text/javascript" src="${h.static_url('deform:static/scripts/deform.js')}"></script>
87 86 ## avoide esaping the %N
88 87 <script>CodeMirror.modeURL = "${h.asset('') + 'js/mode/%N/%N.js'}";</script>
89 88
General Comments 0
You need to be logged in to leave comments. Login now