1/* 2 * Sidr 3 * https://github.com/artberri/sidr 4 * 5 * Copyright (c) 2013 Alberto Varela 6 * Licensed under the MIT license. 7 */ 8 9; (function ($) { 10 11 var sidrMoving = false, 12 sidrOpened = false; 13 14 // Private methods 15 var privateMethods = { 16 // Check for valids urls 17 // From : http://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-an-url 18 isUrl: function (str) { 19 var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol 20 '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name 21 '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address 22 '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path 23 '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string 24 '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator 25 if (!pattern.test(str)) { 26 return false; 27 } else { 28 return true; 29 } 30 }, 31 // Loads the content into the menu bar 32 loadContent: function ($menu, content) { 33 $menu.html(content); 34 }, 35 // Add sidr prefixes 36 addPrefix: function ($element) { 37 var elementId = $element.attr('id'), 38 elementClass = $element.attr('class'); 39 40 if (typeof elementId === 'string' && '' !== elementId) { 41 $element.attr('id', elementId.replace(/([A-Za-z0-9_.\-]+)/g, 'sidr-id-$1')); 42 } 43 if (typeof elementClass === 'string' && '' !== elementClass && 'sidr-inner' !== elementClass) { 44 $element.attr('class', elementClass.replace(/([A-Za-z0-9_.\-]+)/g, 'sidr-class-$1')); 45 } 46 $element.removeAttr('style'); 47 }, 48 execute: function (action, name, callback) { 49 // Check arguments 50 if (typeof name === 'function') { 51 callback = name; 52 name = 'sidr'; 53 } 54 else if (!name) { 55 name = 'sidr'; 56 } 57 58 // Declaring 59 var $menu = $('#' + name), 60 $body = $($menu.data('body')), 61 $html = $('html'), 62 menuWidth = $menu.outerWidth(true), 63 speed = $menu.data('speed'), 64 side = $menu.data('side'), 65 bodyAnimation, 66 menuAnimation, 67 scrollTop; 68 69 // Open Sidr 70 if ('open' === action || ('toogle' === action && !$menu.is(':visible'))) { 71 // Check if we can open it 72 if ($menu.is(':visible') || sidrMoving) { 73 return; 74 } 75 76 // If another menu opened close first 77 if (sidrOpened !== false) { 78 methods.close(sidrOpened, function () { 79 methods.open(name); 80 }); 81 82 return; 83 } 84 85 // Lock sidr 86 sidrMoving = true; 87 88 // Left or right? 89 if (side === 'left') { 90 bodyAnimation = { left: menuWidth + 'px' }; 91 menuAnimation = { left: '0px' }; 92 } 93 else { 94 bodyAnimation = { right: menuWidth + 'px' }; 95 menuAnimation = { right: '0px' }; 96 } 97 98 // Prepare page 99 scrollTop = $html.scrollTop(); 100 $html.css('overflow-x', 'hidden').scrollTop(scrollTop); 101 102 // Open menu 103 $body.css({ 104 width: $body.width(), 105 position: 'fixed' 106 }).animate(bodyAnimation, speed); 107 $menu.css('display', 'block').animate(menuAnimation, speed, function () { 108 sidrMoving = false; 109 sidrOpened = name; 110 // Callback 111 if (typeof callback === 'function') { 112 callback(name); 113 } 114 }); 115 } 116 // Close Sidr 117 else { 118 // Check if we can close it 119 if (!$menu.is(':visible') || sidrMoving) { 120 return; 121 } 122 123 // Lock sidr 124 sidrMoving = true; 125 126 // Right or left menu? 127 if (side === 'left') { 128 bodyAnimation = { left: 0 }; 129 menuAnimation = { left: '-' + menuWidth + 'px' }; 130 } 131 else { 132 bodyAnimation = { right: 0 }; 133 menuAnimation = { right: '-' + menuWidth + 'px' }; 134 } 135 136 // Close menu 137 scrollTop = $html.scrollTop(); 138 $html.removeAttr('style').scrollTop(scrollTop); 139 $body.animate(bodyAnimation, speed); 140 $menu.animate(menuAnimation, speed, function () { 141 $menu.removeAttr('style'); 142 $body.removeAttr('style'); 143 $('html').removeAttr('style'); 144 sidrMoving = false; 145 sidrOpened = false; 146 // Callback 147 if (typeof callback === 'function') { 148 callback(name); 149 } 150 }); 151 } 152 } 153 }; 154 155 // Sidr public methods 156 var methods = { 157 open: function (name, callback) { 158 privateMethods.execute('open', name, callback); 159 }, 160 close: function (name, callback) { 161 privateMethods.execute('close', name, callback); 162 }, 163 toogle: function (name, callback) { 164 privateMethods.execute('toogle', name, callback); 165 } 166 }; 167 168 $.sidr = function (method) { 169 170 if (methods[method]) { 171 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 172 } else if (typeof method === 'function' || typeof method === 'string' || !method) { 173 return methods.toogle.apply(this, arguments); 174 } else { 175 $.error('Method ' + method + ' does not exist on jQuery.sidr'); 176 } 177 178 }; 179 180 $.fn.sidr = function (options) { 181 var settings = $.extend({ 182 name: 'sidr', // Name for the 'sidr' 183 speed: 200, // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds) 184 side: 'left', // Accepts 'left' or 'right' 185 source: null, // Override the source of the content. 186 renaming: true, // The ids and classes will be prepended with a prefix when loading existent content 187 body: 'body' // Page container selector, 188 }, options); 189 190 var name = settings.name,
191 $sideMenu = $('#' + name); 192 193 // If the side menu do not exist create it 194 if ($sideMenu.length === 0) { 195 $sideMenu = $('<div />') 196 .attr('id', name) 197 .appendTo($('body')); 198 } 199 200 // Adding styles and options 201 $sideMenu 202 .addClass('sidr') 203 .addClass(settings.side) 204 .data({ 205 speed: settings.speed, 206 side: settings.side, 207 body: settings.body 208 }); 209 210 // The menu content 211 if (typeof settings.source === 'function') { 212 var newContent = settings.source(name); 213 privateMethods.loadContent($sideMenu, newContent); 214 } 215 else if (typeof settings.source === 'string' && privateMethods.isUrl(settings.source)) { 216 $.get(settings.source, function (data) { 217 privateMethods.loadContent($sideMenu, data); 218 }); 219 } 220 else if (typeof settings.source === 'string') { 221 var htmlContent = '', 222 selectors = settings.source.split(','); 223 224 $.each(selectors, function (index, element) { 225 htmlContent += '<div class="sidr-inner">' + $(element).html() + '</div>'; 226 }); 227 228 // Renaming ids and classes 229 if (settings.renaming) { 230 var $htmlContent = $('<div />').html(htmlContent); 231 $htmlContent.find('*').each(function (index, element) { 232 var $element = $(element); 233 privateMethods.addPrefix($element); 234 }); 235 htmlContent = $htmlContent.html(); 236 } 237 privateMethods.loadContent($sideMenu, htmlContent); 238 } 239 else if (settings.source !== null) { 240 $.error('Invalid Sidr Source'); 241 } 242 243 return this.each(function () { 244 245 var $this = $(this), 246 data = $this.data('sidr'); 247 248 // If the plugin hasn't been initialized yet 249 if (!data) { 250 $this.data('sidr', name); 251 $this.click(function (e) { 252 e.preventDefault(); 253 methods.toogle(name); 254 }); 255 } 256 }); 257 }; 258 259})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.