PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/meet001/static/js/meetup_group_modify.js

http://meetup001.googlecode.com/
JavaScript | 270 lines | 208 code | 35 blank | 27 comment | 33 complexity | 0991c2d8fbdba8ec4014ff01ba392747 MD5 | raw file
  1. var old_g_details;
  2. var new_g_details;
  3. var reset_pressed;
  4. //1--- reset button is pressed.2--- load function is revoked.3--- load completed/start point.
  5. // prepare the form when the DOM is ready
  6. $(document).ready(function() {
  7. var name = $('#g_update_name'),
  8. title = $('#g_update_title'),
  9. description = $('#g_update_desc'),
  10. member_limit = $('#g_update_limit'),
  11. photo = $('#g_update_photo'),
  12. options = { //target: '#output1', // target element(s) to be updated with server response
  13. beforeSubmit: showRequest, // pre-submit callback
  14. success: showResponse, // post-submit callback
  15. iframe: true,
  16. // other available options:
  17. //url: '/group/'+ groupId + '/update/',
  18. type: 'post',
  19. dataType: 'json',
  20. // $.ajax options can be used here too, for example:
  21. timeout: 3000,
  22. },
  23. allFields = $([]).add(name).add(title).add(description).add(member_limit).add(photo),
  24. tips = $("#validateTips"),
  25. submitted = false,
  26. reset_pressed = false;
  27. old_g_details={
  28. name:$('#g_update_name').val(),
  29. title:$('#g_update_title').val(),
  30. description:$('#g_update_desc').val(),
  31. member_limit:$('#g_update_limit').val(),
  32. photo:$('#g_update_photo').val(),
  33. city:$('#city').val(),
  34. district:last_dist
  35. };
  36. $('#district').load('/location/district/', {'city':$('#city').val()}, function(){
  37. $('form#g_update_form select#district').each(function(){
  38. this.value=last_dist;
  39. });
  40. });
  41. $("#dialog").dialog({
  42. bgiframe: true,
  43. autoOpen: false,
  44. height: 460,
  45. width: 400,
  46. resizable: false,
  47. modal: true,
  48. show: 'drop',
  49. hide: 'drop',
  50. buttons: {
  51. '????':function(){
  52. reset();
  53. },
  54. '????': function() {
  55. var bValid = true;
  56. allFields.removeClass('ui-state-error');
  57. bValid = bValid && checkLength(name,"????",2,100);
  58. bValid = bValid && checkLength(title,"????",2,100);
  59. bValid = bValid && checkLength(member_limit,"??????",1,5);
  60. bValid = bValid && checkLength(description,"????",0,5000);
  61. //currently no reg test will be execute
  62. //bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
  63. // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
  64. //bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
  65. //bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
  66. if (bValid) {
  67. //if validated, save the form to the server.
  68. $('#g_update_form').submit();
  69. submitted = true;
  70. $(this).dialog('close');
  71. }
  72. }
  73. },
  74. close: function() {
  75. //remove the checkbox for the reminder.
  76. //and the message in the tips
  77. //and the errors leave in fields
  78. if(submitted == false) reset();
  79. $('#notify_all').removeAttr("checked");
  80. }
  81. });
  82. // bind form using 'ajaxForm'
  83. //$('#g_update_form').ajaxForm(options);
  84. $('#g_update_form').submit(function() {
  85. // inside event callbacks 'this' is the DOM element so we first
  86. // wrap it in a jQuery object and then invoke ajaxSubmit
  87. $(this).ajaxSubmit(options);
  88. // !!! Important !!!
  89. // always return false to prevent standard browser submit and page navigation
  90. return false;
  91. });
  92. $('#edit_group').click(function() {
  93. $('#dialog').dialog('open');
  94. })
  95. .hover(
  96. function(){
  97. $(this).addClass("ui-state-hover");
  98. },
  99. function(){
  100. $(this).removeClass("ui-state-hover");
  101. }
  102. ).mousedown(function(){
  103. $(this).addClass("ui-state-active");
  104. })
  105. .mouseup(function(){
  106. $(this).removeClass("ui-state-active");
  107. });
  108. });
  109. function updateTips(t) {
  110. tips.text(t).effect("highlight",{},1500);
  111. }
  112. function reset(){
  113. var reload_d = false;
  114. reset_pressed = true;
  115. updateTips("???????????");
  116. allFields.removeClass('ui-state-error');
  117. $('#g_update_name').val(old_g_details.name);
  118. $('#g_update_title').val(old_g_details.title);
  119. $('#g_update_desc').val(old_g_details.description);
  120. $('#g_update_limit').val(old_g_details.member_limit);
  121. $('#g_update_photo').val(old_g_details.photo);
  122. $('form#g_update_form select#city').each(function(){
  123. if(this.value!=old_g_details.city){
  124. this.value=old_g_details.city;
  125. reload_d = true;
  126. }
  127. });
  128. if(reload_d){
  129. $('#district').load('/location/district/', {'city':$('#city').val()}, function(){
  130. $('form#g_update_form select#district').each(function(){
  131. this.value=old_g_details.district;
  132. });
  133. });
  134. }
  135. else{
  136. $('form#g_update_form select#district').each(function(){
  137. this.value=old_g_details.district;
  138. });
  139. }
  140. }
  141. function checkLength(o,n,min,max) {
  142. if ( o.val().length > max || o.val().length < min ) {
  143. o.addClass('ui-state-error');
  144. updateTips("????" + n +"???"+ " ?????"+min+" ????? "+max+"?");
  145. return false;
  146. } else {
  147. return true;
  148. }
  149. }
  150. function checkRegexp(o,regexp,n) {
  151. if ( !( regexp.test( o.val() ) ) ) {
  152. o.addClass('ui-state-error');
  153. updateTips(n);
  154. return false;
  155. } else {
  156. return true;
  157. }
  158. }
  159. // pre-submit callback
  160. function showRequest(formData, jqForm, options) {
  161. // formData is an array; here we use $.param to convert it to a string to display it
  162. // but the form plugin does this for you automatically when it submits the data
  163. // var queryString = $.param(formData);
  164. new_g_details={
  165. name:$('#g_update_name').val(),
  166. title:$('#g_update_title').val(),
  167. description:$('#g_update_desc').val(),
  168. member_limit:$('#g_update_limit').val(),
  169. photo:$('#g_update_photo').val(),
  170. city:$('#city').val(),
  171. district:$('#district').val()
  172. };
  173. if(reset_pressed)
  174. {
  175. reset_pressed = false;
  176. return false;
  177. }
  178. if(new_g_details.name == old_g_details.name
  179. && new_g_details.title == old_g_details.title
  180. && new_g_details.description == old_g_details.description
  181. && new_g_details.member_limit == old_g_details.member_limit
  182. && new_g_details.city == old_g_details.city
  183. && new_g_details.district == old_g_details.district
  184. && new_g_details.photo == old_g_details.photo)
  185. {
  186. return false;
  187. }
  188. return true;
  189. }
  190. // post-submit callback
  191. function showResponse(responseText, statusText) {
  192. //firstly check wether the city is changed.
  193. var reload_d = false;
  194. if(old_g_details.city != new_g_details.city){
  195. reload_d = true;
  196. }
  197. old_g_details = new_g_details;
  198. new_g_details='';
  199. $('#g_update_photo').val("");
  200. old_g_details.photo="";
  201. $('.g_name_field').each(function(){
  202. this.textContent = responseText.name;
  203. });
  204. $('.g_title_field').each(function(){
  205. this.textContent = responseText.title;
  206. });
  207. $('.g_description_field').each(function(){
  208. this.textContent = responseText.description;
  209. });
  210. $('.g_memeber_limit').each(function(){
  211. this.textContent = responseText.member_limit;
  212. });
  213. if(responseText.picture_url != undefined){
  214. $('a.g_pic_url').each(function(){
  215. var split_index = this.href.lastIndexOf('/group_pic/')
  216. var prefix = this.href.substring(0,split_index)
  217. this.href=prefix +'/group_pic/'+ responseText.picture_url;
  218. });
  219. $('img.g_pic_url').each(function(){
  220. var split_index = this.src.lastIndexOf('/group_pic/')
  221. var prefix = this.src.substring(0,split_index)
  222. this.src=prefix +'/group_pic/'+ responseText.picture_url;
  223. });
  224. }
  225. //location info
  226. $('#g_location').each(function(){
  227. this.textContent=responseText.city + responseText.district;
  228. });
  229. $('#city').each(function(){
  230. this.value=responseText.city;
  231. });
  232. $('#district').each(function(){
  233. this.value=responseText.district;
  234. });
  235. }