relaxngui.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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,container) { //{{{
  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 (container.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. container.append(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. node.addClass('relaxngui_hidden');
  134. } else if (label) {
  135. if (labeltype == 'xml') {
  136. 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>"));
  137. } else {
  138. 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>"));
  139. }
  140. }
  141. var tpath = ((typeof name === 'undefined') ? path + ' > *' : (tag.localName == 'element' ? path + ' > ' + name : path + '[' + name + ']'));
  142. if (label) {
  143. if (defaul && typeof defaul == 'string' && defaul.match(/^javascript:/)) {
  144. defaul = defaul.replace(/^javascript:/,'');
  145. defaul = ceval ? ceval(defaul) : eval(defaul);
  146. }
  147. 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'");
  148. if (second.readonly)
  149. 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>"));
  150. else {
  151. if (second.type == 'string') {
  152. 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>"));
  153. } else if (second.type == 'integer') {
  154. 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>"));
  155. } else if (second.type == 'positiveInteger') {
  156. if (second.min == undefined) second.min = 1;
  157. 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>"));
  158. } else if (second.type == 'nonNegativeInteger') {
  159. if (second.min == undefined) second.min = 0;
  160. 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>"));
  161. } else if (second.type == 'date') {
  162. 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>"));
  163. } else if (second.type == 'float') {
  164. 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>"));
  165. } else if (second.type == 'text') {
  166. 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>"));
  167. } else if (second.type == 'datalist') {
  168. 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>");
  169. $.each(datalist,function(didx,dname){
  170. if (dname == defaul)
  171. tnode.append('<option value="' + dname + '" selected="selected">' + dname + '</value>');
  172. else
  173. tnode.append('<option value="' + dname + '">' + dname + '</value>');
  174. });
  175. node.append(tnode);
  176. }
  177. }
  178. ret.append(node);
  179. retcount += 1;
  180. } else {
  181. if (tag.localName != 'element') { // its an attribute, simulate its empty-ness
  182. node.attr('data-relaxngui-template',template);
  183. node.attr('data-relaxngui-parent',path);
  184. node.attr('data-relaxngui-path',tpath);
  185. ret.append(node);
  186. retcount += 1;
  187. }
  188. }
  189. if (hint) {
  190. var n = $('<div class="relaxngui_hint"/>');
  191. var s1 = $('<em>Hint: </em>');
  192. var s2 = $('<span/>');
  193. s2.text(hint);
  194. n.append(s1);
  195. n.append(s2);
  196. ret.append(n);
  197. retcount += 1;
  198. }
  199. return retcount;
  200. } //}}}
  201. var recshow = function(elements,template,path,attr) { //{{{
  202. // delete all elements with relaxngui:ignore
  203. if (attr.ignore) {
  204. var tele = $.grep(elements,function(tagv){
  205. var include = true;
  206. $.each(tagv.attributes,function(k,v){
  207. if ((v.localName == 'ignore') && (v.namespaceURI == 'http://rngui.org')) {
  208. include = false;
  209. }
  210. });
  211. return include;
  212. });
  213. elements = $(tele);
  214. }
  215. var ret = $('<div/>');
  216. var lencount = 0;
  217. $.each(elements,function(k,v){
  218. var tag = $(v)[0];
  219. if ((tag.localName == 'element') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  220. $(tag).children('attribute').each(function(l,w){
  221. lencount = lenextract($(w)[0],lencount);
  222. });
  223. lencount = lenextract(tag,lencount);
  224. }
  225. });
  226. $.each(elements,function(k,v){
  227. if (attr.mode == 'even') { attr.mode = 'odd' }
  228. else { attr.mode = 'even'; }
  229. var tag = $(v)[0];
  230. if ((tag.localName == 'element') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  231. var xxx;
  232. if (template) {
  233. 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]">');
  234. xxx = $('<div class="relaxngui_template"><span>✖</span></div>');
  235. xxx.append(yyy);
  236. ret.append(xxx);
  237. xxx = yyy;
  238. } else {
  239. 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]">');
  240. ret.append(xxx);
  241. }
  242. recshow_header(tag,xxx,attr.level);
  243. var rcount = 0;
  244. $(tag).children('attribute').each(function(l,w){
  245. var ttag = $(w)[0];
  246. rcount += recshow_single(ttag,xxx,template,path + ' > ' + $(tag).attr('name'),lencount,attr.optional ? true : false);
  247. });
  248. rcount += recshow_single(tag,xxx,template,path,lencount,attr.optional ? true : false);
  249. var sub;
  250. if (sub = recshow($(tag).children('element, zeroOrMore, optional, choice'),false,path + ' > ' + $(tag).attr('name'),{ ignore: attr.ignore, mode: (attr.mode == 'even' && rcount % 2 == 0 ? 'odd' : 'even' ), level: attr.level + 1 })) {
  251. var inode = xxx.append(sub);
  252. if (template) {
  253. inode.find('[data-relaxngui-template]').each(function(j,t){
  254. $(t).attr('data-relaxngui-template',true);
  255. });
  256. }
  257. }
  258. } else if ((tag.localName == 'zeroOrMore') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  259. var label;
  260. $.each(tag.attributes,function(k,v){
  261. if ((v.localName == 'label') && (v.namespaceURI == 'http://rngui.org')) { label = v.nodeValue; }
  262. });
  263. var but = $('<button class="relaxngui_control">' + label + '</button>');
  264. but.on('click',function(ev){ addelements($(ev.target),$(ev.target).parent()); });
  265. ret.append(recshow($(tag).children(),true,path,{ ignore: attr.ignore, mode: attr.mode, level: attr.level }));
  266. ret.append(but);
  267. } else if ((tag.localName == 'optional') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  268. ret.append(recshow($(tag).children('element, zeroOrMore'),false,path,{ ignore: attr.ignore, mode: (attr.mode == 'even' ? 'odd' : 'even'), optional: true, level: attr.level + 1 }));
  269. } else if ((tag.localName == 'choice') && (tag.namespaceURI == 'http://relaxng.org/ns/structure/1.0')) {
  270. if (!($(tag).children()[0].localName == 'value' && $(tag).children()[0].namespaceURI)) {
  271. $.each($(tag).children(),function(j,u){
  272. var label;
  273. $.each(u.attributes,function(k,v){
  274. if ((v.localName == 'label') && (v.namespaceURI == 'http://rngui.org')) { label = v.nodeValue; }
  275. });
  276. var but = $('<button class="relaxngui_control">' + label + '</button>');
  277. but.on('click',function(ev){ addelements($(ev.target),$(ev.target).parent().parent()); });
  278. var col = $('<span></span');
  279. col.append(recshow($(u).children(),true,path,{ ignore: attr.ignore, mode: attr.mode }));
  280. col.append(but);
  281. ret.append(col);
  282. });
  283. }
  284. }
  285. });
  286. return ret.children().length > 0 ? ret.children() : undefined;
  287. }; //}}}
  288. var serializeXML = function (xml) { //{{{
  289. var out = '';
  290. if (typeof XMLSerializer == 'function') {
  291. var xs = new XMLSerializer();
  292. $(xml).each(function() {
  293. out += xs.serializeToString(this);
  294. });
  295. } else if (xml && xml.xml != 'undefined') {
  296. $(xml).each(function() {
  297. out += this.xml;
  298. });
  299. }
  300. return out;
  301. }; //}}}
  302. this.save = function() { //{{{
  303. var xml;
  304. var curr;
  305. var tar = target.find('[data-relaxngui-path]:not([data-relaxngui-template=true])');
  306. for (var i = 0; i<tar.length;) {
  307. var path = $(tar[i]).attr('data-relaxngui-path');
  308. var parent_path = $(tar[i]).attr('data-relaxngui-parent');
  309. if (i == 0) {
  310. var par = path.replace(/\[data-main\]/,'').replace(/ > /,'');
  311. xml = $XR('<' + par + '/>');
  312. } else {
  313. var ma = path.match(/([^\s]+)$/)[1];
  314. var att;
  315. if (ma.match(/\*\[data-main\]/)) {
  316. // do nothing. seriously. explicitly.
  317. } else if (ma.match(/\[data-main\]/)) {
  318. var par = ma.replace(/\[data-main\]/,'');
  319. var curr = $($XR('<' + par + '/>').documentElement);
  320. $(parent_path,xml).last().append(curr);
  321. } else if (ma.match(/\[data-name\]/)) {
  322. if ($(tar[i]).get_val()) {
  323. var nn = $($XR('<' + $(tar[i]).get_val() + '/>').documentElement).text($(tar[i+1]).get_val());
  324. $(parent_path,xml).append(nn);
  325. }
  326. i+=1;
  327. } else if (att = ma.match(/\[([^\]]+)\]$/)) {
  328. att[1] = att[1].replace(/\\/,'');
  329. $(parent_path + ':last-child',xml).last().attr(att[1],$(tar[i]).get_val());
  330. } else {
  331. if ($(tar[i]).attr('data-optional') == 'true' && $(tar[i]).get_val() == '') {
  332. $(path + ':last-child',xml).last().remove();
  333. } else {
  334. $(path + ':last-child',xml).last().text($(tar[i]).get_val())
  335. }
  336. }
  337. }
  338. i+=1;
  339. }
  340. return xml;
  341. }; //}}}
  342. this.save_text = function() { //{{{
  343. return serializeXML(self.save());
  344. } //}}}
  345. this.content = function(data) { //{{{
  346. if (!(data instanceof XMLDocument)) { // data has to be XMLDocument //{{{
  347. data = $XR($(data).serializeXML());
  348. } //}}}
  349. if (data) {
  350. var x = $(data).serializePrettyXML();
  351. x = x.replace(/\s+xmlns(:[a-zA-Z0-9]+)?=\"[^\"]+\"/g, "");
  352. x = x.replace(/<\?[^>]+>/g, "");
  353. x = x.trim();
  354. y = $(self.save()).serializePrettyXML();
  355. if (x != y) {
  356. target.find('.relaxngui_dyn').remove();
  357. target.find('[data-relaxngui-path]').each(function(k,pa){
  358. var path = $(pa).attr('data-relaxngui-path');
  359. if (!path.match(/data-\w+\]$/)) {
  360. if ($(pa).attr('data-relaxngui-template') == 'true' && path.match(/\*$/)) {
  361. $(data).find(path).each(function(index,ele){
  362. $(target.find('[data-relaxngui-path="' + path + '[data-name]"][data-relaxngui-template="false"]').get(index)).set_val(ele.localName);
  363. $(target.find('[data-relaxngui-path="' + path + '"][data-relaxngui-template="false"]').get(index)).set_val($(ele).text());
  364. });
  365. } else if ($(pa).attr('data-relaxngui-template') == 'true' && !path.match(/\*$/)) {
  366. $(data).find(path).each(function(index,ele){
  367. var att;
  368. var val;
  369. if (att = path.match(/(.*)\[([^\]]+)\]$/)) {
  370. val = $(ele).attr(att[2]);
  371. } else {
  372. val = $(ele).text();
  373. }
  374. if (val && val != '') {
  375. $(target.find('[data-relaxngui-path="' + path + '"][data-relaxngui-template="false"]').get(index)).set_val(val);
  376. }
  377. });
  378. } else {
  379. var att;
  380. var val;
  381. if (att = path.match(/(.*)\[([^\]]+)\]$/)) {
  382. val = $(data).find(att[1]).attr(att[2]);
  383. } else {
  384. val = $(data).find(path).text();
  385. }
  386. if (val && val != '') {
  387. var t = target.find('[data-relaxngui-path="' + path + '"]');
  388. t.set_val(val);
  389. if (t.attr('data-optional') == 'true') {
  390. t.siblings('label').removeClass('optional');
  391. }
  392. }
  393. }
  394. } else {
  395. console.log('-----');
  396. console.log(path);
  397. console.log(target);
  398. console.log($(pa).attr('data-relaxngui-template'));
  399. if ($(pa).attr('data-relaxngui-template') == 'true') {
  400. var but = [];
  401. var but1 = target.find('.relaxngui_table[data-relaxngui-template="false"] > .relaxngui_template > [data-relaxngui-path="' + path + '"][data-relaxngui-template="true"]').parent().parent().find('> button');
  402. var but2 = target.find('.relaxngui_table[data-relaxngui-template="false"] > span > .relaxngui_template > [data-relaxngui-path="' + path + '"][data-relaxngui-template="true"]').parent().parent().find('> button');
  403. if (but1.length > 0) { but = but1; }
  404. if (but2.length > 0) { but = but2; }
  405. console.log(but.length);
  406. if (but.length > 0) {
  407. var dpath = path.replace(/\[data-main\]$/,'');
  408. var par = undefined;
  409. var ind = -1;
  410. $(data).find(dpath).each(function(ke,ele){
  411. if (par != $(ele).parent()[0]) {
  412. ind += 1;
  413. par = $(ele).parent()[0];
  414. }
  415. if ($(but.get(ind)).attr('disabled')) {
  416. $(but.get(ind)).removeAttr('disabled');
  417. but.get(ind).click();
  418. $(but.get(ind)).attr('disabled','disabled');
  419. } else {
  420. but.get(ind).click();
  421. }
  422. });
  423. }
  424. }
  425. }
  426. });
  427. self.set_checkpoint();
  428. }
  429. target.find('.relaxngui_table.closed_conditional').each(function(k,e) {
  430. $(e).find('input:not([readonly]), [contenteditable]:not([readonly])').each(function(l,f) {
  431. if ($(f).get_val() != '') {
  432. $(e).removeClass('closed');
  433. }
  434. });
  435. });
  436. }
  437. }; //}}}
  438. // stuff to determine if user changed something
  439. var orig = '';
  440. this.set_checkpoint = function() { //{{{
  441. orig = self.save_text();
  442. } //}}}
  443. this.has_changed = function() { //{{{
  444. if (orig != self.save_text()) {
  445. return true;
  446. } else {
  447. return false;
  448. }
  449. } //}}}
  450. target.append(recshow($(rng.documentElement),false,'',{ ignore: ignore, mode: 'even', level: 0}));
  451. var self = this;
  452. target.unbind('click.relaxngui');
  453. target.on('click.relaxngui','.relaxngui_header.fold',function(){
  454. $(this).parent().toggleClass('closed');
  455. });
  456. };