From 66a727ab5b5c3be73827d01e74ade648a1ab3844 Mon Sep 17 00:00:00 2001 From: Alberto Varela Date: Sat, 23 Jan 2016 17:33:16 +0100 Subject: [PATCH] Changing browserify with rollup.js --- .babelrc | 4 + Gruntfile.js | 57 ++- dist/jquery.sidr.js | 1081 ++++++++++++++++++--------------------- dist/jquery.sidr.min.js | 4 +- karma.conf.js | 38 +- package.json | 12 +- src/jquery.sidr.js | 6 +- 7 files changed, 567 insertions(+), 635 deletions(-) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..288b057 --- /dev/null +++ b/.babelrc @@ -0,0 +1,4 @@ +{ + "sourceMap": true, + "presets": ['babel-preset-es2015-rollup'] +} diff --git a/Gruntfile.js b/Gruntfile.js index f3e7cc7..e872933 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,4 +1,6 @@ /* eslint global-require:0 */ +var babel = require('rollup-plugin-babel'); + module.exports = function(grunt) { require('load-grunt-tasks')(grunt); @@ -7,6 +9,12 @@ module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), + banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' + + '<%= grunt.template.today("yyyy-mm-dd") %>\n' + + '<%= pkg.homepage ? " * " + pkg.homepage + "\\n" : "" %>' + + ' * Copyright (c) 2013-<%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' + + ' Licensed <%= pkg.license %> */\n', + clean: { dist: ['dist'], cssmin: [ @@ -32,11 +40,7 @@ module.exports = function(grunt) { uglify: { options: { - banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' + - '<%= grunt.template.today("yyyy-mm-dd") %>\n' + - '<%= pkg.homepage ? " * " + pkg.homepage + "\\n" : "" %>' + - ' * Copyright (c) 2013-<%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' + - ' Licensed <%= pkg.license %> */\n' + banner: '<%= banner %>' }, dist: { files: { @@ -68,7 +72,7 @@ module.exports = function(grunt) { watch: { js: { files: ['src/jquery.sidr.js', 'src/js/*.js'], - tasks: 'browserify' + tasks: 'rollup' }, compass: { files: ['src/scss/**/*.scss'], @@ -107,24 +111,6 @@ module.exports = function(grunt) { } }, - browserify: { - dist: { - options: { - transform: [ - ['babelify', { - sourceMap: true, - presets: ['babel-preset-es2015'] - }] - ] - }, - files: { - 'dist/jquery.<%= pkg.name %>.js': [ - 'src/jquery.<%= pkg.name %>.js' - ] - } - } - }, - karma: { unit: { configFile: 'karma.conf.js', @@ -153,6 +139,25 @@ module.exports = function(grunt) { open: 'http://localhost:9000/examples/index.html' } } + }, + + rollup: { + options: { + banner: '<%= banner %>', + format: 'iife', + globals: { + jquery: 'jQuery' + }, + plugins: [ + babel({ + exclude: './node_modules/**' + }) + ] + }, + files: { + dest: 'dist/jquery.<%= pkg.name %>.js', + src: 'src/jquery.<%= pkg.name %>.js' + } } }); @@ -172,7 +177,7 @@ module.exports = function(grunt) { ]); grunt.registerTask('build', [ - 'browserify', + 'rollup', 'compass:distmin', 'copy:cssmin', 'clean:cssmin', @@ -183,7 +188,7 @@ module.exports = function(grunt) { grunt.registerTask('serve', [ 'clean:dist', 'compass:dev', - 'browserify', + 'rollup', 'connect:dist', 'watch' ]); diff --git a/dist/jquery.sidr.js b/dist/jquery.sidr.js index f9d2e48..a14dc34 100644 --- a/dist/jquery.sidr.js +++ b/dist/jquery.sidr.js @@ -1,657 +1,574 @@ -(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o'; - }); + var $$3 = jQuery; + + var bodyAnimationClass = 'sidr-animating'; + var openAction = 'open'; + var closeAction = 'close'; + var transitionEndEvent = 'webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend'; + var Menu = function () { + function Menu(name) { + babelHelpers.classCallCheck(this, Menu); + + this.name = name; + this.item = $$3('#' + name); + this.openClass = name === 'sidr' ? 'sidr-open' : 'sidr-open ' + name + '-open'; + this.menuWidth = this.item.outerWidth(true); + this.speed = this.item.data('speed'); + this.side = this.item.data('side'); + this.displace = this.item.data('displace'); + this.timing = this.item.data('timing'); + this.method = this.item.data('method'); + this.onOpenCallback = this.item.data('onOpen'); + this.onCloseCallback = this.item.data('onClose'); + this.onOpenEndCallback = this.item.data('onOpenEnd'); + this.onCloseEndCallback = this.item.data('onCloseEnd'); + this.body = $$3(this.item.data('body')); + } - // Renaming ids and classes - if (settings.renaming) { - var $htmlContent = $('
').html(htmlContent); + babelHelpers.createClass(Menu, [{ + key: 'getAnimation', + value: function getAnimation(action, element) { + var animation = {}, + prop = this.side; + + if (action === 'open' && element === 'body') { + animation[prop] = this.menuWidth + 'px'; + } else if (action === 'close' && element === 'menu') { + animation[prop] = '-' + this.menuWidth + 'px'; + } else { + animation[prop] = 0; + } - $htmlContent.find('*').each(function (index, element) { - var $element = $(element); + return animation; + } + }, { + key: 'prepareBody', + value: function prepareBody(action) { + var prop = action === 'open' ? 'hidden' : ''; - _helper2.default.addPrefixes($element); - }); - htmlContent = $htmlContent.html(); - } + // Prepare page if container is body + if (this.body.is('body')) { + var $html = $$3('html'), + scrollTop = $html.scrollTop(); - $sideMenu.html(htmlContent); - } else if (settings.source !== null) { - $.error('Invalid Sidr Source'); - } - - return $sideMenu; -} - -function fnSidr(options) { - var transitions = _helper2.default.transitions, - settings = $.extend({ - name: 'sidr', // Name for the 'sidr' - speed: 200, // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds) - side: 'left', // Accepts 'left' or 'right' - source: null, // Override the source of the content. - renaming: true, // The ids and classes will be prepended with a prefix when loading existent content - body: 'body', // Page container selector, - displace: true, // Displace the body content or not - timing: 'ease', // Timing function for CSS transitions - method: 'toggle', // The method to call when element is clicked - onOpen: function onOpen() {}, - // Callback when sidr start opening - onClose: function onClose() {}, - // Callback when sidr start closing - onOpenEnd: function onOpenEnd() {}, - // Callback when sidr end opening - onCloseEnd: function onCloseEnd() {} // Callback when sidr end closing - - }, options), - name = settings.name, - $sideMenu = $('#' + name); - - // If the side menu do not exist create it - if ($sideMenu.length === 0) { - $sideMenu = $('
').attr('id', name).appendTo($('body')); - } - - // Add transition to menu if are supported - if (transitions.supported) { - $sideMenu.css(transitions.property, settings.side + ' ' + settings.speed / 1000 + 's ' + settings.timing); - } - - // Adding styles and options - $sideMenu.addClass('sidr').addClass(settings.side).data({ - speed: settings.speed, - side: settings.side, - body: settings.body, - displace: settings.displace, - timing: settings.timing, - method: settings.method, - onOpen: settings.onOpen, - onClose: settings.onClose, - onOpenEnd: settings.onOpenEnd, - onCloseEnd: settings.onCloseEnd - }); - - $sideMenu = fillContent($sideMenu, settings); - - return this.each(function () { - var $this = $(this), - data = $this.data('sidr'), - flag = false; - - // If the plugin hasn't been initialized yet - if (!data) { - _status2.default.moving = false; - _status2.default.opened = false; - - $this.data('sidr', name); - - $this.bind('touchstart click', function (event) { - event.preventDefault(); - - if (!flag) { - flag = true; - (0, _sidr2.default)(settings.method, name); - - setTimeout(function () { - flag = false; - }, 100); + $html.css('overflow-x', prop).scrollTop(scrollTop); + } } - }); - } - }); -} - -exports.default = fnSidr; - -},{"./helper":4,"./sidr":6,"./status":7}],4:[function(require,module,exports){ -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -var helper = { - // Check for valids urls - // From : http://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-an-url - - isUrl: function isUrl(str) { - var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol - '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|' + // domain name - '((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address - '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path - '(\\?[;&a-z\\d%_.~+=-]*)?' + // query string - '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator - - if (pattern.test(str)) { - return true; - } else { - return false; - } - }, - - // Add sidr prefixes - addPrefixes: function addPrefixes($element) { - this.addPrefix($element, 'id'); - this.addPrefix($element, 'class'); - $element.removeAttr('style'); - }, - addPrefix: function addPrefix($element, attribute) { - var toReplace = $element.attr(attribute); - - if (typeof toReplace === 'string' && toReplace !== '' && toReplace !== 'sidr-inner') { - $element.attr(attribute, toReplace.replace(/([A-Za-z0-9_.\-]+)/g, 'sidr-' + attribute + '-$1')); - } - }, - - // Check if transitions is supported - transitions: function () { - var body = document.body || document.documentElement, - style = body.style, - supported = false, - property = 'transition'; - - if (property in style) { - supported = true; - } else { - (function () { - var prefixes = ['moz', 'webkit', 'o', 'ms'], - prefix = undefined, - i = undefined; - - property = property.charAt(0).toUpperCase() + property.substr(1); - supported = function () { - for (i = 0; i < prefixes.length; i++) { - prefix = prefixes[i]; - if (prefix + property in style) { - return true; + }, { + key: 'openBody', + value: function openBody() { + if (this.displace) { + var transitions = helper.transitions, + $body = this.body; + + if (transitions.supported) { + $body.css(transitions.property, this.side + ' ' + this.speed / 1000 + 's ' + this.timing).css(this.side, 0).css({ + width: $body.width(), + position: 'absolute' + }); + $body.css(this.side, this.menuWidth + 'px'); + } else { + var bodyAnimation = this.getAnimation(openAction, 'body'); + + $body.css({ + width: $body.width(), + position: 'absolute' + }).animate(bodyAnimation, { + queue: false, + duration: this.speed + }); } } + } + }, { + key: 'onCloseBody', + value: function onCloseBody() { + var transitions = helper.transitions, + resetStyles = { + width: '', + position: '', + right: '', + left: '' + }; + + if (transitions.supported) { + resetStyles[transitions.property] = ''; + } - return false; - }(); - property = supported ? '-' + prefix.toLowerCase() + '-' + property.toLowerCase() : null; - })(); - } - - return { - supported: supported, - property: property - }; - }() -}; - -exports.default = helper; - -},{}],5:[function(require,module,exports){ -'use strict'; - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*eslint callback-return: 0*/ - -Object.defineProperty(exports, "__esModule", { - value: true -}); - -var _status = require('./status'); + this.body.css(resetStyles).unbind(transitionEndEvent); + } + }, { + key: 'closeBody', + value: function closeBody() { + var _this = this; + + if (this.displace) { + if (helper.transitions.supported) { + this.body.css(this.side, 0).one(transitionEndEvent, function () { + _this.onCloseBody(); + }); + } else { + var bodyAnimation = this.getAnimation(closeAction, 'body'); + + this.body.animate(bodyAnimation, { + queue: false, + duration: this.speed, + complete: function complete() { + _this.onCloseBody(); + } + }); + } + } + } + }, { + key: 'moveBody', + value: function moveBody(action) { + if (action === openAction) { + this.openBody(); + } else { + this.closeBody(); + } + } + }, { + key: 'onOpenMenu', + value: function onOpenMenu(callback) { + var name = this.name; -var _status2 = _interopRequireDefault(_status); + sidrStatus.moving = false; + sidrStatus.opened = name; -var _helper = require('./helper'); + this.item.unbind(transitionEndEvent); -var _helper2 = _interopRequireDefault(_helper); + this.body.removeClass(bodyAnimationClass).addClass(this.openClass); -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + this.onOpenEndCallback(); -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + if (typeof callback === 'function') { + callback(name); + } + } + }, { + key: 'openMenu', + value: function openMenu(callback) { + var _this2 = this; + + var $item = this.item; + + if (helper.transitions.supported) { + $item.css(this.side, 0).one(transitionEndEvent, function () { + _this2.onOpenMenu(callback); + }); + } else { + var menuAnimation = this.getAnimation(openAction, 'menu'); + + $item.css('display', 'block').animate(menuAnimation, { + queue: false, + duration: this.speed, + complete: function complete() { + _this2.onOpenMenu(callback); + } + }); + } + } + }, { + key: 'onCloseMenu', + value: function onCloseMenu(callback) { + this.item.css({ + left: '', + right: '' + }).unbind(transitionEndEvent); + $$3('html').css('overflow-x', ''); -var $ = jQuery; + sidrStatus.moving = false; + sidrStatus.opened = false; -var bodyAnimationClass = 'sidr-animating', - openAction = 'open', - closeAction = 'close', - transitionEndEvent = 'webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend'; + this.body.removeClass(bodyAnimationClass).removeClass(this.openClass); -var Menu = function () { - function Menu(name) { - _classCallCheck(this, Menu); + this.onCloseEndCallback(); - this.name = name; - this.item = $('#' + name); - this.openClass = name === 'sidr' ? 'sidr-open' : 'sidr-open ' + name + '-open'; - this.menuWidth = this.item.outerWidth(true); - this.speed = this.item.data('speed'); - this.side = this.item.data('side'); - this.displace = this.item.data('displace'); - this.timing = this.item.data('timing'); - this.method = this.item.data('method'); - this.onOpenCallback = this.item.data('onOpen'); - this.onCloseCallback = this.item.data('onClose'); - this.onOpenEndCallback = this.item.data('onOpenEnd'); - this.onCloseEndCallback = this.item.data('onCloseEnd'); - this.body = $(this.item.data('body')); - } + // Callback + if (typeof callback === 'function') { + callback(name); + } + } + }, { + key: 'closeMenu', + value: function closeMenu(callback) { + var _this3 = this; + + var item = this.item; + + if (helper.transitions.supported) { + item.css(this.side, '').one(transitionEndEvent, function () { + _this3.onCloseMenu(callback); + }); + } else { + var menuAnimation = this.getAnimation(closeAction, 'menu'); + + item.animate(menuAnimation, { + queue: false, + duration: this.speed, + complete: function complete() { + _this3.onCloseMenu(); + } + }); + } + } + }, { + key: 'moveMenu', + value: function moveMenu(action, callback) { + this.body.addClass(bodyAnimationClass); + + if (action === openAction) { + this.openMenu(callback); + } else { + this.closeMenu(callback); + } + } + }, { + key: 'move', + value: function move(action, callback) { + // Lock sidr + sidrStatus.moving = true; + + this.prepareBody(action); + this.moveBody(action); + this.moveMenu(action, callback); + } + }, { + key: 'open', + value: function open(callback) { + var _this4 = this; + + // Check if is already opened or moving + if (sidrStatus.opened === this.name || sidrStatus.moving) { + return; + } - _createClass(Menu, [{ - key: 'getAnimation', - value: function getAnimation(action, element) { - var animation = {}, - prop = this.side; + // If another menu opened close first + if (sidrStatus.opened !== false) { + var alreadyOpenedMenu = new Menu(sidrStatus.opened); - if (action === 'open' && element === 'body') { - animation[prop] = this.menuWidth + 'px'; - } else if (action === 'close' && element === 'menu') { - animation[prop] = '-' + this.menuWidth + 'px'; - } else { - animation[prop] = 0; - } + alreadyOpenedMenu.close(function () { + _this4.open(callback); + }); - return animation; - } - }, { - key: 'prepareBody', - value: function prepareBody(action) { - var prop = action === 'open' ? 'hidden' : ''; + return; + } - // Prepare page if container is body - if (this.body.is('body')) { - var $html = $('html'), - scrollTop = $html.scrollTop(); + this.move('open', callback); - $html.css('overflow-x', prop).scrollTop(scrollTop); - } - } - }, { - key: 'openBody', - value: function openBody() { - if (this.displace) { - var transitions = _helper2.default.transitions, - $body = this.body; - - if (transitions.supported) { - $body.css(transitions.property, this.side + ' ' + this.speed / 1000 + 's ' + this.timing).css(this.side, 0).css({ - width: $body.width(), - position: 'absolute' - }); - $body.css(this.side, this.menuWidth + 'px'); - } else { - var bodyAnimation = this.getAnimation(openAction, 'body'); - - $body.css({ - width: $body.width(), - position: 'absolute' - }).animate(bodyAnimation, { - queue: false, - duration: this.speed - }); + // onOpen callback + this.onOpenCallback(); } - } - } - }, { - key: 'onCloseBody', - value: function onCloseBody() { - var transitions = _helper2.default.transitions, - resetStyles = { - width: '', - position: '', - right: '', - left: '' - }; + }, { + key: 'close', + value: function close(callback) { + // Check if is already closed or moving + if (sidrStatus.opened !== this.name || sidrStatus.moving) { + return; + } - if (transitions.supported) { - resetStyles[transitions.property] = ''; - } + this.move('close', callback); - this.body.css(resetStyles).unbind(transitionEndEvent); - } - }, { - key: 'closeBody', - value: function closeBody() { - var _this = this; - - if (this.displace) { - if (_helper2.default.transitions.supported) { - this.body.css(this.side, 0).one(transitionEndEvent, function () { - _this.onCloseBody(); - }); - } else { - var bodyAnimation = this.getAnimation(closeAction, 'body'); - - this.body.animate(bodyAnimation, { - queue: false, - duration: this.speed, - complete: function complete() { - _this.onCloseBody(); - } - }); + // onClose callback + this.onCloseCallback(); } + }, { + key: 'toggle', + value: function toggle(callback) { + if (sidrStatus.opened === this.name) { + this.close(callback); + } else { + this.open(callback); + } + } + }]); + return Menu; + }(); + + var $$2 = jQuery; + + function execute(action, name, callback) { + var sidr = new Menu(name); + + switch (action) { + case 'open': + sidr.open(callback); + break; + case 'close': + sidr.close(callback); + break; + case 'toggle': + sidr.toggle(callback); + break; + default: + $$2.error('Method ' + action + ' does not exist on jQuery.sidr'); + break; } } - }, { - key: 'moveBody', - value: function moveBody(action) { - if (action === openAction) { - this.openBody(); - } else { - this.closeBody(); - } - } - }, { - key: 'onOpenMenu', - value: function onOpenMenu(callback) { - var name = this.name; - - _status2.default.moving = false; - _status2.default.opened = name; - this.item.unbind(transitionEndEvent); - - this.body.removeClass(bodyAnimationClass).addClass(this.openClass); - - this.onOpenEndCallback(); + var i; + var $ = jQuery; + var publicMethods = ['open', 'close', 'toggle']; + var methodName; + var methods = {}; + var getMethod = function getMethod(methodName) { + return function (name, callback) { + // Check arguments + if (typeof name === 'function') { + callback = name; + name = 'sidr'; + } else if (!name) { + name = 'sidr'; + } - if (typeof callback === 'function') { - callback(name); - } + execute(methodName, name, callback); + }; + }; + for (i = 0; i < publicMethods.length; i++) { + methodName = publicMethods[i]; + methods[methodName] = getMethod(methodName); } - }, { - key: 'openMenu', - value: function openMenu(callback) { - var _this2 = this; - var $item = this.item; - - if (_helper2.default.transitions.supported) { - $item.css(this.side, 0).one(transitionEndEvent, function () { - _this2.onOpenMenu(callback); - }); + function sidr(method) { + if (methods[method]) { + return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); + } else if (typeof method === 'function' || typeof method === 'string' || !method) { + return methods.toggle.apply(this, arguments); } else { - var menuAnimation = this.getAnimation(openAction, 'menu'); - - $item.css('display', 'block').animate(menuAnimation, { - queue: false, - duration: this.speed, - complete: function complete() { - _this2.onOpenMenu(callback); - } - }); + $.error('Method ' + method + ' does not exist on jQuery.sidr'); } } - }, { - key: 'onCloseMenu', - value: function onCloseMenu(callback) { - this.item.css({ - left: '', - right: '' - }).unbind(transitionEndEvent); - $('html').css('overflow-x', ''); - _status2.default.moving = false; - _status2.default.opened = false; + var $$1 = jQuery; - this.body.removeClass(bodyAnimationClass).removeClass(this.openClass); + function fillContent($sideMenu, settings) { + // The menu content + if (typeof settings.source === 'function') { + var newContent = settings.source(name); - this.onCloseEndCallback(); - - // Callback - if (typeof callback === 'function') { - callback(name); - } - } - }, { - key: 'closeMenu', - value: function closeMenu(callback) { - var _this3 = this; - - var item = this.item; - - if (_helper2.default.transitions.supported) { - item.css(this.side, '').one(transitionEndEvent, function () { - _this3.onCloseMenu(callback); + $sideMenu.html(newContent); + } else if (typeof settings.source === 'string' && helper.isUrl(settings.source)) { + $$1.get(settings.source, function (data) { + $sideMenu.html(data); }); - } else { - var menuAnimation = this.getAnimation(closeAction, 'menu'); + } else if (typeof settings.source === 'string') { + var htmlContent = '', + selectors = settings.source.split(','); - item.animate(menuAnimation, { - queue: false, - duration: this.speed, - complete: function complete() { - _this3.onCloseMenu(); - } + $$1.each(selectors, function (index, element) { + htmlContent += '
' + $$1(element).html() + '
'; }); - } - } - }, { - key: 'moveMenu', - value: function moveMenu(action, callback) { - this.body.addClass(bodyAnimationClass); - if (action === openAction) { - this.openMenu(callback); - } else { - this.closeMenu(callback); - } - } - }, { - key: 'move', - value: function move(action, callback) { - // Lock sidr - _status2.default.moving = true; - - this.prepareBody(action); - this.moveBody(action); - this.moveMenu(action, callback); - } - }, { - key: 'open', - value: function open(callback) { - var _this4 = this; - - // Check if is already opened or moving - if (_status2.default.opened === this.name || _status2.default.moving) { - return; - } + // Renaming ids and classes + if (settings.renaming) { + var $htmlContent = $$1('
').html(htmlContent); - // If another menu opened close first - if (_status2.default.opened !== false) { - var alreadyOpenedMenu = new Menu(_status2.default.opened); + $htmlContent.find('*').each(function (index, element) { + var $element = $$1(element); - alreadyOpenedMenu.close(function () { - _this4.open(callback); - }); + helper.addPrefixes($element); + }); + htmlContent = $htmlContent.html(); + } - return; + $sideMenu.html(htmlContent); + } else if (settings.source !== null) { + $$1.error('Invalid Sidr Source'); } - this.move('open', callback); - - // onOpen callback - this.onOpenCallback(); + return $sideMenu; } - }, { - key: 'close', - value: function close(callback) { - // Check if is already closed or moving - if (_status2.default.opened !== this.name || _status2.default.moving) { - return; - } - this.move('close', callback); + function fnSidr(options) { + var transitions = helper.transitions, + settings = $$1.extend({ + name: 'sidr', // Name for the 'sidr' + speed: 200, // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds) + side: 'left', // Accepts 'left' or 'right' + source: null, // Override the source of the content. + renaming: true, // The ids and classes will be prepended with a prefix when loading existent content + body: 'body', // Page container selector, + displace: true, // Displace the body content or not + timing: 'ease', // Timing function for CSS transitions + method: 'toggle', // The method to call when element is clicked + onOpen: function onOpen() {}, + // Callback when sidr start opening + onClose: function onClose() {}, + // Callback when sidr start closing + onOpenEnd: function onOpenEnd() {}, + // Callback when sidr end opening + onCloseEnd: function onCloseEnd() {} // Callback when sidr end closing + + }, options), + name = settings.name, + $sideMenu = $$1('#' + name); + + // If the side menu do not exist create it + if ($sideMenu.length === 0) { + $sideMenu = $$1('
').attr('id', name).appendTo($$1('body')); + } - // onClose callback - this.onCloseCallback(); - } - }, { - key: 'toggle', - value: function toggle(callback) { - if (_status2.default.opened === this.name) { - this.close(callback); - } else { - this.open(callback); + // Add transition to menu if are supported + if (transitions.supported) { + $sideMenu.css(transitions.property, settings.side + ' ' + settings.speed / 1000 + 's ' + settings.timing); } - } - }]); - return Menu; -}(); + // Adding styles and options + $sideMenu.addClass('sidr').addClass(settings.side).data({ + speed: settings.speed, + side: settings.side, + body: settings.body, + displace: settings.displace, + timing: settings.timing, + method: settings.method, + onOpen: settings.onOpen, + onClose: settings.onClose, + onOpenEnd: settings.onOpenEnd, + onCloseEnd: settings.onCloseEnd + }); -exports.default = Menu; + $sideMenu = fillContent($sideMenu, settings); -},{"./helper":4,"./status":7}],6:[function(require,module,exports){ -'use strict'; + return this.each(function () { + var $this = $$1(this), + data = $this.data('sidr'), + flag = false; -Object.defineProperty(exports, "__esModule", { - value: true -}); + // If the plugin hasn't been initialized yet + if (!data) { + sidrStatus.moving = false; + sidrStatus.opened = false; -var _execute = require('./execute'); + $this.data('sidr', name); -var _execute2 = _interopRequireDefault(_execute); + $this.bind('touchstart click', function (event) { + event.preventDefault(); -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + if (!flag) { + flag = true; + sidr(settings.method, name); -var i, - $ = jQuery, - publicMethods = ['open', 'close', 'toggle'], - methodName, - methods = {}, - getMethod = function getMethod(methodName) { - return function (name, callback) { - // Check arguments - if (typeof name === 'function') { - callback = name; - name = 'sidr'; - } else if (!name) { - name = 'sidr'; + setTimeout(function () { + flag = false; + }, 100); + } + }); + } + }); } - (0, _execute2.default)(methodName, name, callback); - }; -}; - -for (i = 0; i < publicMethods.length; i++) { - methodName = publicMethods[i]; - methods[methodName] = getMethod(methodName); -} - -function sidr(method) { - if (methods[method]) { - return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); - } else if (typeof method === 'function' || typeof method === 'string' || !method) { - return methods.toggle.apply(this, arguments); - } else { - $.error('Method ' + method + ' does not exist on jQuery.sidr'); - } -} - -exports.default = sidr; - -},{"./execute":2}],7:[function(require,module,exports){ -"use strict"; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -var sidrStatus = { - moving: false, - opened: false -}; - -exports.default = sidrStatus; - -},{}]},{},[1]); + jQuery.sidr = sidr; + jQuery.fn.sidr = fnSidr; + +}()); \ No newline at end of file diff --git a/dist/jquery.sidr.min.js b/dist/jquery.sidr.min.js index c1246c1..0120c47 100644 --- a/dist/jquery.sidr.min.js +++ b/dist/jquery.sidr.min.js @@ -1,4 +1,4 @@ -/*! sidr - v2.0.0 - 2016-01-20 +/*! sidr - v2.1.0 - 2016-01-23 * http://www.berriart.com/sidr/ * Copyright (c) 2013-2016 Alberto Varela; Licensed MIT */ -!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g'+m(b).html()+"
"}),b.renaming){var f=m("
").html(d);f.find("*").each(function(a,b){var c=m(b);h["default"].addPrefixes(c)}),d=f.html()}a.html(d)}else null!==b.source&&m.error("Invalid Sidr Source");return a}function f(a){var b=h["default"].transitions,c=m.extend({name:"sidr",speed:200,side:"left",source:null,renaming:!0,body:"body",displace:!0,timing:"ease",method:"toggle",onOpen:function(){},onClose:function(){},onOpenEnd:function(){},onCloseEnd:function(){}},a),d=c.name,f=m("#"+d);return 0===f.length&&(f=m("
").attr("id",d).appendTo(m("body"))),b.supported&&f.css(b.property,c.side+" "+c.speed/1e3+"s "+c.timing),f.addClass("sidr").addClass(c.side).data({speed:c.speed,side:c.side,body:c.body,displace:c.displace,timing:c.timing,method:c.method,onOpen:c.onOpen,onClose:c.onClose,onOpenEnd:c.onOpenEnd,onCloseEnd:c.onCloseEnd}),f=e(f,c),this.each(function(){var a=m(this),b=a.data("sidr"),e=!1;b||(j["default"].moving=!1,j["default"].opened=!1,a.data("sidr",d),a.bind("touchstart click",function(a){a.preventDefault(),e||(e=!0,(0,l["default"])(c.method,d),setTimeout(function(){e=!1},100))}))})}Object.defineProperty(c,"__esModule",{value:!0});var g=a("./helper"),h=d(g),i=a("./status"),j=d(i),k=a("./sidr"),l=d(k),m=jQuery;c["default"]=f},{"./helper":4,"./sidr":6,"./status":7}],4:[function(a,b,c){"use strict";Object.defineProperty(c,"__esModule",{value:!0});var d={isUrl:function(a){var b=new RegExp("^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$","i");return b.test(a)?!0:!1},addPrefixes:function(a){this.addPrefix(a,"id"),this.addPrefix(a,"class"),a.removeAttr("style")},addPrefix:function(a,b){var c=a.attr(b);"string"==typeof c&&""!==c&&"sidr-inner"!==c&&a.attr(b,c.replace(/([A-Za-z0-9_.\-]+)/g,"sidr-"+b+"-$1"))},transitions:function(){var a=document.body||document.documentElement,b=a.style,c=!1,d="transition";return d in b?c=!0:!function(){var a=["moz","webkit","o","ms"],e=void 0,f=void 0;d=d.charAt(0).toUpperCase()+d.substr(1),c=function(){for(f=0;f'+u(b).html()+"
"}),b.renaming){var f=u("
").html(d);f.find("*").each(function(a,b){var c=u(b);i.addPrefixes(c)}),d=f.html()}a.html(d)}else null!==b.source&&u.error("Invalid Sidr Source");return a}function d(a){var d=i.transitions,e=u.extend({name:"sidr",speed:200,side:"left",source:null,renaming:!0,body:"body",displace:!0,timing:"ease",method:"toggle",onOpen:function(){},onClose:function(){},onOpenEnd:function(){},onCloseEnd:function(){}},a),f=e.name,g=u("#"+f);return 0===g.length&&(g=u("
").attr("id",f).appendTo(u("body"))),d.supported&&g.css(d.property,e.side+" "+e.speed/1e3+"s "+e.timing),g.addClass("sidr").addClass(e.side).data({speed:e.speed,side:e.side,body:e.body,displace:e.displace,timing:e.timing,method:e.method,onOpen:e.onOpen,onClose:e.onClose,onOpenEnd:e.onOpenEnd,onCloseEnd:e.onCloseEnd}),g=c(g,e),this.each(function(){var a=u(this),c=a.data("sidr"),d=!1;c||(h.moving=!1,h.opened=!1,a.data("sidr",f),a.bind("touchstart click",function(a){a.preventDefault(),d||(d=!0,b(e.method,f),setTimeout(function(){d=!1},100))}))})}var e={};e.classCallCheck=function(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")},e.createClass=function(){function a(a,b){for(var c=0;c=1.4.3" }, "devDependencies": { - "babel-preset-es2015": "^6.3.13", + "babel-preset-es2015-rollup": "^1.1.1", "babelify": "^7.2.0", - "browserify-istanbul": "^0.2.1", "chai": "^3.4.1", "grunt": "^0.4.5", - "grunt-browserify": "^4.0.1", "grunt-codeclimate-reporter": "^1.2.0", "grunt-contrib-clean": "^0.7.0", "grunt-contrib-compass": "^1.0.4", @@ -36,19 +34,23 @@ "grunt-contrib-watch": "^0.6.1", "grunt-eslint": "^17.3.1", "grunt-karma": "^0.12.1", + "grunt-rollup": "^0.6.1", "grunt-scss-lint": "^0.3.8", "karma": "^0.13.19", - "karma-browserify": "^4.4.2", "karma-chai": "^0.1.0", "karma-coverage": "^0.5.3", "karma-jquery": "^0.1.0", "karma-mocha": "^0.2.1", "karma-mocha-reporter": "^1.1.5", "karma-phantomjs-launcher": "^0.2.3", + "karma-rollup-preprocessor": "^2.0.1", "karma-sinon-chai": "^1.1.0", "load-grunt-tasks": "^3.4.0", "mocha": "^2.3.4", - "phantomjs": "^1.9.19" + "phantomjs": "^1.9.19", + "rollup": "^0.25.1", + "rollup-plugin-babel": "^2.3.9", + "rollup-plugin-istanbul": "file:///home/alberto/Projects/rollup-plugin-istanbul" }, "keywords": [ "menu", diff --git a/src/jquery.sidr.js b/src/jquery.sidr.js index 5324447..c65ae0a 100644 --- a/src/jquery.sidr.js +++ b/src/jquery.sidr.js @@ -9,7 +9,5 @@ import sidr from './js/sidr'; import fnSidr from './js/fnSidr'; -var $ = jQuery; - -$.sidr = sidr; -$.fn.sidr = fnSidr; +jQuery.sidr = sidr; +jQuery.fn.sidr = fnSidr;