think.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* 扩展Movesay对象 */
  2. (function($){
  3. /**
  4. * 获取Movesay基础配置
  5. * @type {object}
  6. */
  7. var Movesay = window.Think;
  8. /* 基础对象检测 */
  9. Movesay || $.error("Movesay基础配置没有正确加载!");
  10. /**
  11. * 解析URL
  12. * @param {string} url 被解析的URL
  13. * @return {object} 解析后的数据
  14. */
  15. Movesay.parse_url = function(url){
  16. var parse = url.match(/^(?:([a-z]+):\/\/)?([\w-]+(?:\.[\w-]+)+)?(?::(\d+))?([\w-\/]+)?(?:\?((?:\w+=[^#&=\/]*)?(?:&\w+=[^#&=\/]*)*))?(?:#([\w-]+))?$/i);
  17. parse || $.error("url格式不正确!");
  18. return {
  19. "scheme" : parse[1],
  20. "host" : parse[2],
  21. "port" : parse[3],
  22. "path" : parse[4],
  23. "query" : parse[5],
  24. "fragment" : parse[6]
  25. };
  26. }
  27. Movesay.parse_str = function(str){
  28. var value = str.split("&"), vars = {}, param;
  29. for(val in value){
  30. param = value[val].split("=");
  31. vars[param[0]] = param[1];
  32. }
  33. return vars;
  34. }
  35. Movesay.parse_name = function(name, type){
  36. if(type){
  37. /* 下划线转驼峰 */
  38. name.replace(/_([a-z])/g, function($0, $1){
  39. return $1.toUpperCase();
  40. });
  41. /* 首字母大写 */
  42. name.replace(/[a-z]/, function($0){
  43. return $0.toUpperCase();
  44. });
  45. } else {
  46. /* 大写字母转小写 */
  47. name = name.replace(/[A-Z]/g, function($0){
  48. return "_" + $0.toLowerCase();
  49. });
  50. /* 去掉首字符的下划线 */
  51. if(0 === name.indexOf("_")){
  52. name = name.substr(1);
  53. }
  54. }
  55. return name;
  56. }
  57. //scheme://host:port/path?query#fragment
  58. Movesay.U = function(url, vars, suffix){
  59. var info = this.parse_url(url), path = [], param = {}, reg;
  60. /* 验证info */
  61. info.path || $.error("url格式错误!");
  62. url = info.path;
  63. /* 组装URL */
  64. if(0 === url.indexOf("/")){ //路由模式
  65. this.MODEL[0] == 0 && $.error("该URL模式不支持使用路由!(" + url + ")");
  66. /* 去掉右侧分割符 */
  67. if("/" == url.substr(-1)){
  68. url = url.substr(0, url.length -1)
  69. }
  70. url = ("/" == this.DEEP) ? url.substr(1) : url.substr(1).replace(/\//g, this.DEEP);
  71. url = "/" + url;
  72. } else { //非路由模式
  73. /* 解析URL */
  74. path = url.split("/");
  75. path = [path.pop(), path.pop(), path.pop()].reverse();
  76. path[1] || $.error("Movesay.U(" + url + ")没有指定控制器");
  77. if(path[0]){
  78. param[this.VAR[0]] = this.MODEL[1] ? path[0].toLowerCase() : path[0];
  79. }
  80. param[this.VAR[1]] = this.MODEL[1] ? this.parse_name(path[1]) : path[1];
  81. param[this.VAR[2]] = path[2].toLowerCase();
  82. url = "?" + $.param(param);
  83. }
  84. /* 解析参数 */
  85. if(typeof vars === "string"){
  86. vars = this.parse_str(vars);
  87. } else if(!$.isPlainObject(vars)){
  88. vars = {};
  89. }
  90. /* 解析URL自带的参数 */
  91. info.query && $.extend(vars, this.parse_str(info.query));
  92. if(vars){
  93. url += "&" + $.param(vars);
  94. }
  95. if(0 != this.MODEL[0]){
  96. url = url.replace("?" + (path[0] ? this.VAR[0] : this.VAR[1]) + "=", "/")
  97. .replace("&" + this.VAR[1] + "=", this.DEEP)
  98. .replace("&" + this.VAR[2] + "=", this.DEEP)
  99. .replace(/(\w+=&)|(&?\w+=$)/g, "")
  100. .replace(/[&=]/g, this.DEEP);
  101. /* 添加伪静态后缀 */
  102. if(false !== suffix){
  103. suffix = suffix || this.MODEL[2].split("|")[0];
  104. if(suffix){
  105. url += "." + suffix;
  106. }
  107. }
  108. }
  109. url = this.APP + url;
  110. return url;
  111. }
  112. /* 设置表单的值 */
  113. Movesay.setValue = function(name, value){
  114. var first = name.substr(0,1), input, i = 0, val;
  115. if(value === "") return;
  116. if("#" === first || "." === first){
  117. input = $(name);
  118. } else {
  119. input = $("[name='" + name + "']");
  120. }
  121. if(input.eq(0).is(":radio")) { //单选按钮
  122. input.filter("[value='" + value + "']").each(function(){this.checked = true});
  123. } else if(input.eq(0).is(":checkbox")) { //复选框
  124. if(!$.isArray(value)){
  125. val = new Array();
  126. val[0] = value;
  127. } else {
  128. val = value;
  129. }
  130. for(i = 0, len = val.length; i < len; i++){
  131. input.filter("[value='" + val[i] + "']").each(function(){this.checked = true});
  132. }
  133. } else { //其他表单选项直接设置值
  134. input.val(value);
  135. }
  136. }
  137. })(jQuery);