relaxngui.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. This file is part of RelaxNGui.
  3. RelaxNGui is free software: you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or (at your option) any later
  6. version.
  7. RelaxNGui is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  9. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License along with
  11. RelaxNGui (file COPYING in the main directory). If not, see
  12. <http://www.gnu.org/licenses/>.
  13. */
  14. var RelaxNGui = function(rng,target,ceval,ignore=false) {
  15. if (!(rng instanceof XMLDocument)) { // rng has to be XMLDocument //{{{
  16. rng = $XR($(rng).serializeXML());
  17. } //}}}
  18. var lenextract = function(tag, lencount) { //{{{
  19. $.each(tag.attributes,function(k,v){
  20. if ((v.localName == 'label') && (v.namespaceURI == 'http://rngui.org')) { lencount = v.nodeValue.length > lencount ? v.nodeValue.length : lencount; }
  21. });
  22. return lencount;
  23. }; //}}}
  24. var labextract = function(type,tag) { //{{{
  25. var ret = { 'type': type, 'wrap': false, 'readonly': false, 'label': '', default: '' };
  26. $.each(tag.attributes,function(k,v){
  27. if ((v.localName == 'label') && (v.namespaceURI == 'http://rngui.org')) { ret['label'] = v.nodeValue; }
  28. if ((v.localName == 'readonly') && (v.namespaceURI == 'http://rngui.org')) { ret['readonly'] = v.nodeValue == 'true' ? true : false; }
  29. if ((v.localName == 'wrap') && (v.namespaceURI == 'http://rngui.org')) { ret['wrap'] = v.nodeValue == 'true' ? true : false; }
  30. });
  31. $.each(tag.children,function(k,v){
  32. if ((v.localName == 'param') && (v.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  33. $.each(v.attributes,function(l,w){
  34. if ((w.localName == 'name') && (w.nodeValue == 'minInclusive')) {
  35. ret['min'] = v.lastChild.nodeValue;
  36. }
  37. if ((w.localName == 'name') && (w.nodeValue == 'maxInclusive')) {
  38. ret['max'] = v.lastChild.nodeValue;
  39. }
  40. });
  41. }
  42. });
  43. return ret;
  44. }; //}}}
  45. var addelements = function(target) { //{{{
  46. var template = target.parent().find('> .relaxngui_template').clone(true,true);
  47. template.removeClass('relaxngui_template');
  48. template.find('[data-relaxngui-template]').each(function(j,t){
  49. $(t).attr('data-relaxngui-template',false);
  50. });
  51. template.find('.relaxngui_template [data-relaxngui-template]').each(function(j,t){
  52. $(t).attr('data-relaxngui-template',true);
  53. });
  54. template.addClass('relaxngui_dyn');
  55. template.on('click', '> *:first-child', function(ev){ delelements($(ev.target)); });
  56. if (target.parent().find('> .relaxngui_dyn').length % 2 == 1) {
  57. var evens = template.find('.even');
  58. var odds = template.find('.odd');
  59. evens.each(function(k,v){
  60. $(v).removeClass('even');
  61. $(v).addClass('odd');
  62. });
  63. odds.each(function(k,v){
  64. $(v).removeClass('odd');
  65. $(v).addClass('even');
  66. });
  67. }
  68. target.parent().find('> .relaxngui_control').before(template);
  69. }; //}}}
  70. var delelements = function(target) { //{{{
  71. var it = target.parent();
  72. var par = target.parent().parent();
  73. it.remove();
  74. par.trigger('relaxngui_remove');
  75. }; //}}}
  76. var recshow_header = function(tag,ret,level){ //{{{
  77. var header;
  78. var fold;
  79. $.each(tag.attributes,function(k,v){
  80. if ((v.localName == 'header') && (v.namespaceURI == 'http://rngui.org')) { header = v.nodeValue; }
  81. if ((v.localName == 'fold') && (v.namespaceURI == 'http://rngui.org')) { fold = v.nodeValue; }
  82. });
  83. if (header) {
  84. if (fold != undefined) {
  85. var xxx = $("<div data-relaxngui-level='" + level + "' class='relaxngui_header fold'>" + header + "</div>");
  86. xxx.prepend($("<span class='relaxngui_fold'></span>"));
  87. ret.append(xxx);
  88. if (fold == 'closed' || fold == 'closed_conditional') {
  89. ret.addClass('closed');
  90. }
  91. if (fold == 'closed_conditional') {
  92. ret.addClass(fold);
  93. }
  94. } else {
  95. ret.append($("<div data-relaxngui-level='" + level + "' class='relaxngui_header'>" + header + "</div>"));
  96. }
  97. }
  98. return fold;
  99. } //}}}
  100. var recshow_single = function(tag,ret,template,path,lencount,optional){ //{{{
  101. var node = $('<div class="relaxngui_row"/>');
  102. var name;
  103. var label;
  104. var labeltype;
  105. var second = {};
  106. var datalist = [];
  107. var defaul = '';
  108. var hint = '';
  109. var retcount = 0;
  110. $.each(tag.attributes,function(k,v){
  111. if ((v.localName == 'label') && (v.namespaceURI == 'http://rngui.org')) { label = v.nodeValue; }
  112. if ((v.localName == 'date') && (v.namespaceURI == 'http://rngui.org')) { label = v.nodeValue; }
  113. if ((v.localName == 'labeltype') && (v.namespaceURI == 'http://rngui.org')) { labeltype = v.nodeValue; }
  114. if ((v.localName == 'default') && (v.namespaceURI == 'http://rngui.org')) { defaul = v.nodeValue; }
  115. if ((v.localName == 'hint') && (v.namespaceURI == 'http://rngui.org')) { hint = v.nodeValue; }
  116. if (v.localName == 'name') { name = v.nodeValue.replace(/:/,'\\:'); }
  117. });
  118. $.each($(tag).children('data[type=string]'), function(k,v) { second = labextract('string',v); });
  119. $.each($(tag).children('data[type=integer]'), function(k,v) { second = labextract('integer',v); });
  120. $.each($(tag).children('data[type=nonNegativeInteger]'), function(k,v) { second = labextract('nonNegativeInteger',v); });
  121. $.each($(tag).children('data[type=positiveInteger]'), function(k,v) { second = labextract('positiveInteger',v); });
  122. $.each($(tag).children('data[type=float]'), function(k,v) { second = labextract('float',v); });
  123. $.each($(tag).children('data[type=date]'), function(k,v) { second = labextract('date',v); });
  124. $.each($(tag).children('text'), function(k,v) { second = labextract('text',v); });
  125. $.each($(tag).find('choice > value'), function(k,v) {
  126. second = labextract('datalist',$(v).parent()[0]);
  127. datalist.push(v.textContent);
  128. });
  129. if (name && label) {
  130. node.append($("<label class='relaxngui_cell" + (optional && defaul == '' ? " optional": "") + "' style='min-width: " + (lencount+1) + "ex' for=''>" + label + "</label><span class='relaxngui_cell'>⇒</span>"));
  131. } else if (name) {
  132. // a tag without information is ignored
  133. } else if (label) {
  134. if (labeltype == 'xml') {
  135. node.append($("<input data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + path + " > *[data-name]' class='relaxngui_cell' type='text' pattern='^[a-zA-Z_][a-zA-Z0-9_-]*$' id='' placeholder='" + label + "'></input><span class='relaxngui_cell'>⇒</span>"));
  136. } else {
  137. node.append($("<input data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + path + " > *[data-name]' class='relaxngui_cell' type='text' pattern='^[a-z_][a-zA-Z0-9_]*$' id='' placeholder='" + label + "'></input><span class='relaxngui_cell'>⇒</span>"));
  138. }
  139. }
  140. var tpath = ((typeof name === 'undefined') ? path + ' > *' : (tag.localName == 'element' ? path + ' > ' + name : path + '[' + name + ']'));
  141. if (label) {
  142. if (defaul && typeof defaul == 'string' && defaul.match(/^javascript:/)) {
  143. defaul = defaul.replace(/^javascript:/,'');
  144. defaul = ceval ? ceval(defaul) : eval(defaul);
  145. }
  146. var os = (optional ? " onkeyup='var sl = $(this).siblings(\"label\"); if ($(this).get_val() == \"\") { if (!sl.hasClass(\"optional\")) { sl.addClass(\"optional\") } } else { sl.removeClass(\"optional\") }' data-optional='true'" : " data-optional='false'");
  147. if (second.readonly)
  148. node.append($("<input " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' type='text' id='' readonly='readonly'" + os + "></input>"));
  149. else {
  150. if (second.type == 'string') {
  151. node.append($("<input " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' type='text' id='' placeholder='" + second.label + "'" + os + "></input>"));
  152. } else if (second.type == 'integer') {
  153. node.append($("<input " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' type='number' id='' placeholder='" + second.label + "'" + (second.min != undefined ? (" min='" + second.min + "'") : '') + (second.max != undefined ? (" max='" + second.max + "'") : '') + os + "></input>"));
  154. } else if (second.type == 'positiveInteger') {
  155. if (second.min == undefined) second.min = 1;
  156. node.append($("<input " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' type='number' id='' placeholder='" + second.label + "'" + (second.min != undefined ? (" min='" + second.min + "'") : '') + (second.max != undefined ? (" max='" + second.max + "'") : '') + os + "></input>"));
  157. } else if (second.type == 'nonNegativeInteger') {
  158. if (second.min == undefined) second.min = 0;
  159. node.append($("<input " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' type='number' id='' placeholder='" + second.label + "'" + (second.min != undefined ? (" min='" + second.min + "'") : '') + (second.max != undefined ? (" max='" + second.max + "'") : '') + os + "></input>"));
  160. } else if (second.type == 'date') {
  161. node.append($("<input " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' type='date' id='' placeholder='" + second.label + "'" + os + "></input>"));
  162. } else if (second.type == 'float') {
  163. node.append($("<input " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' type='number' step='any' id='' placeholder='" + second.label + "'" + (second.min != undefined ? (" min='" + second.min + "'") : '') + (second.max != undefined ? (" max='" + second.max + "'") : '') + os + "></input>"));
  164. } else if (second.type == 'text') {
  165. node.append($("<div contenteditable='true' data-relaxngui-wrap='" + second.wrap + "' " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' id='' placeholder='" + second.label + "'" + os + "></div>"));
  166. } else if (second.type == 'datalist') {
  167. var tnode = $("<select " + (defaul && typeof defaul == 'string' ? 'value="' + defaul + '"' : '') + " data-relaxngui-template='" + template + "' data-relaxngui-parent='" + path + "' data-relaxngui-path='" + tpath + "' class='relaxngui_cell' id='' size='1'" + os + "></select>");
  168. $.each(datalist,function(didx,dname){
  169. if (dname == defaul)
  170. tnode.append('<option value="' + dname + '" selected="selected">' + dname + '</value>');
  171. else
  172. tnode.append('<option value="' + dname + '">' + dname + '</value>');
  173. });
  174. node.append(tnode);
  175. }
  176. }
  177. ret.append(node);
  178. retcount += 1;
  179. } else {
  180. if (tag.localName != 'element') { // its an attribute, simulate its empty-ness
  181. node.attr('data-relaxngui-template',template);
  182. node.attr('data-relaxngui-parent',path);
  183. node.attr('data-relaxngui-path',tpath);
  184. ret.append(node);
  185. retcount += 1;
  186. }
  187. }
  188. if (hint) {
  189. var n = $('<div class="relaxngui_hint"/>');
  190. var s1 = $('<em>Hint: </em>');
  191. var s2 = $('<span/>');
  192. s2.text(hint);
  193. n.append(s1);
  194. n.append(s2);
  195. ret.append(n);
  196. retcount += 1;
  197. }
  198. return retcount;
  199. } //}}}
  200. var recshow = function(elements,template,path,attr) { //{{{
  201. // delete all elements with relaxngui:ignore
  202. if (attr.ignore) {
  203. var tele = $.grep(elements,function(tagv){
  204. var include = true;
  205. $.each(tagv.attributes,function(k,v){
  206. if ((v.localName == 'ignore') && (v.namespaceURI == 'http://rngui.org')) {
  207. include = false;
  208. }
  209. });
  210. return include;
  211. });
  212. elements = $(tele);
  213. }
  214. var ret = $('<div/>');
  215. var lencount = 0;
  216. $.each(elements,function(k,v){
  217. var tag = $(v)[0];
  218. if ((tag.localName == 'element') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  219. $(tag).children('attribute').each(function(l,w){
  220. lencount = lenextract($(w)[0],lencount);
  221. });
  222. lencount = lenextract(tag,lencount);
  223. }
  224. });
  225. $.each(elements,function(k,v){
  226. if (attr.mode == 'even') { attr.mode = 'odd' }
  227. else { attr.mode = 'even'; }
  228. var tag = $(v)[0];
  229. if ((tag.localName == 'element') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  230. var xxx;
  231. if (template) {
  232. var yyy = $('<div class="relaxngui_table ' + attr.mode + '" data-relaxngui-template="true" data-relaxngui-parent="' + path + '" data-relaxngui-path="' + (path == '' ? ' > ' + (typeof elements.attr('name') === 'undefined' ? '*' : elements.attr('name')) : path + ' > ' + (typeof $(tag).attr('name') === 'undefined' ? '*' : $(tag).attr('name'))) + '[data-main]">');
  233. xxx = $('<div class="relaxngui_template"><span>✖</span></div>');
  234. xxx.append(yyy);
  235. ret.append(xxx);
  236. xxx = yyy;
  237. } else {
  238. xxx = $('<div class="relaxngui_table ' + attr.mode + '" data-relaxngui-template="false" data-relaxngui-parent="' + path + '" data-relaxngui-path="' + (path == '' ? ' > ' + elements.attr('name') : path + ' > ' + $(tag).attr('name')) + '[data-main]">');
  239. ret.append(xxx);
  240. }
  241. recshow_header(tag,xxx,attr.level);
  242. var rcount = 0;
  243. $(tag).children('attribute').each(function(l,w){
  244. var ttag = $(w)[0];
  245. rcount += recshow_single(ttag,xxx,template,path + ' > ' + $(tag).attr('name'),lencount,attr.optional ? true : false);
  246. });
  247. rcount += recshow_single(tag,xxx,template,path,lencount,attr.optional ? true : false);
  248. var sub;
  249. if (sub = recshow($(tag).children('element, zeroOrMore, optional'),false,path + ' > ' + $(tag).attr('name'),{ ignore: attr.ignore, mode: (attr.mode == 'even' && rcount % 2 == 0 ? 'odd' : 'even' ), level: attr.level + 1 })) {
  250. var inode = xxx.append(sub);
  251. if (template) {
  252. inode.find('[data-relaxngui-template]').each(function(j,t){
  253. $(t).attr('data-relaxngui-template',true);
  254. });
  255. }
  256. }
  257. } else if ((tag.localName == 'zeroOrMore') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  258. var label;
  259. $.each(tag.attributes,function(k,v){
  260. if ((v.localName == 'label') && (v.namespaceURI == 'http://rngui.org')) { label = v.nodeValue; }
  261. });
  262. var but = $('<button class="relaxngui_control">' + label + '</button>');
  263. but.on('click',function(ev){ addelements($(ev.target)); });
  264. ret.append(recshow($(tag).children(),true,path,{ ignore: attr.ignore, mode: attr.mode, level: attr.level }));
  265. ret.append(but);
  266. // } else if ((tag.localName == 'choice') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  267. // var label;
  268. // $.each(tag.attributes,function(k,v){
  269. // if ((v.localName == 'label') && (v.namespaceURI == 'http://rngui.org')) { label = v.nodeValue; }
  270. // });
  271. // var but = $('<button class="relaxngui_control">' + label + '</button>');
  272. // but.on('click',function(ev){ addelements($(ev.target)); });
  273. //
  274. // ret.append(recshow($(tag).children(),true,path,{ ignore: attr.ignore, mode: attr.mode }));
  275. // ret.append(but);
  276. } else if ((tag.localName == 'optional') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  277. ret.append(recshow($(tag).children('element, zeroOrMore'),false,path,{ ignore: attr.ignore, mode: (attr.mode == 'even' ? 'odd' : 'even'), optional: true, level: attr.level + 1 }));
  278. }
  279. });
  280. return ret.children().length > 0 ? ret.children() : undefined;
  281. }; //}}}
  282. var serializeXML = function (xml) { //{{{
  283. var out = '';
  284. if (typeof XMLSerializer == 'function') {
  285. var xs = new XMLSerializer();
  286. $(xml).each(function() {
  287. out += xs.serializeToString(this);
  288. });
  289. } else if (xml && xml.xml != 'undefined') {
  290. $(xml).each(function() {
  291. out += this.xml;
  292. });
  293. }
  294. return out;
  295. }; //}}}
  296. this.save = function() { //{{{
  297. var xml;
  298. var curr;
  299. var tar = target.find('[data-relaxngui-path]:not([data-relaxngui-template=true])');
  300. for (var i = 0; i<tar.length;) {
  301. var path = $(tar[i]).attr('data-relaxngui-path');
  302. var parent_path = $(tar[i]).attr('data-relaxngui-parent');
  303. if (i == 0) {
  304. var par = path.replace(/\[data-main\]/,'').replace(/ > /,'');
  305. xml = $XR('<' + par + '/>');
  306. } else {
  307. var ma = path.match(/([^\s]+)$/)[1];
  308. var att;
  309. if (ma.match(/\*\[data-main\]/)) {
  310. // do nothing. seriously. explicitly.
  311. } else if (ma.match(/\[data-main\]/)) {
  312. var par = ma.replace(/\[data-main\]/,'');
  313. var curr = $($XR('<' + par + '/>').documentElement);
  314. $(parent_path,xml).last().append(curr);
  315. } else if (ma.match(/\[data-name\]/)) {
  316. if ($(tar[i]).get_val()) {
  317. var nn = $($XR('<' + $(tar[i]).get_val() + '/>').documentElement).text($(tar[i+1]).get_val());
  318. $(parent_path,xml).append(nn);
  319. }
  320. i+=1;
  321. } else if (att = ma.match(/\[([^\]]+)\]$/)) {
  322. att[1] = att[1].replace(/\\/,'');
  323. $(parent_path + ':last-child',xml).last().attr(att[1],$(tar[i]).get_val());
  324. } else {
  325. if ($(tar[i]).attr('data-optional') == 'true' && $(tar[i]).get_val() == '') {
  326. $(path + ':last-child',xml).last().remove();
  327. } else {
  328. $(path + ':last-child',xml).last().text($(tar[i]).get_val())
  329. }
  330. }
  331. }
  332. i+=1;
  333. }
  334. return xml;
  335. }; //}}}
  336. this.save_text = function() { //{{{
  337. return serializeXML(self.save());
  338. } //}}}
  339. this.content = function(data) { //{{{
  340. if (!(data instanceof XMLDocument)) { // data has to be XMLDocument //{{{
  341. data = $XR($(data).serializeXML());
  342. } //}}}
  343. if (data) {
  344. var x = $(data).serializePrettyXML();
  345. x = x.replace(/\s+xmlns(:[a-zA-Z0-9]+)?=\"[^\"]+\"/g, "");
  346. x = x.replace(/<\?[^>]+>/g, "");
  347. x = x.trim();
  348. y = $(self.save()).serializePrettyXML();
  349. if (x != y) {
  350. target.find('.relaxngui_dyn').remove();
  351. target.find('[data-relaxngui-path]').each(function(k,pa){
  352. var path = $(pa).attr('data-relaxngui-path');
  353. if (!path.match(/data-\w+\]$/)) {
  354. if ($(pa).attr('data-relaxngui-template') == 'true' && path.match(/\*$/)) {
  355. $(data).find(path).each(function(index,ele){
  356. $(target.find('[data-relaxngui-path="' + path + '[data-name]"][data-relaxngui-template="false"]').get(index)).set_val(ele.localName);
  357. $(target.find('[data-relaxngui-path="' + path + '"][data-relaxngui-template="false"]').get(index)).set_val($(ele).text());
  358. });
  359. } else if ($(pa).attr('data-relaxngui-template') == 'true' && !path.match(/\*$/)) {
  360. $(data).find(path).each(function(index,ele){
  361. var att;
  362. var val;
  363. if (att = path.match(/(.*)\[([^\]]+)\]$/)) {
  364. val = $(ele).attr(att[2]);
  365. } else {
  366. val = $(ele).text();
  367. }
  368. if (val && val != '') {
  369. $(target.find('[data-relaxngui-path="' + path + '"][data-relaxngui-template="false"]').get(index)).set_val(val);
  370. }
  371. });
  372. } else {
  373. var att;
  374. var val;
  375. if (att = path.match(/(.*)\[([^\]]+)\]$/)) {
  376. val = $(data).find(att[1]).attr(att[2]);
  377. } else {
  378. val = $(data).find(path).text();
  379. }
  380. if (val && val != '') {
  381. var t = target.find('[data-relaxngui-path="' + path + '"]');
  382. t.set_val(val);
  383. if (t.attr('data-optional') == 'true') {
  384. t.siblings('label').removeClass('optional');
  385. }
  386. }
  387. }
  388. } else {
  389. if ($(pa).attr('data-relaxngui-template') == 'true') {
  390. var but = target.find('.relaxngui_table[data-relaxngui-template="false"] > .relaxngui_template > [data-relaxngui-path="' + path + '"][data-relaxngui-template="true"]').parent().parent().find('> button');
  391. if (but.length > 0) {
  392. var dpath = path.replace(/\[data-main\]$/,'');
  393. var par = undefined;
  394. var ind = -1;
  395. $(data).find(dpath).each(function(ke,ele){
  396. if (par != $(ele).parent()[0]) {
  397. ind += 1;
  398. par = $(ele).parent()[0];
  399. }
  400. if ($(but.get(ind)).attr('disabled')) {
  401. $(but.get(ind)).removeAttr('disabled');
  402. but.get(ind).click();
  403. $(but.get(ind)).attr('disabled','disabled');
  404. } else {
  405. but.get(ind).click();
  406. }
  407. });
  408. }
  409. }
  410. }
  411. });
  412. self.set_checkpoint();
  413. }
  414. target.find('.relaxngui_table.closed_conditional').each(function(k,e) {
  415. $(e).find('input:not([readonly]), [contenteditable]:not([readonly])').each(function(l,f) {
  416. if ($(f).get_val() != '') {
  417. $(e).removeClass('closed');
  418. }
  419. });
  420. });
  421. }
  422. }; //}}}
  423. // stuff to determine if user changed something
  424. var orig = '';
  425. this.set_checkpoint = function() { //{{{
  426. orig = self.save_text();
  427. } //}}}
  428. this.has_changed = function() { //{{{
  429. if (orig != self.save_text()) {
  430. return true;
  431. } else {
  432. return false;
  433. }
  434. } //}}}
  435. target.append(recshow($(rng.documentElement),false,'',{ ignore: ignore, mode: 'even', level: 0}));
  436. var self = this;
  437. target.unbind('click.relaxngui');
  438. target.on('click.relaxngui','.relaxngui_header.fold',function(){
  439. $(this).parent().toggleClass('closed');
  440. });
  441. };