├── .editorconfig ├── .gitignore ├── .jshintrc ├── README.md ├── bower.json ├── gulpfile.js ├── logo.png ├── mantis.js ├── mantis.min.js ├── package.json └── src ├── attributes.js ├── classes.js ├── core.js ├── css.js ├── manipulation.js ├── mantis.js └── utils.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = tab 7 | indent_size = 4 8 | end_of_line = lf 9 | charset = utf-8 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ############# 2 | ## Mantis.js 3 | ############# 4 | 5 | .DS_Store 6 | *.sublime-project 7 | *.sublime-workspace 8 | /node_modules/ 9 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "camelcase": true, 3 | "curly": true, 4 | "eqeqeq": true, 5 | "freeze": true, 6 | "immed": true, 7 | "newcap": true, 8 | "noempty": true, 9 | "nonbsp": true, 10 | "quotmark": "single", 11 | "undef": true, 12 | "strict": true, 13 | "debug": true, 14 | "eqnull": true, 15 | "browser": true, 16 | "devel": true, 17 | "node": true, 18 | "predef": ["define"] 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Mantis.js 2 | ========= 3 | 4 | [![Join the chat at https://gitter.im/acauamontiel/mantis-js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/acauamontiel/mantis-js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 5 | 6 | [![Bower version](https://badge.fury.io/bo/mantis-js.svg)](http://badge.fury.io/bo/mantis-js) 7 | [![npm version](https://badge.fury.io/js/mantis-js.svg)](http://badge.fury.io/js/mantis-js) 8 | 9 |

10 | 11 |

