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

/attachments/script/util.js

https://github.com/maxogden/relaxed.tv
JavaScript | 201 lines | 184 code | 16 blank | 1 comment | 43 complexity | a4c19c9bcb1aa5c40d664c154934b7b3 MD5 | raw file
  1. var util = function() {
  2. $.fn.serializeObject = function() {
  3. var o = {};
  4. var a = this.serializeArray();
  5. $.each(a, function() {
  6. if (o[this.name]) {
  7. if (!o[this.name].push) {
  8. o[this.name] = [o[this.name]];
  9. }
  10. o[this.name].push(this.value || '');
  11. } else {
  12. o[this.name] = this.value || '';
  13. }
  14. });
  15. return o;
  16. };
  17. function inURL(url, str) {
  18. var exists = false;
  19. if ( url.indexOf( str ) > -1 ) {
  20. exists = true;
  21. }
  22. return exists;
  23. }
  24. function registerEmitter() {
  25. var Emitter = function(obj) {
  26. this.emit = function(obj, channel) {
  27. if (!channel) var channel = 'data';
  28. this.trigger(channel, obj);
  29. };
  30. };
  31. MicroEvent.mixin(Emitter);
  32. app.emitter = new Emitter();
  33. }
  34. function listenFor(keys) {
  35. var shortcuts = { // from jquery.hotkeys.js
  36. 8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause",
  37. 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home",
  38. 37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del",
  39. 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7",
  40. 104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/",
  41. 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8",
  42. 120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 191: "/", 224: "meta"
  43. }
  44. window.addEventListener("keyup", function(e) {
  45. var pressed = shortcuts[e.keyCode];
  46. if(_.include(keys, pressed)) app.emitter.emit("keyup", pressed);
  47. }, false);
  48. }
  49. function observeExit(elem, callback) {
  50. var cancelButton = elem.find('.cancelButton');
  51. app.emitter.on('esc', function() {
  52. cancelButton.click();
  53. app.emitter.clear('esc');
  54. });
  55. cancelButton.click(callback);
  56. }
  57. function show( thing ) {
  58. $('.' + thing ).show();
  59. $('.' + thing + '-overlay').show();
  60. }
  61. function hide( thing ) {
  62. $('.' + thing ).hide();
  63. $('.' + thing + '-overlay').hide();
  64. }
  65. function position( thing, elem, offset ) {
  66. var position = $(elem.target).offset();
  67. if (offset) {
  68. if (offset.top) position.top += offset.top;
  69. if (offset.left) position.left += offset.left;
  70. }
  71. $('.' + thing + '-overlay').show().click(function(e) {
  72. $(e.target).hide();
  73. $('.' + thing).hide();
  74. });
  75. $('.' + thing).show().css({top: position.top + $(elem.target).height(), left: position.left});
  76. }
  77. function render( template, target, options ) {
  78. if ( !options ) options = {data: {}};
  79. if ( !options.data ) options = {data: options};
  80. var html = $.mustache( $( "." + template + "Template:first" ).html(), options.data );
  81. if (target instanceof jQuery) {
  82. var targetDom = target;
  83. } else {
  84. var targetDom = $( "." + target + ":first" );
  85. }
  86. if( options.append ) {
  87. targetDom.append( html );
  88. } else {
  89. targetDom.html( html );
  90. }
  91. if (template in app.after) app.after[template]();
  92. }
  93. function notify( message, options ) {
  94. if (!options) var options = {};
  95. $('#notification-container').show();
  96. $('#notification-message').text(message);
  97. if (!options.loader) $('.notification-loader').hide();
  98. if (options.loader) $('.notification-loader').show();
  99. if (!options.persist) setTimeout(function() { $('#notification-container').hide() }, 3000);
  100. }
  101. function formatMetadata(data) {
  102. out = '<dl>';
  103. $.each(data, function(key, val) {
  104. if (typeof(val) == 'string' && key[0] != '_') {
  105. out = out + '<dt>' + key + '<dd>' + val;
  106. } else if (typeof(val) == 'object' && key != "geometry" && val != null) {
  107. if (key == 'properties') {
  108. $.each(val, function(attr, value){
  109. out = out + '<dt>' + attr + '<dd>' + value;
  110. })
  111. } else {
  112. out = out + '<dt>' + key + '<dd>' + val.join(', ');
  113. }
  114. }
  115. });
  116. out = out + '</dl>';
  117. return out;
  118. }
  119. function getBaseURL(url) {
  120. var baseURL = "";
  121. if ( inURL(url, '_design') ) {
  122. if (inURL(url, '_rewrite')) {
  123. var path = url.split("#")[0];
  124. if (path[path.length - 1] === "/") {
  125. baseURL = "";
  126. } else {
  127. baseURL = '_rewrite/';
  128. }
  129. } else {
  130. baseURL = '_rewrite/';
  131. }
  132. }
  133. return baseURL;
  134. }
  135. var persist = {
  136. restore: function() {
  137. $('.persist').each(function(i, el) {
  138. var inputId = $(el).attr('id');
  139. if(localStorage.getItem(inputId)) $('#' + inputId).val(localStorage.getItem(inputId));
  140. })
  141. },
  142. save: function(id) {
  143. localStorage.setItem(id, $('#' + id).val());
  144. },
  145. clear: function() {
  146. $('.persist').each(function(i, el) {
  147. localStorage.removeItem($(el).attr('id'));
  148. })
  149. }
  150. }
  151. // simple debounce adapted from underscore.js
  152. function delay(func, wait) {
  153. return function() {
  154. var context = this, args = arguments;
  155. var throttler = function() {
  156. delete app.timeout;
  157. func.apply(context, args);
  158. };
  159. if (!app.timeout) app.timeout = setTimeout(throttler, wait);
  160. };
  161. };
  162. function resetForm(form) {
  163. $(':input', form)
  164. .not(':button, :submit, :reset, :hidden')
  165. .val('')
  166. .removeAttr('checked')
  167. .removeAttr('selected');
  168. }
  169. return {
  170. inURL: inURL,
  171. registerEmitter: registerEmitter,
  172. listenFor: listenFor,
  173. show: show,
  174. hide: hide,
  175. position: position,
  176. render: render,
  177. notify: notify,
  178. observeExit: observeExit,
  179. formatMetadata: formatMetadata,
  180. getBaseURL: getBaseURL,
  181. resetForm: resetForm,
  182. delay: delay,
  183. persist: persist
  184. };
  185. }();