PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jmvc/plugins/error/error.js

https://github.com/rentzsch/javascriptmvc
JavaScript | 202 lines | 195 code | 6 blank | 1 comment | 36 complexity | d138b647d85c655ae36100f5c73912a6 MD5 | raw file
  1. MVC.ApplicationError = MVC.JsonPModel.extend('application_error',
  2. {
  3. controller_name: "error",
  4. domain: 'https://damnit.jupiterit.com', name: 'error',
  5. textarea_text: "type description here",
  6. textarea_title: "Damn It!",
  7. close_time: 10,
  8. prompt_text: "Something just went wrong. Please describe your most recent actions and let us know what happened. We'll fix the problem.",
  9. prompt_user: true,
  10. generate_content: function(params){
  11. var content = [];
  12. // intentionally put HTML Content at the end
  13. for(var attr in params){
  14. if(params.hasOwnProperty(attr) && attr != 'toString' && attr != 'HTML Content') content.push(attr+':\n '+params[attr]);
  15. }
  16. if(params['HTML Content'])
  17. content.push('HTML Content'+':\n '+params['HTML Content']);
  18. return content.join('\n');
  19. },
  20. create_containing_div: function(){
  21. var div = document.createElement('div');
  22. div.id = '_application_error';
  23. div.style.position = MVC.Browser.Gecko ? 'fixed' : 'absolute';
  24. div.style.bottom = '0px';
  25. div.style.left = '0px';
  26. div.style.margin = '0px';
  27. return div;
  28. },
  29. create_title: function(){
  30. var title = document.createElement('div');
  31. title.style.backgroundImage = 'url('+MVC.mvc_root+'/plugins/error/background.png)';
  32. title.style.backgroundAttachment = 'scroll';
  33. title.style.backgroundRepeat = 'repeat-x';
  34. title.style.backgroundPosition = 'center top';
  35. title.style.font = 'bold 12pt verdana';
  36. title.style.color ='white';
  37. title.style.padding='0px 5px 0px 10px';
  38. title.innerHTML+= "<a style='float:right; width: 50px;text-decoration:underline; color: Red; padding-left: 25px; font-size: 10pt; cursor: pointer' onclick='MVC.ApplicationError.send()'>Close</a> "+
  39. "<span id='_error_seconds' style='float:right; font-size:10pt;'></span>"+this.textarea_title;
  40. return title;
  41. },
  42. create_form: function(callback){
  43. var form = document.createElement('form');
  44. var leftmargin = MVC.Browser.IE ? 5 : 10;
  45. form.id = '_error_form';
  46. form.onsubmit = callback;
  47. form.innerHTML ="<div style='float: left; width: 300px;margin-left:"+leftmargin+"px;'>"+this.prompt_text+"</div>"+
  48. "<input type='submit' value='Send' style='font-size: 12pt; float:right; margin: 17px 5px 0px 0px; width:60px;padding:5px;'/>"+
  49. "<textarea style='width: 335px; color: gray;' rows='"+(MVC.Browser.Gecko ? 2 : 3)+"' name='description' id='_error_text' "+
  50. "onfocus='MVC.ApplicationError.text_area_focus();' "+
  51. "onblur='MVC.ApplicationError.text_area_blur();' >"+this.textarea_text+"</textarea>";
  52. form.style.padding = '0px';
  53. form.style.font = 'normal 10pt verdana';
  54. form.style.margin = '0px';
  55. form.style.backgroundColor = '#FAE8CD';
  56. return form;
  57. },
  58. create_send_function: function(error){
  59. this.send = MVC.Function.bind(function(event){
  60. var params = {error: {}}, description;
  61. params.error.subject = error.subject;
  62. if((description = MVC.$E('_error_text'))){error['User Description'] = description.value;}
  63. if(this.prompt_user) {
  64. this.pause_count_down();
  65. document.body.removeChild(MVC.$E('_application_error'));
  66. }
  67. params.error.content = this.generate_content(error);
  68. this.kill_event(event);
  69. params.user_crypted_key = APPLICATION_KEY;
  70. this.create(params, function(){});
  71. }, this);
  72. },
  73. create_dom: function(error){
  74. if(MVC.$E('_application_error')) return;
  75. var div = this.create_containing_div();
  76. document.body.appendChild(div);
  77. div.appendChild(this.create_title());
  78. div.appendChild(this.create_form(this.send));
  79. this.set_width();
  80. var seconds_remaining;
  81. var timer;
  82. this.count_down = function(){
  83. seconds_remaining --;
  84. document.getElementById('_error_seconds').innerHTML = 'This will close in '+seconds_remaining+' seconds.';
  85. if(seconds_remaining == 0){
  86. MVC.ApplicationError.pause_count_down();
  87. MVC.ApplicationError.send();
  88. }
  89. };
  90. this.start_count_down = MVC.Function.bind(function(){
  91. seconds_remaining = this.close_time;
  92. MVC.$E('_error_seconds').innerHTML = 'This will close in '+seconds_remaining+' seconds.';
  93. timer = setInterval(MVC.ApplicationError.count_down, 1000);
  94. }, this);
  95. this.pause_count_down = function(){
  96. clearInterval(timer);
  97. timer = null;
  98. MVC.$E('_error_seconds').innerHTML = '';
  99. };
  100. this.start_count_down();
  101. },
  102. prompt_and_send: function(error){
  103. this.create_send_function(error);
  104. if(this.prompt_user == true)
  105. this.create_dom(error);
  106. else
  107. this.send();
  108. },
  109. notify: function(e){
  110. e = this.transform_error(e);
  111. MVC.Object.extend(e,{
  112. 'Browser' : navigator.userAgent,
  113. 'Page' : location.href,
  114. 'HTML Content' : document.documentElement.innerHTML.replace(/\n/g,"\n ").replace(/\t/g," ")
  115. });
  116. if(Error && new Error().stack) e.Stack = new Error().stack;
  117. if(!e.subject) e.subject = 'ApplicationError on: '+window.location.href;
  118. this.prompt_and_send(e);
  119. return false;
  120. },
  121. text_area_focus: function(){
  122. var area = document.getElementById('_error_text');
  123. if(area.value == this.textarea_text) area.value = '';
  124. area.style.color = 'black';
  125. MVC.ApplicationError.pause_count_down();
  126. },
  127. text_area_blur: function(){
  128. var area = MVC.$E('_error_text');
  129. if(area.value == this.textarea_text || area.value == '') area.value = this.textarea_text;
  130. area.style.color = 'gray';
  131. MVC.ApplicationError.start_count_down();
  132. },
  133. set_width: function(){
  134. var cont, width;
  135. if(!(cont = MVC.$E('_application_error') )) return;
  136. width = document.body.clientWidth;
  137. cont.style.width = width+'px';
  138. MVC.$E('_error_text').style.width = (width-400)+'px';
  139. },
  140. transform_error: function(error){
  141. if(typeof error == 'string'){
  142. var old = error;
  143. error = { toString: function(){return old;}};
  144. error.message = old;
  145. }
  146. if(MVC.Browser.Opera && error.message) {
  147. var error_arr = error.message.match('Backtrace');
  148. if(error_arr) {
  149. var message = error.message;
  150. error.message = message.substring(0,error_arr.index);
  151. error.backtrace = message.substring(error_arr.index+11,message.length);
  152. }
  153. }
  154. return error;
  155. },
  156. kill_event: function(event) {
  157. if(! event) return;
  158. event.cancelBubble = true;
  159. if (event.stopPropagation) event.stopPropagation();
  160. if (event.preventDefault) event.preventDefault();
  161. }
  162. },
  163. {}
  164. )
  165. MVC.error_handler = function(msg, url, l){
  166. var e = {
  167. message: msg,
  168. fileName: url,
  169. lineNumber: l
  170. };
  171. MVC.ApplicationError.notify(e);
  172. return false;
  173. };
  174. if(MVC.Controller){
  175. MVC.Controller._dispatch_action = function(instance, action_name, params){
  176. try{
  177. return instance[action_name](params);
  178. }catch(e){
  179. MVC.ApplicationError.kill_event(params.event);
  180. e = MVC.ApplicationError.transform_error(e);
  181. MVC.Object.extend(e,{
  182. 'Controller': instance.klass.className,
  183. 'Action': action_name,
  184. subject: 'Dispatch Error: '+((e.message && typeof(e.message) == 'string') ? e.message : e.toString())
  185. });
  186. MVC.ApplicationError.notify(e);
  187. return false;
  188. }
  189. };
  190. }
  191. if(window.attachEvent) {
  192. window.attachEvent("onresize", MVC.ApplicationError.set_width);
  193. }else{
  194. window.addEventListener('resize', MVC.ApplicationError.set_width, false);
  195. }
  196. window.onerror = MVC.error_handler;