register.dialog.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. (function($) {
  2. function Popup(el,id) {
  3. this.nameInput = $(el);
  4. this.nameInput.attr("readonly", "readonly");
  5. this.idInput = this.nameInput.prev();
  6. this.InputId = id;
  7. this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "selectItem");
  8. this.build();
  9. //this.selectItem();
  10. this.hide();
  11. };
  12. Popup.prototype = {
  13. build: function() {
  14. //清空按钮
  15. this.ClearController = $('<div class="button"><a href="#">清 空</a></div>').click(this.bindToObj(function(event) {
  16. this.selectItem("", "");
  17. this.hide();
  18. return false;
  19. }));
  20. this.popupController = $('<div class="selecting"></div>').append(this.ClearController);
  21. //特殊方法针对注册、添加弹出的dialog
  22. var list = $('<ul></ul>');
  23. if(this.InputId=="Nature"){
  24. $.each(natureItems,function(){
  25. list.append('<li><a href="#" id="' + this.id +'">'+ this.name +'</a></li>');
  26. });
  27. this.popupContent = $('<div></div>').addClass('natureDialog-content');
  28. }
  29. if(this.InputId=="Industry"){
  30. $.each(industryItems, function(){
  31. list.append('<li><a href="#" id="' + this.id +'">'+ this.name +'</a></li>');
  32. });
  33. this.popupContent = $('<div></div>').addClass('industryDialog-content');
  34. }
  35. if(this.InputId=="Province"){
  36. $.each(provinceItems, function(){
  37. list.append('<li><a href="#" id="' + this.id +'">'+ this.name +'</a></li>');
  38. });
  39. this.popupContent = $('<div></div>').addClass('ProvinceDialog-content');
  40. }
  41. if(this.InputId=="Category"){
  42. $.each(jobcategoryItems, function(){
  43. list.append('<li><a href="#" id="' + this.id +'">'+ this.name +'</a></li>');
  44. });
  45. this.popupContent = $('<div></div>').addClass('CategoryDialog-content');
  46. }
  47. if(this.InputId=="Nation"){
  48. $.each(nationItems, function(){
  49. list.append('<li><a href="#" id="' + this.id +'">'+ this.name +'</a></li>');
  50. });
  51. this.popupContent = $('<div></div>').addClass('CategoryDialog-content');
  52. }
  53. if(this.InputId=="Political"){
  54. $.each(politicalItems, function(){
  55. list.append('<li><a href="#" id="' + this.id +'">'+ this.name +'</a></li>');
  56. });
  57. this.popupContent = $('<div></div>').addClass('PoliticalDialog-content');
  58. }
  59. list.prependTo(this.popupContent);
  60. this.popupPanel = this.rootLayers = $('<div class="selectorDialog"></div>')
  61. .css({ display: "none", position: "absolute", zIndex: 900 })
  62. .append(this.popupController,this.popupContent)
  63. .appendTo(document.body);
  64. if ($.browser.msie && $.browser.version < 7) {
  65. this.ieframe = $('<iframe class="dialog_iframe" frameborder="0" src="#"></iframe>')
  66. .css({ position: "absolute", display: "none", zIndex: 99 })
  67. .insertBefore(this.popupPanel);
  68. this.rootLayers = this.rootLayers.add(this.ieframe);
  69. };
  70. $("a", this.popupContent).click(this.bindToObj(function(event) {
  71. this.selectItem($(event.target).attr("id"), $(event.target).attr("innerHTML"));
  72. this.hide();
  73. return false;
  74. }));
  75. //this.nameInput.change(this.bindToObj(function() { this.selectItem(); }));
  76. },
  77. selectItem: function(item, text) {
  78. this.nameInput.val(text);
  79. this.idInput.val(item);
  80. },
  81. show: function() {
  82. this.setPosition();
  83. this.rootLayers.css("display", "block");
  84. this.nameInput.unbind("focus", this.show);
  85. $(document.body).click(this.hideIfClickOutside);
  86. },
  87. hide: function() {
  88. this.rootLayers.css("display", "none");
  89. $(document.body).unbind("click", this.hideIfClickOutside);
  90. this.nameInput.focus(this.show);
  91. },
  92. hideIfClickOutside: function(event) {
  93. if (event.target != this.nameInput[0] && !this.insideSelector(event)) {
  94. this.hide();
  95. };
  96. },
  97. setPosition: function() {
  98. var offset = this.nameInput.offset();
  99. this.rootLayers.css({
  100. top: offset.top + this.nameInput.outerHeight(),
  101. left: $(window).width()/2 - this.rootLayers.outerWidth()/2
  102. //left: offset.left
  103. });
  104. if (this.ieframe) {
  105. this.ieframe.css({
  106. width: this.popupPanel.outerWidth(),
  107. height: this.popupPanel.outerHeight()
  108. });
  109. };
  110. },
  111. insideSelector: function(event) {
  112. var offset = this.popupPanel.offset();
  113. offset.right = offset.left + this.popupPanel.outerWidth();
  114. offset.bottom = offset.top + this.popupPanel.outerHeight();
  115. return event.pageY < offset.bottom &&
  116. event.pageY > offset.top &&
  117. event.pageX < offset.right &&
  118. event.pageX > offset.left;
  119. },
  120. bindToObj: function(fn) {
  121. var self = this;
  122. return function() { return fn.apply(self, arguments) };
  123. },
  124. bindMethodsToObj: function() {
  125. for (var i = 0; i < arguments.length; i++) {
  126. this[arguments[i]] = this.bindToObj(this[arguments[i]]);
  127. };
  128. }
  129. };
  130. $.fn.popup = function(id) {
  131. return this.each(function() {
  132. new Popup(this,id);
  133. });
  134. };
  135. })(jQuery);