oscshop_common.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. $(function(){
  2. var breadcrumbs=$('#breadcrumbs li').first().find('a').text();
  3. var breadcrumbs2=$('#breadcrumbs li.active').text();
  4. $('#sidebar .menu-text').each(function(i){
  5. var t=$(this).text();
  6. if(t==breadcrumbs){
  7. $('#sidebar>ul>li').eq(i).addClass('open');
  8. $('#sidebar>ul>li').eq(i).find('ul').css({'display': 'block'});
  9. }
  10. });
  11. $('.submenu a span.url-title').each(function(i){
  12. var t=$(this).text();
  13. if(t==breadcrumbs2){
  14. $(this).css({'color': '#f60'});
  15. }
  16. });
  17. $('.delete').click(function(){
  18. var f=confirm('确认要执行该操作吗?');
  19. if(f==false){
  20. return false;
  21. }
  22. if($(this).hasClass("one")){
  23. if($('input:checked').size()==0){
  24. alert('请选择一个条目');
  25. return false;
  26. }else{
  27. var target,query,form;
  28. form=$('#table');
  29. target = $(this).attr('href');
  30. query = form.find('input,select,textarea').serialize();
  31. $.post(target,query).success(function(data){});
  32. }
  33. }
  34. });
  35. //全选的实现
  36. $(".check-all").click(function(){
  37. $(".ids").prop("checked", this.checked);
  38. });
  39. $(".ids").click(function(){
  40. var option = $(".ids");
  41. option.each(function(i){
  42. if(!this.checked){
  43. $(".check-all").prop("checked", false);
  44. return false;
  45. }else{
  46. $(".check-all").prop("checked", true);
  47. }
  48. });
  49. });
  50. var Osc = window.Oscshop={};
  51. /* 设置表单的值 */
  52. Osc.setValue = function(name, value){
  53. var first = name.substr(0,1), input, i = 0, val;
  54. if(value === "") return;
  55. if("#" === first || "." === first){
  56. input = $(name);
  57. } else {
  58. input = $("[name='" + name + "']");
  59. }
  60. if(input.eq(0).is(":radio")) { //单选按钮
  61. input.filter("[value='" + value + "']").each(function(){this.checked = true});
  62. } else if(input.eq(0).is(":checkbox")) { //复选框
  63. if(!$.isArray(value)){
  64. val = new Array();
  65. val[0] = value;
  66. } else {
  67. val = value;
  68. }
  69. for(i = 0, len = val.length; i < len; i++){
  70. input.filter("[value='" + val[i] + "']").each(function(){this.checked = true});
  71. }
  72. } else { //其他表单选项直接设置值
  73. input.val(value);
  74. }
  75. }
  76. });
  77. // Autocomplete */
  78. (function($) {
  79. $.fn.autocomplete = function(option) {
  80. return this.each(function() {
  81. this.timer = null;
  82. this.items = new Array();
  83. $.extend(this, option);
  84. $(this).attr('autocomplete', 'off');
  85. // Focus
  86. $(this).on('focus', function() {
  87. this.request();
  88. });
  89. // Blur
  90. $(this).on('blur', function() {
  91. setTimeout(function(object) {
  92. object.hide();
  93. }, 200, this);
  94. });
  95. // Keydown
  96. $(this).on('keydown', function(event) {
  97. switch(event.keyCode) {
  98. case 27: // escape
  99. this.hide();
  100. break;
  101. default:
  102. this.request();
  103. break;
  104. }
  105. });
  106. /**/
  107. // Click
  108. this.click = function(event) {
  109. event.preventDefault();
  110. value = $(event.target).parent().attr('data-value');
  111. if (value && this.items[value]) {
  112. this.select(this.items[value]);
  113. }
  114. }
  115. // Show
  116. this.show = function() {
  117. var pos = $(this).position();
  118. $(this).siblings('ul.dropdown-menu').css({
  119. top: pos.top + $(this).outerHeight(),
  120. left: pos.left
  121. });
  122. $(this).siblings('ul.dropdown-menu').show();
  123. }
  124. // Hide
  125. this.hide = function() {
  126. $(this).siblings('ul.dropdown-menu').hide();
  127. }
  128. // Request
  129. this.request = function() {
  130. clearTimeout(this.timer);
  131. this.timer = setTimeout(function(object) {
  132. object.source($(object).val(), $.proxy(object.response, object));
  133. }, 200, this);
  134. }
  135. // Response
  136. this.response = function(json) {
  137. html = '';
  138. if (json.length) {
  139. for (i = 0; i < json.length; i++) {
  140. this.items[json[i]['value']] = json[i];
  141. }
  142. for (i = 0; i < json.length; i++) {
  143. if (!json[i]['category']) {
  144. html += '<li data-value="' + json[i]['value'] + '"><a href="#">' + json[i]['label'] + '</a></li>';
  145. }
  146. }
  147. // Get all the ones with a categories
  148. var category = new Array();
  149. for (i = 0; i < json.length; i++) {
  150. if (json[i]['category']) {
  151. if (!category[json[i]['category']]) {
  152. category[json[i]['category']] = new Array();
  153. category[json[i]['category']]['name'] = json[i]['category'];
  154. category[json[i]['category']]['item'] = new Array();
  155. }
  156. category[json[i]['category']]['item'].push(json[i]);
  157. }
  158. }
  159. for (i in category) {
  160. html += '<li class="dropdown-header">' + category[i]['name'] + '</li>';
  161. for (j = 0; j < category[i]['item'].length; j++) {
  162. html += '<li data-value="' + category[i]['item'][j]['value'] + '"><a href="#">&nbsp;&nbsp;&nbsp;' + category[i]['item'][j]['label'] + '</a></li>';
  163. }
  164. }
  165. }
  166. if (html) {
  167. this.show();
  168. } else {
  169. this.hide();
  170. }
  171. $(this).siblings('ul.dropdown-menu').html(html);
  172. }
  173. $(this).after('<ul class="dropdown-menu"></ul>');
  174. $(this).siblings('ul.dropdown-menu').delegate('a', 'click', $.proxy(this.click, this));
  175. });
  176. }
  177. })(window.jQuery);