threadsort.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. [Discuz!] (C)2001-2099 Comsenz Inc.
  3. This is NOT a freeware, use is subject to license terms
  4. $Id: threadsort.js 30962 2012-07-04 07:57:45Z zhangjie $
  5. */
  6. function xmlobj() {
  7. var obj = new Object();
  8. obj.createXMLDoc = function(xmlstring) {
  9. var xmlobj = false;
  10. if(window.DOMParser && document.implementation && document.implementation.createDocument) {
  11. try{
  12. var domparser = new DOMParser();
  13. xmlobj = domparser.parseFromString(xmlstring, 'text/xml');
  14. } catch(e) {
  15. }
  16. } else if(window.ActiveXObject) {
  17. var versions = ["MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "Microsoft.XmlDom"];
  18. for(var i=0; i<versions.length; i++) {
  19. try {
  20. xmlobj = new ActiveXObject(versions[i]);
  21. if(xmlobj) {
  22. xmlobj.async = false;
  23. xmlobj.loadXML(xmlstring);
  24. }
  25. } catch(e) {}
  26. }
  27. }
  28. return xmlobj;
  29. };
  30. obj.xml2json = function(xmlobj, node) {
  31. var nodeattr = node.attributes;
  32. if(nodeattr != null) {
  33. if(nodeattr.length && xmlobj == null) {
  34. xmlobj = new Object();
  35. }
  36. for(var i = 0;i < nodeattr.length;i++) {
  37. xmlobj[nodeattr[i].name] = nodeattr[i].value;
  38. }
  39. }
  40. var nodetext = "text";
  41. if(node.text == null) {
  42. nodetext = "textContent";
  43. }
  44. var nodechilds = node.childNodes;
  45. if(nodechilds != null) {
  46. if(nodechilds.length && xmlobj == null) {
  47. xmlobj = new Object();
  48. }
  49. for(var i = 0;i < nodechilds.length;i++) {
  50. if(nodechilds[i].tagName != null) {
  51. if(nodechilds[i].childNodes[0] != null && nodechilds[i].childNodes.length <= 1 && (nodechilds[i].childNodes[0].nodeType == 3 || nodechilds[i].childNodes[0].nodeType == 4)) {
  52. if(xmlobj[nodechilds[i].tagName] == null) {
  53. xmlobj[nodechilds[i].tagName] = nodechilds[i][nodetext];
  54. } else {
  55. if(typeof(xmlobj[nodechilds[i].tagName]) == "object" && xmlobj[nodechilds[i].tagName].length) {
  56. xmlobj[nodechilds[i].tagName][xmlobj[nodechilds[i].tagName].length] = nodechilds[i][nodetext];
  57. } else {
  58. xmlobj[nodechilds[i].tagName] = [xmlobj[nodechilds[i].tagName]];
  59. xmlobj[nodechilds[i].tagName][1] = nodechilds[i][nodetext];
  60. }
  61. }
  62. } else {
  63. if(nodechilds[i].childNodes.length) {
  64. if(xmlobj[nodechilds[i].tagName] == null) {
  65. xmlobj[nodechilds[i].tagName] = new Object();
  66. this.xml2json(xmlobj[nodechilds[i].tagName], nodechilds[i]);
  67. } else {
  68. if(xmlobj[nodechilds[i].tagName].length) {
  69. xmlobj[nodechilds[i].tagName][xmlobj[nodechilds[i].tagName].length] = new Object();
  70. this.xml2json(xmlobj[nodechilds[i].tagName][xmlobj[nodechilds[i].tagName].length-1], nodechilds[i]);
  71. } else {
  72. xmlobj[nodechilds[i].tagName] = [xmlobj[nodechilds[i].tagName]];
  73. xmlobj[nodechilds[i].tagName][1] = new Object();
  74. this.xml2json(xmlobj[nodechilds[i].tagName][1], nodechilds[i]);
  75. }
  76. }
  77. } else {
  78. xmlobj[nodechilds[i].tagName] = nodechilds[i][nodetext];
  79. }
  80. }
  81. }
  82. }
  83. }
  84. };
  85. return obj;
  86. }
  87. var xml = new xmlobj();
  88. var xmlpar = xml.createXMLDoc(forum_optionlist);
  89. var forum_optionlist_obj = new Object();
  90. xml.xml2json(forum_optionlist_obj, xmlpar);
  91. function changeselectthreadsort(selectchoiceoptionid, optionid, type) {
  92. if(selectchoiceoptionid == '0') {
  93. return;
  94. }
  95. var soptionid = 's' + optionid;
  96. var sselectchoiceoptionid = 's' + selectchoiceoptionid;
  97. forum_optionlist = forum_optionlist_obj['forum_optionlist'];
  98. var choicesarr = forum_optionlist[soptionid]['schoices'];
  99. var lastcount = 1;
  100. var name = issearch = id = nameid = '';
  101. if(type == 'search') {
  102. issearch = ', \'search\'';
  103. name = ' name="searchoption[' + optionid + '][value]"';
  104. id = 'id="' + forum_optionlist[soptionid]['sidentifier'] + '"';
  105. } else {
  106. name = ' name="typeoption[' + forum_optionlist[soptionid]['sidentifier'] + ']"';
  107. id = 'id="typeoption_' + forum_optionlist[soptionid]['sidentifier'] + '"';
  108. }
  109. if((choicesarr[sselectchoiceoptionid]['slevel'] == 1 || type == 'search') && choicesarr[sselectchoiceoptionid]['scount'] == 1) {
  110. nameid = name + ' ' + id;
  111. }
  112. var selectoption = '<select' + nameid + ' class="ps vm" onchange="changeselectthreadsort(this.value, \'' + optionid + '\'' + issearch + ');checkoption(\'' + forum_optionlist[soptionid]['sidentifier'] + '\', \'' + forum_optionlist[soptionid]['srequired'] + '\', \'' + forum_optionlist[soptionid]['stype'] + '\')" ' + ((forum_optionlist[soptionid]['sunchangeable'] == 1 && type == 'update') ? 'disabled' : '') + '><option value="0">请选择</option>';
  113. for(var i in choicesarr) {
  114. nameid = '';
  115. if((choicesarr[sselectchoiceoptionid]['slevel'] == 1 || type == 'search') && choicesarr[i]['scount'] == choicesarr[sselectchoiceoptionid]['scount']) {
  116. nameid = name + ' ' + id;
  117. }
  118. if(choicesarr[i]['sfoptionid'] != '0') {
  119. var patrn1 = new RegExp("^" + choicesarr[i]['sfoptionid'] + "\\.", 'i');
  120. var patrn2 = new RegExp("^" + choicesarr[i]['sfoptionid'] + "$", 'i');
  121. if(selectchoiceoptionid.match(patrn1) == null && selectchoiceoptionid.match(patrn2) == null) {
  122. continue;
  123. }
  124. }
  125. if(choicesarr[i]['scount'] != lastcount) {
  126. if(parseInt(choicesarr[i]['scount']) >= (parseInt(choicesarr[sselectchoiceoptionid]['scount']) + parseInt(choicesarr[sselectchoiceoptionid]['slevel']))) {
  127. break;
  128. }
  129. selectoption += '</select>' + "\r\n" + '<select' + nameid + ' class="ps vm" onchange="changeselectthreadsort(this.value, \'' + optionid + '\'' + issearch + ');checkoption(\'' + forum_optionlist[soptionid]['sidentifier'] + '\', \'' + forum_optionlist[soptionid]['srequired'] + '\', \'' + forum_optionlist[soptionid]['stype'] + '\')" ' + ((forum_optionlist[soptionid]['sunchangeable'] == 1 && type == 'update') ? 'disabled' : '') + '><option value="0">请选择</option>';
  130. lastcount = parseInt(choicesarr[i]['scount']);
  131. }
  132. var patrn1 = new RegExp("^" + choicesarr[i]['soptionid'] + "\\.", 'i');
  133. var patrn2 = new RegExp("^" + choicesarr[i]['soptionid'] + "$", 'i');
  134. var isnext = '';
  135. if(parseInt(choicesarr[i]['slevel']) != 1) {
  136. isnext = '&raquo;';
  137. }
  138. if(selectchoiceoptionid.match(patrn1) != null || selectchoiceoptionid.match(patrn2) != null) {
  139. selectoption += "\r\n" + '<option value="' + choicesarr[i]['soptionid'] + '" selected="selected">' + choicesarr[i]['scontent'] + isnext + '</option>';
  140. } else {
  141. selectoption += "\r\n" + '<option value="' + choicesarr[i]['soptionid'] + '">' + choicesarr[i]['scontent'] + isnext + '</option>';
  142. }
  143. }
  144. selectoption += '</select>';
  145. if(type == 'search') {
  146. selectoption += "\r\n" + '<input type="hidden" name="searchoption[' + optionid + '][type]" value="select">';
  147. }
  148. $('select_' + forum_optionlist[soptionid]['sidentifier']).innerHTML = selectoption;
  149. }
  150. function checkoption(identifier, required, checktype, checkmaxnum, checkminnum, checkmaxlength) {
  151. if(checktype != 'image' && checktype != 'select' && !$('typeoption_' + identifier) || !$('check' + identifier)) {
  152. return true;
  153. }
  154. var ce = $('check' + identifier);
  155. ce.innerHTML = '';
  156. if(checktype == 'select') {
  157. if(required != '0' && ($('typeoption_' + identifier) == null || $('typeoption_' + identifier).value == '0')) {
  158. warning(ce, '必填项目没有填写');
  159. return false;
  160. } else if(required == '0' && ($('typeoption_' + identifier) == null || $('typeoption_' + identifier).value == '0')) {
  161. ce.innerHTML = '<img src="' + IMGDIR + '/check_error.gif" width="16" height="16" class="vm" /> 请选择下一级';
  162. ce.className = "warning";
  163. return true;
  164. }
  165. }
  166. if(checktype == 'radio' || checktype == 'checkbox') {
  167. var nodes = $('typeoption_' + identifier).parentNode.parentNode.parentNode.getElementsByTagName('INPUT');
  168. var nodechecked = false;
  169. for(var i=0; i<nodes.length; i++) {
  170. if(nodes[i].id == 'typeoption_' + identifier) {
  171. if(nodes[i].checked) {
  172. nodechecked = true;
  173. }
  174. }
  175. }
  176. if(!nodechecked && required != '0') {
  177. warning(ce, '必填项目没有填写');
  178. return false;
  179. }
  180. }
  181. if(checktype == 'image') {
  182. var checkvalue = $('sortaid_' + identifier).value;
  183. } else {
  184. var checkvalue = $('typeoption_' + identifier).value;
  185. }
  186. if(required != '0') {
  187. if(checkvalue == '') {
  188. warning(ce, '必填项目没有填写');
  189. return false;
  190. } else {
  191. ce.innerHTML = '<img src="' + IMGDIR + '/check_right.gif" width="16" height="16" class="vm" />';
  192. }
  193. }
  194. if(checkvalue) {
  195. if(checktype == 'email' && !(/^[\-\.\w]+@[\.\-\w]+(\.\w+)+$/.test(checkvalue))) {
  196. warning(ce, '邮件地址不正确');
  197. return false;
  198. } else if((checktype == 'text' || checktype == 'textarea') && checkmaxlength != '0' && mb_strlen(checkvalue) > checkmaxlength) {
  199. warning(ce, '填写项目长度过长');
  200. return false;
  201. } else if((checktype == 'number' || checktype == 'range')) {
  202. if(isNaN(checkvalue)) {
  203. warning(ce, '数字填写不正确');
  204. return false;
  205. } else if(checkmaxnum != '0' && parseInt(checkvalue) > parseInt(checkmaxnum)) {
  206. warning(ce, '大于设置最大值');
  207. return false;
  208. } else if(checkminnum != '0' && parseInt(checkvalue) < parseInt(checkminnum)) {
  209. warning(ce, '小于设置最小值');
  210. return false;
  211. }
  212. } else if(checktype == 'url' && !(/(http[s]?|ftp):\/\/[^\/\.]+?\..+\w[\/]?$/i.test(checkvalue))) {
  213. warning(ce, '请正确填写以http://开头的URL地址');
  214. return false;
  215. }
  216. ce.innerHTML = '<img src="' + IMGDIR + '/check_right.gif" width="16" height="16" class="vm" />';
  217. }
  218. return true;
  219. }