12 | 13 | [Mantis.js][website-mantisjs] is probably the simplest and lightest weight JavaScript library for modern browsers. It has the most essential and useful features like DOM traversal and manipulation, event handling, Ajax, and more with a similar API to jQuery and Zepto.js. 14 | 15 | 16 | Usage 17 | ----- 18 | 19 | Include Mantis.js with a script tag near the end of your page: 20 | 21 | ```html 22 | 23 | ``` 24 | 25 | A CDN will be available soon. 26 | 27 | 28 | API Methods 29 | ----------- 30 | 31 | List of all the methods currently working 32 | 33 | * [x] Mantis() 34 | * [x] Mantis.extend() 35 | * [x] Mantis.map() 36 | * [x] Mantis.noConflict() 37 | * [x] Mantis.type() 38 | * [x] Mantis.fn 39 | * [x] Mantis.fn.addClass() 40 | * [x] Mantis.fn.after() 41 | * [x] Mantis.fn.append() 42 | * [x] Mantis.fn.attr() 43 | * [x] Mantis.fn.before() 44 | * [x] Mantis.fn.css() 45 | * [x] Mantis.fn.each() 46 | * [x] Mantis.fn.extend() 47 | * [x] Mantis.fn.hasAttr() 48 | * [x] Mantis.fn.hasClass() 49 | * [x] Mantis.fn.html() 50 | * [x] Mantis.fn.map() 51 | * [x] Mantis.fn.prepend() 52 | * [x] Mantis.fn.removeAttr() 53 | * [x] Mantis.fn.removeClass() 54 | * [x] Mantis.fn.toggleClass() 55 | 56 | 57 | Browser Support 58 | --------------- 59 | 60 | ![Chrome](https://raw.github.com/alrra/browser-logos/master/chrome/chrome_48x48.png) | ![Firefox](https://raw.github.com/alrra/browser-logos/master/firefox/firefox_48x48.png) | ![IE](https://raw.github.com/alrra/browser-logos/master/internet-explorer/internet-explorer_48x48.png) | ![Safari](https://raw.github.com/alrra/browser-logos/master/safari/safari_48x48.png) 61 | --- | --- | --- | --- | 62 | Latest ✔ | Latest ✔ | 9+ ✔ | 6.1+ ✔ | 63 | 64 | 65 | Donating 66 | -------- 67 | 68 | Support this project and [others by acauamontiel][github-acauamontiel] via [gratipay][gratipay-acauamontiel]. 69 | 70 | [![Support via Gratipay][gratipay]][gratipay-acauamontiel] 71 | 72 | 73 | License 74 | ------- 75 | 76 | © 2013 - 2016 Acauã Montiel 77 | 78 | [MIT License][mit-acauamontiel] 79 | 80 | 81 | [website-mantisjs]: http://mantisjs.com/ 82 | [website-acauamontiel]: http://acauamontiel.com.br/ 83 | [github-acauamontiel]: https://github.com/acauamontiel/ 84 | [mit-acauamontiel]: http://acaua.mit-license.org/ 85 | [gratipay-acauamontiel]: https://gratipay.com/acauamontiel/ 86 | [gratipay]: https://cdn.rawgit.com/gratipay/gratipay-badge/2.3.0/dist/gratipay.svg 87 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mantis-js", 3 | "description": "JavaScript library for DOM manipulation", 4 | "main": "mantis.js", 5 | "authors": [ 6 | "Acauã Montiel " 7 | ], 8 | "license": "MIT", 9 | "keywords": [ 10 | "js", 11 | "dom" 12 | ], 13 | "homepage": "http://mantisjs.com", 14 | "repository": { 15 | "type": "git", 16 | "url": "git@github.com:mantisjs/mantis-js.git" 17 | }, 18 | "moduleType": [ 19 | "amd", 20 | "es6", 21 | "globals", 22 | "node" 23 | ], 24 | "ignore": [ 25 | "**/.*", 26 | "node_modules", 27 | "bower_components", 28 | "test", 29 | "tests", 30 | "playground", 31 | "src", 32 | "*.json", 33 | "*.md", 34 | "*.png", 35 | "gulpfile.js" 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'), 2 | gutil = require('gulp-util'), 3 | jshint = require('gulp-jshint'), 4 | stylish = require('jshint-stylish'), 5 | include = require('gulp-include'), 6 | prettify = require('gulp-js-prettify'), 7 | uglify = require('gulp-uglify'), 8 | rename = require('gulp-rename'), 9 | connect = require('gulp-connect'), 10 | colors = require('colors'), 11 | path = { 12 | src : 'src/mantis.js', 13 | build : 'mantis.js', 14 | min : 'mantis.min.js' 15 | }; 16 | 17 | function logger (event) { 18 | var file = event.path.split(__dirname)[1], 19 | message, 20 | color; 21 | 22 | switch (event.type) { 23 | case 'added': 24 | color = 'green'; 25 | break; 26 | case 'changed': 27 | color = 'yellow'; 28 | break; 29 | case 'deleted': 30 | color = 'red'; 31 | break; 32 | } 33 | 34 | message = 'The file ' + file.bold.blue + ' was ' + event.type.bold[color]; 35 | 36 | gutil.log('[Mantis.js] '.green + message); 37 | } 38 | 39 | gulp.task('concat', function() { 40 | return gulp.src(path.src) 41 | .pipe(include()) 42 | .pipe(prettify({ 43 | indent_with_tabs : true, 44 | preserve_newlines : false, 45 | end_with_newline : true 46 | })) 47 | .pipe(gulp.dest('./')); 48 | }); 49 | 50 | gulp.task('uglify', function() { 51 | return gulp.src(path.build) 52 | .pipe(uglify()) 53 | .pipe(rename(path.min)) 54 | .pipe(gulp.dest('./')); 55 | }); 56 | 57 | gulp.task('lint', function () { 58 | return gulp.src(path.build) 59 | .pipe(jshint()) 60 | .pipe(jshint.reporter(stylish)); 61 | }); 62 | 63 | gulp.task('connect', function () { 64 | connect.server({ 65 | root: __dirname, 66 | livereload: true 67 | }); 68 | }); 69 | 70 | gulp.task('watch', function () { 71 | gulp.watch(['src/*.js', 'playground/*.html'], ['concat']).on('change', function (event) { 72 | logger(event); 73 | }); 74 | 75 | gulp.watch([path.build], ['uglify', 'lint']).on('change', function (event) { 76 | gulp.src(__dirname) 77 | .pipe(connect.reload()); 78 | }); 79 | }); 80 | 81 | gulp.task('build' , ['concat' , 'uglify']); 82 | gulp.task('default' , ['watch' , 'connect']); 83 | -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/acauamontiel/mantis-js/ba36d105e458c1c8425cc699ecb0e3c0d4f190b8/logo.png -------------------------------------------------------------------------------- /mantis.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Mantis.js 3 | * http://mantisjs.com 4 | * 5 | * Copyright 2013 - 2015 Acaua Montiel (@acauamontiel) 6 | * Released under the MIT license (http://acaua.mit-license.org) 7 | */ 8 | (function(root, factory) { 9 | 'use strict'; 10 | if (typeof define === 'function' && define.amd) { 11 | define([], factory); 12 | } else if (typeof exports === 'object') { 13 | module.exports = factory(); 14 | } else { 15 | root.Mantis = root.$ = factory(); 16 | } 17 | })(this, function() { 18 | 'use strict'; 19 | /* 20 | * Mantis.js 21 | * Core - src/core.js 22 | */ 23 | var $ = function(selector, context) { 24 | return new Mantis.fn.init(selector, context); 25 | }, 26 | _$, 27 | _Mantis, 28 | types = 'Boolean Number String Function Array Date RegExp Object Error'.split(' '), 29 | class2type = {}; 30 | if (window) { 31 | _Mantis = window.Mantis; 32 | _$ = window.$; 33 | $.noConflict = function(deep) { 34 | if (window.$ === $) { 35 | window.$ = _$; 36 | } 37 | if (deep && window.Mantis === $) { 38 | window.Mantis = _Mantis; 39 | } 40 | return $; 41 | }; 42 | } 43 | 44 | function Mantis(nodes) { 45 | var i; 46 | nodes = nodes || []; 47 | for (i = 0; i < nodes.length; i++) { 48 | this[i] = nodes[i]; 49 | } 50 | this.length = nodes.length; 51 | this.selector = nodes.selector; 52 | this.context = nodes.context; 53 | } 54 | Mantis.fn = Mantis.prototype = { 55 | constructor: Mantis, 56 | version: '0.0.0', 57 | selector: '', 58 | length: 0 59 | }; 60 | $.fn = Mantis.fn; 61 | Mantis.fn.init = function(selector, context) { 62 | var nodes; 63 | if (!selector) { 64 | return this; 65 | } 66 | context = context || document; 67 | if (typeof selector === 'string') { 68 | nodes = context.querySelectorAll(selector); 69 | } else if (selector.length) { 70 | nodes = selector; 71 | } else { 72 | nodes = [selector]; 73 | } 74 | nodes.selector = selector; 75 | nodes.context = context; 76 | return new Mantis(nodes); 77 | }; 78 | Mantis.fn.init.prototype = Mantis.fn; 79 | /* 80 | * Mantis.js 81 | * Utils - src/utils.js 82 | */ 83 | function likeArray(obj) { 84 | return typeof obj.length === 'number'; 85 | } 86 | 87 | function flatten(array) { 88 | return array.length > 0 ? [].concat.apply([], array) : array; 89 | } 90 | $.map = function(elements, callback) { 91 | var results = [], 92 | result, 93 | key, 94 | i; 95 | if (likeArray(elements)) { 96 | for (i = 0; i < elements.length; i++) { 97 | result = callback(elements[i], i); 98 | if (result != null) { 99 | results.push(result); 100 | } 101 | } 102 | } else { 103 | for (key in elements) { 104 | result = callback(elements[key], key); 105 | if (result != null) { 106 | results.push(result); 107 | } 108 | } 109 | } 110 | return flatten(results); 111 | }; 112 | for (var i = 0; types.length > i; i++) { 113 | class2type['[object ' + types[i] + ']'] = types[i].toLowerCase(); 114 | } 115 | $.type = function(obj) { 116 | if (obj == null) { 117 | return obj + ''; 118 | } 119 | if (typeof obj === 'object' || typeof obj === 'function') { 120 | return class2type[class2type.toString.call(obj)] || 'object'; 121 | } else { 122 | return typeof obj; 123 | } 124 | }; 125 | $.extend = function(target, source) { 126 | var p; 127 | for (p in source) { 128 | try { 129 | if (source[p].constructor === Object) { 130 | target[p] = $.extend(target[p], source[p]); 131 | } else { 132 | target[p] = source[p]; 133 | } 134 | } catch (e) { 135 | target[p] = source[p]; 136 | } 137 | } 138 | return target; 139 | }; 140 | Mantis.extend = Mantis.fn.extend = function(obj) { 141 | return $.extend(Mantis.fn, obj); 142 | }; 143 | Mantis.extend({ 144 | each: function(callback) { 145 | return this.map(callback); 146 | }, 147 | map: function(callback) { 148 | return $.map(this, function(element, i) { 149 | return callback.call(element, i, element); 150 | }); 151 | } 152 | }); 153 | /* 154 | * Mantis.js 155 | * Attributes - src/attributes.js 156 | */ 157 | Mantis.extend({ 158 | attr: function(attr, value) { 159 | if (typeof attr === 'object') { 160 | return this.each(function() { 161 | var a; 162 | for (a in attr) { 163 | this.setAttribute(a, attr[a]); 164 | } 165 | }); 166 | } else { 167 | if (value) { 168 | return this.each(function() { 169 | this.setAttribute(attr, value); 170 | }); 171 | } else { 172 | return this[0].getAttribute(attr); 173 | } 174 | } 175 | }, 176 | removeAttr: function(value) { 177 | var v = value.split(' '); 178 | return this.each(function() { 179 | var i; 180 | for (i = 0; i < v.length; i++) { 181 | this.removeAttribute(v[i]); 182 | } 183 | }); 184 | }, 185 | hasAttr: function(value) { 186 | var v = value.split(' '), 187 | r = false; 188 | this.each(function() { 189 | for (var i = 0; i < v.length; i++) { 190 | if (this.hasAttribute(v[i])) { 191 | r = true; 192 | } 193 | } 194 | }); 195 | return r; 196 | } 197 | }); 198 | /* 199 | * Mantis.js 200 | * CSS - src/css.js 201 | */ 202 | Mantis.extend({ 203 | css: function(prop, value) { 204 | if (typeof prop === 'object') { 205 | return this.each(function() { 206 | var key; 207 | for (key in prop) { 208 | this.style[key] = prop[key]; 209 | } 210 | }); 211 | } else if (value) { 212 | return this.each(function() { 213 | this.style[prop] = value; 214 | }); 215 | } else { 216 | return getComputedStyle(this[0], null).getPropertyValue(prop); 217 | } 218 | } 219 | }); 220 | /* 221 | * Mantis.js 222 | * Classes - src/classes.js 223 | */ 224 | Mantis.extend({ 225 | addClass: function(value) { 226 | if (this[0].classList) { 227 | var v = value.split(' '); 228 | return this.each(function() { 229 | var i; 230 | for (i = 0; i < v.length; i++) { 231 | this.classList.add(v[i]); 232 | } 233 | }); 234 | } else { 235 | return this.each(function() { 236 | this.className += (this.className === '') ? value : ' ' + value; 237 | }); 238 | } 239 | }, 240 | removeClass: function(value) { 241 | var v = value.split(' '); 242 | if (this[0].classList) { 243 | return this.each(function() { 244 | var i; 245 | for (i = 0; i < v.length; i++) { 246 | this.classList.remove(v[i]); 247 | } 248 | }); 249 | } else { 250 | return this.each(function() { 251 | var c = ' ' + this.className.replace(/[\t\r\n]/g, ' ') + ' ', 252 | i; 253 | for (i = 0; i < v.length; i++) { 254 | while (c.indexOf(' ' + v[i] + ' ') >= 0) { 255 | c = c.replace(' ' + v[i] + ' ', ' '); 256 | } 257 | } 258 | this.className = c.replace(/^\s+|\s+$/g, ''); 259 | }); 260 | } 261 | }, 262 | hasClass: function(value) { 263 | var v = value.split(' '), 264 | contains, 265 | i; 266 | if (this[0].classList) { 267 | contains = function(v) { 268 | return this.classList.contains(v); 269 | }; 270 | } else { 271 | contains = function(v) { 272 | var c = ' ' + this.className.replace(/[\t\r\n]/g, ' ') + ' '; 273 | return (c.indexOf(' ' + v + ' ') >= 0); 274 | }; 275 | } 276 | for (i = 0; i < v.length; i++) { 277 | if (contains.call(this[0], v[i])) { 278 | return true; 279 | } 280 | } 281 | return false; 282 | }, 283 | toggleClass: function(value) { 284 | var v = value.split(' '); 285 | return this.each(function() { 286 | var $this = $(this), 287 | i; 288 | for (i = 0; i < v.length; i++) { 289 | $this[($this.hasClass(v[i])) ? 'removeClass' : 'addClass'](v[i]); 290 | } 291 | }); 292 | } 293 | }); 294 | /* 295 | * Mantis.js 296 | * Manipulation - src/manipulation.js 297 | */ 298 | Mantis.extend({ 299 | html: function(value) { 300 | if (value) { 301 | return this.each(function() { 302 | this.innerHTML = value; 303 | }); 304 | } else { 305 | return this[0].innerHTML; 306 | } 307 | }, 308 | append: function(value) { 309 | var i; 310 | if (typeof value === 'string') { 311 | return this.each(function() { 312 | this.insertAdjacentHTML('beforeend', value); 313 | }); 314 | } else if (value.constructor === Mantis) { 315 | return this.each(function() { 316 | for (i = 0; i < value.length; i++) { 317 | this.appendChild(value[i]); 318 | } 319 | }); 320 | } 321 | }, 322 | prepend: function(value) { 323 | var length; 324 | if (typeof value === 'string') { 325 | return this.each(function() { 326 | this.insertAdjacentHTML('afterbegin', value); 327 | }); 328 | } else if (value.constructor === Mantis) { 329 | length = value.length; 330 | return this.each(function() { 331 | while (length--) { 332 | this.insertBefore(value[length], this.firstChild); 333 | } 334 | }); 335 | } 336 | }, 337 | before: function(value) { 338 | var i; 339 | if (typeof value === 'string') { 340 | return this.each(function() { 341 | this.insertAdjacentHTML('beforebegin', value); 342 | }); 343 | } else if (value.constructor === Mantis) { 344 | return this.each(function() { 345 | for (i = 0; i < value.length; i++) { 346 | this.parentNode.insertBefore(value[i], this); 347 | } 348 | }); 349 | } 350 | }, 351 | after: function(value) { 352 | var length; 353 | if (typeof value === 'string') { 354 | return this.each(function() { 355 | this.insertAdjacentHTML('afterend', value); 356 | }); 357 | } else if (value.constructor === Mantis) { 358 | length = value.length; 359 | return this.each(function() { 360 | while (length--) { 361 | this.parentNode.insertBefore(value[length], this.nextSibling); 362 | } 363 | }); 364 | } 365 | } 366 | }); 367 | return $; 368 | }); -------------------------------------------------------------------------------- /mantis.min.js: -------------------------------------------------------------------------------- 1 | !function(t,n){"use strict";"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?module.exports=n():t.Mantis=t.$=n()}(this,function(){"use strict";function t(t){var n;for(t=t||[],n=0;n0?[].concat.apply([],t):t}var i,r,s=function(n,e){return new t.fn.init(n,e)},o="Boolean Number String Function Array Date RegExp Object Error".split(" "),c={};window&&(r=window.Mantis,i=window.$,s.noConflict=function(t){return window.$===s&&(window.$=i),t&&window.Mantis===s&&(window.Mantis=r),s}),t.fn=t.prototype={constructor:t,version:"0.0.0",selector:"",length:0},s.fn=t.fn,t.fn.init=function(n,e){var i;return n?(e=e||document,i="string"==typeof n?e.querySelectorAll(n):n.length?n:[n],i.selector=n,i.context=e,new t(i)):this},t.fn.init.prototype=t.fn,s.map=function(t,i){var r,s,o,c=[];if(n(t))for(o=0;oa;a++)c["[object "+o[a]+"]"]=o[a].toLowerCase();return s.type=function(t){return null==t?t+"":"object"==typeof t||"function"==typeof t?c[c.toString.call(t)]||"object":typeof t},s.extend=function(t,n){var e;for(e in n)try{t[e]=n[e].constructor===Object?s.extend(t[e],n[e]):n[e]}catch(i){t[e]=n[e]}return t},t.extend=t.fn.extend=function(n){return s.extend(t.fn,n)},t.extend({each:function(t){return this.map(t)},map:function(t){return s.map(this,function(n,e){return t.call(n,e,n)})}}),t.extend({attr:function(t,n){return"object"==typeof t?this.each(function(){var n;for(n in t)this.setAttribute(n,t[n])}):n?this.each(function(){this.setAttribute(t,n)}):this[0].getAttribute(t)},removeAttr:function(t){var n=t.split(" ");return this.each(function(){var t;for(t=0;t=0;)e=e.replace(" "+n[t]+" "," ");this.className=e.replace(/^\s+|\s+$/g,"")})},hasClass:function(t){var n,e,i=t.split(" ");for(n=this[0].classList?function(t){return this.classList.contains(t)}:function(t){var n=" "+this.className.replace(/[\t\r\n]/g," ")+" ";return n.indexOf(" "+t+" ")>=0},e=0;e", 34 | "license": "MIT", 35 | "bugs": { 36 | "url": "https://github.com/acauamontiel/mantis-js/issues" 37 | }, 38 | "homepage": "http://mantisjs.com" 39 | } 40 | -------------------------------------------------------------------------------- /src/attributes.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Mantis.js 3 | * Attributes - src/attributes.js 4 | */ 5 | 6 | Mantis.extend({ 7 | attr: function (attr, value) { 8 | if (typeof attr === 'object') { 9 | return this.each(function() { 10 | var a; 11 | 12 | for (a in attr) { 13 | this.setAttribute(a, attr[a]); 14 | } 15 | }); 16 | } else { 17 | if (value) { 18 | return this.each(function() { 19 | this.setAttribute(attr, value); 20 | }); 21 | } else { 22 | return this[0].getAttribute(attr); 23 | } 24 | } 25 | }, 26 | 27 | removeAttr: function (value) { 28 | var v = value.split(' '); 29 | 30 | return this.each(function() { 31 | var i; 32 | 33 | for (i = 0; i < v.length; i++) { 34 | this.removeAttribute(v[i]); 35 | } 36 | }); 37 | }, 38 | 39 | hasAttr: function (value) { 40 | var v = value.split(' '), 41 | r = false; 42 | 43 | this.each(function() { 44 | for (var i = 0; i < v.length; i++) { 45 | if (this.hasAttribute(v[i])) { 46 | r = true; 47 | } 48 | } 49 | }); 50 | 51 | return r; 52 | } 53 | }); 54 | -------------------------------------------------------------------------------- /src/classes.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Mantis.js 3 | * Classes - src/classes.js 4 | */ 5 | 6 | Mantis.extend({ 7 | addClass: function (value) { 8 | if (this[0].classList) { 9 | var v = value.split(' '); 10 | 11 | return this.each(function () { 12 | var i; 13 | 14 | for (i = 0; i < v.length; i++) { 15 | this.classList.add(v[i]); 16 | } 17 | }); 18 | } else { 19 | return this.each(function () { 20 | this.className += (this.className === '') ? 21 | value : ' ' + value; 22 | }); 23 | } 24 | }, 25 | 26 | removeClass: function (value) { 27 | var v = value.split(' '); 28 | 29 | if (this[0].classList) { 30 | return this.each(function () { 31 | var i; 32 | 33 | for (i = 0; i < v.length; i++) { 34 | this.classList.remove(v[i]); 35 | } 36 | }); 37 | } else { 38 | return this.each(function () { 39 | var c = ' ' + this.className.replace(/[\t\r\n]/g, ' ') + ' ', 40 | i; 41 | 42 | for (i = 0; i < v.length; i++) { 43 | while (c.indexOf(' ' + v[i] + ' ') >= 0) { 44 | c = c.replace(' ' + v[i] + ' ', ' '); 45 | } 46 | } 47 | 48 | this.className = c.replace(/^\s+|\s+$/g, ''); 49 | }); 50 | } 51 | }, 52 | 53 | hasClass: function (value) { 54 | var v = value.split(' '), 55 | contains, 56 | i; 57 | 58 | if (this[0].classList) { 59 | contains = function (v) { 60 | return this.classList.contains(v); 61 | }; 62 | } else { 63 | contains = function (v) { 64 | var c = ' ' + this.className.replace(/[\t\r\n]/g, ' ') + ' '; 65 | return (c.indexOf(' ' + v + ' ') >= 0); 66 | }; 67 | } 68 | 69 | for (i = 0; i < v.length; i++) { 70 | if (contains.call(this[0], v[i])) { 71 | return true; 72 | } 73 | } 74 | 75 | return false; 76 | }, 77 | 78 | toggleClass: function (value) { 79 | var v = value.split(' '); 80 | 81 | return this.each(function () { 82 | var $this = $(this), 83 | i; 84 | 85 | for (i = 0; i < v.length; i++) { 86 | $this[($this.hasClass(v[i])) ? 87 | 'removeClass' : 'addClass'](v[i]); 88 | } 89 | }); 90 | } 91 | }); 92 | -------------------------------------------------------------------------------- /src/core.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Mantis.js 3 | * Core - src/core.js 4 | */ 5 | 6 | var $ = function (selector, context) { 7 | return new Mantis.fn.init(selector, context); 8 | }, 9 | _$, 10 | _Mantis, 11 | types = 'Boolean Number String Function Array Date RegExp Object Error'.split(' '), 12 | class2type = {}; 13 | 14 | if (window) { 15 | _Mantis = window.Mantis; 16 | _$ = window.$; 17 | 18 | $.noConflict = function (deep) { 19 | if (window.$ === $) { 20 | window.$ = _$; 21 | } 22 | 23 | if (deep && window.Mantis === $) { 24 | window.Mantis = _Mantis; 25 | } 26 | 27 | return $; 28 | }; 29 | } 30 | 31 | function Mantis (nodes) { 32 | var i; 33 | 34 | nodes = nodes || []; 35 | 36 | for (i = 0; i < nodes.length; i++) { 37 | this[i] = nodes[i]; 38 | } 39 | 40 | this.length = nodes.length; 41 | this.selector = nodes.selector; 42 | this.context = nodes.context; 43 | } 44 | 45 | Mantis.fn = Mantis.prototype = { 46 | constructor: Mantis, 47 | version: '0.0.0', 48 | selector: '', 49 | length: 0 50 | }; 51 | 52 | $.fn = Mantis.fn; 53 | 54 | Mantis.fn.init = function (selector, context) { 55 | var nodes; 56 | 57 | if (!selector) { 58 | return this; 59 | } 60 | 61 | context = context || document; 62 | 63 | if (typeof selector === 'string') { 64 | nodes = context.querySelectorAll(selector); 65 | } else if (selector.length) { 66 | nodes = selector; 67 | } else { 68 | nodes = [selector]; 69 | } 70 | 71 | nodes.selector = selector; 72 | nodes.context = context; 73 | 74 | return new Mantis(nodes); 75 | }; 76 | 77 | Mantis.fn.init.prototype = Mantis.fn; 78 | -------------------------------------------------------------------------------- /src/css.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Mantis.js 3 | * CSS - src/css.js 4 | */ 5 | 6 | Mantis.extend({ 7 | css: function (prop, value) { 8 | if (typeof prop === 'object') { 9 | return this.each(function() { 10 | var key; 11 | 12 | for (key in prop) { 13 | this.style[key] = prop[key]; 14 | } 15 | }); 16 | } else if (value) { 17 | return this.each(function() { 18 | this.style[prop] = value; 19 | }); 20 | } 21 | 22 | return getComputedStyle(this[0], null).getPropertyValue(prop); 23 | } 24 | }); 25 | -------------------------------------------------------------------------------- /src/manipulation.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Mantis.js 3 | * Manipulation - src/manipulation.js 4 | */ 5 | 6 | Mantis.extend({ 7 | html: function (value) { 8 | if (value) { 9 | return this.each(function () { 10 | this.innerHTML = value; 11 | }); 12 | } else { 13 | return this[0].innerHTML; 14 | } 15 | }, 16 | 17 | append: function (value) { 18 | var i; 19 | 20 | if (typeof value === 'string') { 21 | return this.each(function () { 22 | this.insertAdjacentHTML('beforeend', value); 23 | }); 24 | } else if (value.constructor === Mantis) { 25 | return this.each(function () { 26 | for (i = 0; i < value.length; i++) { 27 | this.appendChild(value[i]); 28 | } 29 | }); 30 | } 31 | }, 32 | 33 | prepend: function (value) { 34 | var length; 35 | 36 | if (typeof value === 'string') { 37 | return this.each(function () { 38 | this.insertAdjacentHTML('afterbegin', value); 39 | }); 40 | } else if (value.constructor === Mantis) { 41 | length = value.length; 42 | 43 | return this.each(function () { 44 | while (length--) { 45 | this.insertBefore(value[length], this.firstChild); 46 | } 47 | }); 48 | } 49 | }, 50 | 51 | before: function (value) { 52 | var i; 53 | 54 | if (typeof value === 'string') { 55 | return this.each(function () { 56 | this.insertAdjacentHTML('beforebegin', value); 57 | }); 58 | } else if (value.constructor === Mantis) { 59 | return this.each(function () { 60 | for (i = 0; i < value.length; i++) { 61 | this.parentNode.insertBefore(value[i], this); 62 | } 63 | }); 64 | } 65 | }, 66 | 67 | after: function (value) { 68 | var length; 69 | 70 | if (typeof value === 'string') { 71 | return this.each(function () { 72 | this.insertAdjacentHTML('afterend', value); 73 | }); 74 | } else if (value.constructor === Mantis) { 75 | length = value.length; 76 | 77 | return this.each(function () { 78 | while (length--) { 79 | this.parentNode.insertBefore(value[length], this.nextSibling); 80 | } 81 | }); 82 | } 83 | } 84 | }); 85 | -------------------------------------------------------------------------------- /src/mantis.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Mantis.js 3 | * http://mantisjs.com 4 | * 5 | * Copyright 2013 - 2015 Acaua Montiel (@acauamontiel) 6 | * Released under the MIT license (http://acaua.mit-license.org) 7 | */ 8 | 9 | (function (root, factory) { 10 | 'use strict'; 11 | 12 | if (typeof define === 'function' && define.amd) { 13 | define([], factory); 14 | } else if (typeof exports === 'object') { 15 | module.exports = factory(); 16 | } else { 17 | root.Mantis = root.$ = factory(); 18 | } 19 | })(this, function () { 20 | 'use strict'; 21 | 22 | //= include ./core.js 23 | //= include ./utils.js 24 | //= include ./attributes.js 25 | //= include ./css.js 26 | //= include ./classes.js 27 | //= include ./manipulation.js 28 | 29 | return $; 30 | }); 31 | -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Mantis.js 3 | * Utils - src/utils.js 4 | */ 5 | 6 | function likeArray(obj) { 7 | return typeof obj.length === 'number'; 8 | } 9 | 10 | function flatten(array) { 11 | return array.length > 0 ? [].concat.apply([], array) : array; 12 | } 13 | 14 | $.map = function (elements, callback) { 15 | var results = [], 16 | result, 17 | key, 18 | i; 19 | 20 | if (likeArray(elements)) { 21 | for (i = 0; i < elements.length; i++) { 22 | result = callback(elements[i], i); 23 | 24 | if (result != null) { 25 | results.push(result); 26 | } 27 | } 28 | } else { 29 | for (key in elements) { 30 | result = callback(elements[key], key); 31 | 32 | if (result != null) { 33 | results.push(result); 34 | } 35 | } 36 | } 37 | 38 | return flatten(results); 39 | }; 40 | 41 | for (var i = 0; types.length > i; i++) { 42 | class2type['[object ' + types[i] + ']' ] = types[i].toLowerCase(); 43 | } 44 | 45 | $.type = function (obj) { 46 | if (obj == null) { 47 | return obj + ''; 48 | } 49 | 50 | if (typeof obj === 'object' || typeof obj === 'function') { 51 | return class2type[class2type.toString.call(obj)] || 'object'; 52 | } else { 53 | return typeof obj; 54 | } 55 | }; 56 | 57 | $.extend = function (target, source) { 58 | var p; 59 | 60 | for (p in source) { 61 | try { 62 | if (source[p].constructor === Object) { 63 | target[p] = $.extend(target[p], source[p]); 64 | } else { 65 | target[p] = source[p]; 66 | } 67 | } catch(e) { 68 | target[p] = source[p]; 69 | } 70 | } 71 | 72 | return target; 73 | }; 74 | 75 | Mantis.extend = Mantis.fn.extend = function (obj) { 76 | return $.extend(Mantis.fn, obj); 77 | }; 78 | 79 | Mantis.extend({ 80 | each: function (callback) { 81 | return this.map(callback); 82 | }, 83 | 84 | map: function (callback) { 85 | return $.map(this, function(element, i) { 86 | return callback.call(element, i, element); 87 | }); 88 | } 89 | }); 90 | --------------------------------------------------------------------------------