Primer Commit

This commit is contained in:
LuisAngelSalinasl
2025-08-04 18:51:41 -06:00
commit 8fcbb98114
8990 changed files with 1407288 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

View File

@@ -0,0 +1,293 @@
# [jQuery asGradient](https://github.com/amazingSurge/jquery-asGradient) ![bower][bower-image] [![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] [![prs-welcome]](#contributing)
> A jquery plugin used to manipulate css image gradient. You can add a new color stop. Change the position of color stop. Or remove a color stop. In the end, it can output a formated standard css gradient string.
## Table of contents
- [Main files](#main-files)
- [Quick start](#quick-start)
- [Requirements](#requirements)
- [Usage](#usage)
- [Examples](#examples)
- [Options](#options)
- [Methods](#methods)
- [No conflict](#no-conflict)
- [Browser support](#browser-support)
- [Contributing](#contributing)
- [Development](#development)
- [Changelog](#changelog)
- [Copyright and license](#copyright-and-license)
## Main files
```
dist/
├── jquery-asGradient.js
├── jquery-asGradient.es.js
└── jquery-asGradient.min.js
```
## Quick start
Several quick start options are available:
#### Download the latest build
* [Development](https://raw.githubusercontent.com/amazingSurge/jquery-asGradient/master/dist/jquery-asGradient.js) - unminified
* [Production](https://raw.githubusercontent.com/amazingSurge/jquery-asGradient/master/dist/jquery-asGradient.min.js) - minified
#### Install From Bower
```sh
bower install jquery-asGradient --save
```
#### Install From Npm
```sh
npm install jquery-asGradient --save
```
#### Install From Yarn
```sh
yarn add jquery-asGradient
```
#### Build From Source
If you want build from source:
```sh
git clone git@github.com:amazingSurge/jquery-asGradient.git
cd jquery-asGradient
npm install
npm install -g gulp-cli babel-cli
gulp build
```
Done!
## Requirements
`jquery-asGradient` requires the latest version of [`jQuery`](https://jquery.com/download/) and [`jQuery-asColor`](https://github.com/amazingSurge/jquery-asColor).
## Usage
#### Including files:
```html
<script src="/path/to/jquery.js"></script>
<script src="/path/to/jquery-asColor.js"></script>
<script src="/path/to/jquery-asGradient.js"></script>
```
#### Initialization
All you need to do is call the plugin on the element:
```javascript
var gradient = new AsGradient('linear-gradient(to rgba(0, 0, 0, 1), rgba(255, 255, 255, 1))', {
cleanPosition: true,
color: {
format: 'rgba'
}
});
```
## Examples
There are some example usages that you can look at to get started. They can be found in the
[examples folder](https://github.com/amazingSurge/jquery-asGradient/tree/master/examples).
## Options
`jquery-asGradient` can accept an options object to alter the way it behaves. You can see the default options by call `$.asGradient.setDefaults()`. The structure of an options object is as follows:
```
{
prefixes: ['-webkit-', '-moz-', '-ms-', '-o-'],
forceStandard: true,
angleUseKeyword: true,
emptyString: '',
degradationFormat: false,
cleanPosition: true,
color: {
format: false, // rgb, rgba, hsl, hsla, hex
hexUseName: false,
reduceAlpha: true,
shortenHex: true,
zeroAlphaAsTransparent: false,
invalidValue: {
r: 0,
g: 0,
b: 0,
a: 1
}
}
}
```
## Methods
```javascript
var gradient = new AsGradient('linear-gradient(to bottom, yellow, blue)');
gradient.toString();
```
#### toString()
Get gradient string.
```javascript
// Get standard string.
gradient.toString();
// Get string by prefix.
gradient.toString('-moz-');
```
#### fromString()
Set values from gradient string.
```javascript
gradient.fromString('linear-gradient(to bottom, yellow 0%, blue 100%)');
```
#### getPrefixedStrings()
Get prefixed strings array.
```javascript
gradient.getPrefixedStrings();
```
#### val()
Get or set gradient string.
```javascript
// get gradient string
gradient.val();
// set gradient string
gradient.val('linear-gradient(to bottom, yellow 0%, blue 100%)');
```
#### angle()
Get or set angle.
```javascript
// get gradient angle
gradient.angle();
// set gradient angle
gradient.angle(60);
```
#### append(color, position)
Append a new color stop.
```javascript
gradient.append('#fff', '50%');
```
#### insert(color, position, index)
Insert a color stop to index
```javascript
gradient.append('#fff', '50%', 1);
```
#### getCurrent()
Get current color stop.
```javascript
var stop = gradient.getCurrent();
```
#### setCurrentById(id)
Set current color stop by id.
```javascript
gradient.setCurrentById(2);
```
#### getById(id)
Get color stop by index.
```javascript
var stop = gradient.get(2);
```
#### removeById(id)
Remove color stop by id.
```javascript
gradient.removeById(2);
```
#### get(index)
Get color stop by index.
```javascript
var stop = gradient.get(2);
```
#### remove(index)
Remove color stop by index.
```javascript
gradient.remove(2);
```
#### empty()
Empty color stops.
```javascript
gradient.empty();
```
#### reset()
Reset gradient.
```javascript
gradient.reset();
```
## No conflict
If you have to use other plugin with the same namespace, just call the `$.asGradient.noConflict` method to revert to it.
```html
<script src="other-plugin.js"></script>
<script src="jquery-asGradient.js"></script>
<script>
$.asGradient.noConflict();
// Code that uses other plugin's "$().asGradient" can follow here.
</script>
```
## Browser support
Tested on all major browsers.
| <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/safari/safari_32x32.png" alt="Safari"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/chrome/chrome_32x32.png" alt="Chrome"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/firefox/firefox_32x32.png" alt="Firefox"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/edge/edge_32x32.png" alt="Edge"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/internet-explorer/internet-explorer_32x32.png" alt="IE"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/opera/opera_32x32.png" alt="Opera"> |
|:--:|:--:|:--:|:--:|:--:|:--:|
| Latest ✓ | Latest ✓ | Latest ✓ | Latest ✓ | 9-11 ✓ | Latest ✓ |
As a jQuery plugin, you also need to see the [jQuery Browser Support](http://jquery.com/browser-support/).
## Contributing
Anyone and everyone is welcome to contribute. Please take a moment to
review the [guidelines for contributing](CONTRIBUTING.md). Make sure you're using the latest version of `jquery-asGradient` before submitting an issue. There are several ways to help out:
* [Bug reports](CONTRIBUTING.md#bug-reports)
* [Feature requests](CONTRIBUTING.md#feature-requests)
* [Pull requests](CONTRIBUTING.md#pull-requests)
* Write test cases for open bug issues
* Contribute to the documentation
## Development
`jquery-asGradient` is built modularly and uses Gulp as a build system to build its distributable files. To install the necessary dependencies for the build system, please run:
```sh
npm install -g gulp
npm install -g babel-cli
npm install
```
Then you can generate new distributable files from the sources, using:
```
gulp build
```
More gulp tasks can be found [here](CONTRIBUTING.md#available-tasks).
## Changelog
To see the list of recent changes, see [Releases section](https://github.com/amazingSurge/jquery-asGradient/releases).
## Copyright and license
Copyright (C) 2016 amazingSurge.
Licensed under [the LGPL license](LICENSE).
[⬆ back to top](#table-of-contents)
[bower-image]: https://img.shields.io/bower/v/jquery-asGradient.svg?style=flat
[bower-link]: https://david-dm.org/amazingSurge/jquery-asGradient/dev-status.svg
[npm-image]: https://badge.fury.io/js/jquery-asGradient.svg?style=flat
[npm-url]: https://npmjs.org/package/jquery-asGradient
[license]: https://img.shields.io/npm/l/jquery-asGradient.svg?style=flat
[prs-welcome]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg
[daviddm-image]: https://david-dm.org/amazingSurge/jquery-asGradient.svg?style=flat
[daviddm-url]: https://david-dm.org/amazingSurge/jquery-asGradient

View File

@@ -0,0 +1,647 @@
/**
* jQuery asGradient v0.3.3
* https://github.com/amazingSurge/jquery-asGradient
*
* Copyright (c) amazingSurge
* Released under the LGPL-3.0 license
*/
import $ from 'jquery';
import Color from 'jquery-asColor';
var DEFAULTS = {
prefixes: ['-webkit-', '-moz-', '-ms-', '-o-'],
forceStandard: true,
angleUseKeyword: true,
emptyString: '',
degradationFormat: false,
cleanPosition: true,
color: {
format: false, // rgb, rgba, hsl, hsla, hex
hexUseName: false,
reduceAlpha: true,
shortenHex: true,
zeroAlphaAsTransparent: false,
invalidValue: {
r: 0,
g: 0,
b: 0,
a: 1
}
}
};
/* eslint no-extend-native: "off" */
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
'use strict';
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
}
return this.indexOf(search, start) !== -1;
};
}
function getPrefix() {
const ua = window.navigator.userAgent;
let prefix = '';
if (/MSIE/g.test(ua)) {
prefix = '-ms-';
} else if (/Firefox/g.test(ua)) {
prefix = '-moz-';
} else if (/(WebKit)/i.test(ua)) {
prefix = '-webkit-';
} else if (/Opera/g.test(ua)) {
prefix = '-o-';
}
return prefix;
}
function flip(o) {
const flipped = {};
for (const i in o) {
if (o.hasOwnProperty(i)) {
flipped[o[i]] = i;
}
}
return flipped;
}
function reverseDirection(direction) {
const mapping = {
'top': 'bottom',
'right': 'left',
'bottom': 'top',
'left': 'right',
'right top': 'left bottom',
'top right': 'bottom left',
'bottom right': 'top left',
'right bottom': 'left top',
'left bottom': 'right top',
'bottom left': 'top right',
'top left': 'bottom right',
'left top': 'right bottom'
};
return mapping.hasOwnProperty(direction) ? mapping[direction] : direction;
}
function isDirection(n) {
const reg = /^(top|left|right|bottom)$/i;
return reg.test(n);
}
var keywordAngleMap = {
'to top': 0,
'to right': 90,
'to bottom': 180,
'to left': 270,
'to right top': 45,
'to top right': 45,
'to bottom right': 135,
'to right bottom': 135,
'to left bottom': 225,
'to bottom left': 225,
'to top left': 315,
'to left top': 315
};
const angleKeywordMap = flip(keywordAngleMap);
const RegExpStrings = (() => {
const color = /(?:rgba|rgb|hsla|hsl)\s*\([\s\d\.,%]+\)|#[a-z0-9]{3,6}|[a-z]+/i;
const position = /\d{1,3}%/i;
const angle = /(?:to ){0,1}(?:(?:top|left|right|bottom)\s*){1,2}|\d+deg/i;
const stop = new RegExp(`(${color.source})\\s*(${position.source}){0,1}`, 'i');
const stops = new RegExp(stop.source, 'gi');
const parameters = new RegExp(`(?:(${angle.source})){0,1}\\s*,{0,1}\\s*(.*?)\\s*`, 'i');
const full = new RegExp(`^(-webkit-|-moz-|-ms-|-o-){0,1}(linear|radial|repeating-linear)-gradient\\s*\\(\\s*(${parameters.source})\\s*\\)$`, 'i');
return {
FULL: full,
ANGLE: angle,
COLOR: color,
POSITION: position,
STOP: stop,
STOPS: stops,
PARAMETERS: new RegExp(`^${parameters.source}$`, 'i')
};
})();
var GradientString = {
matchString: function(string) {
const matched = this.parseString(string);
if(matched && matched.value && matched.value.stops && matched.value.stops.length > 1){
return true;
}
return false;
},
parseString: function(string) {
string = $.trim(string);
let matched;
if ((matched = RegExpStrings.FULL.exec(string)) !== null) {
let value = this.parseParameters(matched[3]);
return {
prefix: (typeof matched[1] === 'undefined') ? null : matched[1],
type: matched[2],
value: value
};
} else {
return false;
}
},
parseParameters: function(string) {
let matched;
if ((matched = RegExpStrings.PARAMETERS.exec(string)) !== null) {
let stops = this.parseStops(matched[2]);
return {
angle: (typeof matched[1] === 'undefined') ? 0 : matched[1],
stops: stops
};
} else {
return false;
}
},
parseStops: function(string) {
let matched;
const result = [];
if ((matched = string.match(RegExpStrings.STOPS)) !== null) {
$.each(matched, (i, item) => {
const stop = this.parseStop(item);
if (stop) {
result.push(stop);
}
});
return result;
} else {
return false;
}
},
formatStops: function(stops, cleanPosition) {
let stop;
const output = [];
let positions = [];
const colors = [];
let position;
for (let i = 0; i < stops.length; i++) {
stop = stops[i];
if (typeof stop.position === 'undefined' || stop.position === null) {
if (i === 0) {
position = 0;
} else if (i === stops.length - 1) {
position = 1;
} else {
position = undefined;
}
} else {
position = stop.position;
}
positions.push(position);
colors.push(stop.color.toString());
}
positions = ((data => {
let start = null;
let average;
for (let i = 0; i < data.length; i++) {
if (isNaN(data[i])) {
if (start === null) {
start = i;
continue;
}
} else if (start) {
average = (data[i] - data[start - 1]) / (i - start + 1);
for (let j = start; j < i; j++) {
data[j] = data[start - 1] + (j - start + 1) * average;
}
start = null;
}
}
return data;
}))(positions);
for (let x = 0; x < stops.length; x++) {
if (cleanPosition && ((x === 0 && positions[x] === 0) || (x === stops.length - 1 && positions[x] === 1))) {
position = '';
} else {
position = ` ${this.formatPosition(positions[x])}`;
}
output.push(colors[x] + position);
}
return output.join(', ');
},
parseStop: function(string) {
let matched;
if ((matched = RegExpStrings.STOP.exec(string)) !== null) {
let position = this.parsePosition(matched[2]);
return {
color: matched[1],
position: position
};
} else {
return false;
}
},
parsePosition: function(string) {
if (typeof string === 'string' && string.substr(-1) === '%') {
string = parseFloat(string.slice(0, -1) / 100);
}
if(typeof string !== 'undefined' && string !== null) {
return parseFloat(string, 10);
} else {
return null;
}
},
formatPosition: function(value) {
return `${parseInt(value * 100, 10)}%`;
},
parseAngle: function(string, notStandard) {
if (typeof string === 'string' && string.includes('deg')) {
string = string.replace('deg', '');
}
if (!isNaN(string)) {
if (notStandard) {
string = this.fixOldAngle(string);
}
}
if (typeof string === 'string') {
const directions = string.split(' ');
const filtered = [];
for (const i in directions) {
if (isDirection(directions[i])) {
filtered.push(directions[i].toLowerCase());
}
}
let keyword = filtered.join(' ');
if (!string.includes('to ')) {
keyword = reverseDirection(keyword);
}
keyword = `to ${keyword}`;
if (keywordAngleMap.hasOwnProperty(keyword)) {
string = keywordAngleMap[keyword];
}
}
let value = parseFloat(string, 10);
if (value > 360) {
value %= 360;
} else if (value < 0) {
value %= -360;
if (value !== 0) {
value += 360;
}
}
return value;
},
fixOldAngle: function(value) {
value = parseFloat(value);
value = Math.abs(450 - value) % 360;
value = parseFloat(value.toFixed(3));
return value;
},
formatAngle: function(value, notStandard, useKeyword) {
value = parseInt(value, 10);
if (useKeyword && angleKeywordMap.hasOwnProperty(value)) {
value = angleKeywordMap[value];
if (notStandard) {
value = reverseDirection(value.substr(3));
}
} else {
if (notStandard) {
value = this.fixOldAngle(value);
}
value = `${value}deg`;
}
return value;
}
};
class ColorStop {
constructor(color, position, gradient) {
this.color = Color(color, gradient.options.color);
this.position = GradientString.parsePosition(position);
this.id = ++gradient._stopIdCount;
this.gradient = gradient;
}
setPosition(string) {
const position = GradientString.parsePosition(string);
if(this.position !== position){
this.position = position;
this.gradient.reorder();
}
}
setColor(string) {
this.color.fromString(string);
}
remove() {
this.gradient.removeById(this.id);
}
}
var GradientTypes = {
LINEAR: {
parse(result) {
return {
r: (result[1].substr(-1) === '%') ? parseInt(result[1].slice(0, -1) * 2.55, 10) : parseInt(result[1], 10),
g: (result[2].substr(-1) === '%') ? parseInt(result[2].slice(0, -1) * 2.55, 10) : parseInt(result[2], 10),
b: (result[3].substr(-1) === '%') ? parseInt(result[3].slice(0, -1) * 2.55, 10) : parseInt(result[3], 10),
a: 1
};
},
to(gradient, instance, prefix) {
if (gradient.stops.length === 0) {
return instance.options.emptyString;
}
if (gradient.stops.length === 1) {
return gradient.stops[0].color.to(instance.options.degradationFormat);
}
let standard = instance.options.forceStandard;
let _prefix = instance._prefix;
if (!_prefix) {
standard = true;
}
if (prefix && -1 !== $.inArray(prefix, instance.options.prefixes)) {
standard = false;
_prefix = prefix;
}
const angle = GradientString.formatAngle(gradient.angle, !standard, instance.options.angleUseKeyword);
const stops = GradientString.formatStops(gradient.stops, instance.options.cleanPosition);
const output = `linear-gradient(${angle}, ${stops})`;
if (standard) {
return output;
} else {
return _prefix + output;
}
}
}
};
class AsGradient {
constructor(string, options) {
if (typeof string === 'object' && typeof options === 'undefined') {
options = string;
string = undefined;
}
this.value = {
angle: 0,
stops: []
};
this.options = $.extend(true, {}, DEFAULTS, options);
this._type = 'LINEAR';
this._prefix = null;
this.length = this.value.stops.length;
this.current = 0;
this._stopIdCount = 0;
this.init(string);
}
init(string) {
if (string) {
this.fromString(string);
}
}
val(value) {
if (typeof value === 'undefined') {
return this.toString();
} else {
this.fromString(value);
return this;
}
}
angle(value) {
if (typeof value === 'undefined') {
return this.value.angle;
} else {
this.value.angle = GradientString.parseAngle(value);
return this;
}
}
append(color, position) {
return this.insert(color, position, this.length);
}
reorder() {
if(this.length < 2){
return;
}
this.value.stops = this.value.stops.sort((a, b) => a.position - b.position);
}
insert(color, position, index) {
if (typeof index === 'undefined') {
index = this.current;
}
const stop = new ColorStop(color, position, this);
this.value.stops.splice(index, 0, stop);
this.length = this.length + 1;
this.current = index;
return stop;
}
getById(id) {
if(this.length > 0){
for(const i in this.value.stops){
if(id === this.value.stops[i].id){
return this.value.stops[i];
}
}
}
return false;
}
removeById(id) {
const index = this.getIndexById(id);
if(index){
this.remove(index);
}
}
getIndexById(id) {
let index = 0;
for(const i in this.value.stops){
if(id === this.value.stops[i].id){
return index;
}
index ++;
}
return false;
}
getCurrent() {
return this.value.stops[this.current];
}
setCurrentById(id) {
let index = 0;
for(const i in this.value.stops){
if(this.value.stops[i].id !== id){
index ++;
} else {
this.current = index;
}
}
}
get(index) {
if (typeof index === 'undefined') {
index = this.current;
}
if (index >= 0 && index < this.length) {
this.current = index;
return this.value.stops[index];
} else {
return false;
}
}
remove(index) {
if (typeof index === 'undefined') {
index = this.current;
}
if (index >= 0 && index < this.length) {
this.value.stops.splice(index, 1);
this.length = this.length - 1;
this.current = index - 1;
}
}
empty() {
this.value.stops = [];
this.length = 0;
this.current = 0;
}
reset() {
this.value._angle = 0;
this.empty();
this._prefix = null;
this._type = 'LINEAR';
}
type(type) {
if (typeof type === 'string' && (type = type.toUpperCase()) && typeof GradientTypes[type] !== 'undefined') {
this._type = type;
return this;
} else {
return this._type;
}
}
fromString(string) {
this.reset();
const result = GradientString.parseString(string);
if (result) {
this._prefix = result.prefix;
this.type(result.type);
if (result.value) {
this.value.angle = GradientString.parseAngle(result.value.angle, this._prefix !== null);
$.each(result.value.stops, (i, stop) => {
this.append(stop.color, stop.position);
});
}
}
}
toString(prefix) {
if(prefix === true){
prefix = getPrefix();
}
return GradientTypes[this.type()].to(this.value, this, prefix);
}
matchString(string) {
return GradientString.matchString(string);
}
toStringWithAngle(angle, prefix) {
const value = $.extend(true, {}, this.value);
value.angle = GradientString.parseAngle(angle);
if(prefix === true){
prefix = getPrefix();
}
return GradientTypes[this.type()].to(value, this, prefix);
}
getPrefixedStrings() {
const strings = [];
for (let i in this.options.prefixes) {
if(Object.hasOwnProperty.call(this.options.prefixes, i)){
strings.push(this.toString(this.options.prefixes[i]));
}
}
return strings;
}
static setDefaults(options) {
$.extend(true, DEFAULTS, $.isPlainObject(options) && options);
}
}
var info = {
version:'0.3.3'
};
const OtherAsGradient = $.asGradient;
const jQueryAsGradient = function(...args) {
return new AsGradient(...args);
};
$.asGradient = jQueryAsGradient;
$.asGradient.Constructor = AsGradient;
$.extend($.asGradient, {
setDefaults: AsGradient.setDefaults,
noConflict: function() {
$.asGradient = OtherAsGradient;
return jQueryAsGradient;
}
}, GradientString, info);
var main = $.asGradient;
export default main;

View File

@@ -0,0 +1,859 @@
/**
* jQuery asGradient v0.3.3
* https://github.com/amazingSurge/jquery-asGradient
*
* Copyright (c) amazingSurge
* Released under the LGPL-3.0 license
*/
(function(global, factory) {
if (typeof define === 'function' && define.amd) {
define('AsGradient', ['exports', 'jquery', 'jquery-asColor'], factory);
} else if (typeof exports !== 'undefined') {
factory(exports, require('jquery'), require('jquery-asColor'));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.jQuery, global.AsColor);
global.AsGradient = mod.exports;
}
})(this, function(exports, _jquery, _jqueryAsColor) {
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _jquery2 = _interopRequireDefault(_jquery);
var _jqueryAsColor2 = _interopRequireDefault(_jqueryAsColor);
function _interopRequireDefault(obj) {
return obj && obj.__esModule
? obj
: {
default: obj
};
}
var _typeof =
typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'
? function(obj) {
return typeof obj;
}
: function(obj) {
return obj &&
typeof Symbol === 'function' &&
obj.constructor === Symbol &&
obj !== Symbol.prototype
? 'symbol'
: typeof obj;
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
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;
};
})();
var DEFAULTS = {
prefixes: ['-webkit-', '-moz-', '-ms-', '-o-'],
forceStandard: true,
angleUseKeyword: true,
emptyString: '',
degradationFormat: false,
cleanPosition: true,
color: {
format: false, // rgb, rgba, hsl, hsla, hex
hexUseName: false,
reduceAlpha: true,
shortenHex: true,
zeroAlphaAsTransparent: false,
invalidValue: {
r: 0,
g: 0,
b: 0,
a: 1
}
}
};
/* eslint no-extend-native: "off" */
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
'use strict';
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
}
return this.indexOf(search, start) !== -1;
};
}
function getPrefix() {
var ua = window.navigator.userAgent;
var prefix = '';
if (/MSIE/g.test(ua)) {
prefix = '-ms-';
} else if (/Firefox/g.test(ua)) {
prefix = '-moz-';
} else if (/(WebKit)/i.test(ua)) {
prefix = '-webkit-';
} else if (/Opera/g.test(ua)) {
prefix = '-o-';
}
return prefix;
}
function flip(o) {
var flipped = {};
for (var i in o) {
if (o.hasOwnProperty(i)) {
flipped[o[i]] = i;
}
}
return flipped;
}
function reverseDirection(direction) {
var mapping = {
top: 'bottom',
right: 'left',
bottom: 'top',
left: 'right',
'right top': 'left bottom',
'top right': 'bottom left',
'bottom right': 'top left',
'right bottom': 'left top',
'left bottom': 'right top',
'bottom left': 'top right',
'top left': 'bottom right',
'left top': 'right bottom'
};
return mapping.hasOwnProperty(direction) ? mapping[direction] : direction;
}
function isDirection(n) {
var reg = /^(top|left|right|bottom)$/i;
return reg.test(n);
}
var keywordAngleMap = {
'to top': 0,
'to right': 90,
'to bottom': 180,
'to left': 270,
'to right top': 45,
'to top right': 45,
'to bottom right': 135,
'to right bottom': 135,
'to left bottom': 225,
'to bottom left': 225,
'to top left': 315,
'to left top': 315
};
var angleKeywordMap = flip(keywordAngleMap);
var RegExpStrings = (function() {
var color = /(?:rgba|rgb|hsla|hsl)\s*\([\s\d\.,%]+\)|#[a-z0-9]{3,6}|[a-z]+/i;
var position = /\d{1,3}%/i;
var angle = /(?:to ){0,1}(?:(?:top|left|right|bottom)\s*){1,2}|\d+deg/i;
var stop = new RegExp(
'(' + color.source + ')\\s*(' + position.source + '){0,1}',
'i'
);
var stops = new RegExp(stop.source, 'gi');
var parameters = new RegExp(
'(?:(' + angle.source + ')){0,1}\\s*,{0,1}\\s*(.*?)\\s*',
'i'
);
var full = new RegExp(
'^(-webkit-|-moz-|-ms-|-o-){0,1}(linear|radial|repeating-linear)-gradient\\s*\\(\\s*(' +
parameters.source +
')\\s*\\)$',
'i'
);
return {
FULL: full,
ANGLE: angle,
COLOR: color,
POSITION: position,
STOP: stop,
STOPS: stops,
PARAMETERS: new RegExp('^' + parameters.source + '$', 'i')
};
})();
var GradientString = {
matchString: function matchString(string) {
var matched = this.parseString(string);
if (
matched &&
matched.value &&
matched.value.stops &&
matched.value.stops.length > 1
) {
return true;
}
return false;
},
parseString: function parseString(string) {
string = _jquery2.default.trim(string);
var matched = void 0;
if ((matched = RegExpStrings.FULL.exec(string)) !== null) {
var value = this.parseParameters(matched[3]);
return {
prefix: typeof matched[1] === 'undefined' ? null : matched[1],
type: matched[2],
value: value
};
} else {
return false;
}
},
parseParameters: function parseParameters(string) {
var matched = void 0;
if ((matched = RegExpStrings.PARAMETERS.exec(string)) !== null) {
var stops = this.parseStops(matched[2]);
return {
angle: typeof matched[1] === 'undefined' ? 0 : matched[1],
stops: stops
};
} else {
return false;
}
},
parseStops: function parseStops(string) {
var _this = this;
var matched = void 0;
var result = [];
if ((matched = string.match(RegExpStrings.STOPS)) !== null) {
_jquery2.default.each(matched, function(i, item) {
var stop = _this.parseStop(item);
if (stop) {
result.push(stop);
}
});
return result;
} else {
return false;
}
},
formatStops: function formatStops(stops, cleanPosition) {
var stop = void 0;
var output = [];
var positions = [];
var colors = [];
var position = void 0;
for (var i = 0; i < stops.length; i++) {
stop = stops[i];
if (typeof stop.position === 'undefined' || stop.position === null) {
if (i === 0) {
position = 0;
} else if (i === stops.length - 1) {
position = 1;
} else {
position = undefined;
}
} else {
position = stop.position;
}
positions.push(position);
colors.push(stop.color.toString());
}
positions = (function(data) {
var start = null;
var average = void 0;
for (var _i = 0; _i < data.length; _i++) {
if (isNaN(data[_i])) {
if (start === null) {
start = _i;
continue;
}
} else if (start) {
average = (data[_i] - data[start - 1]) / (_i - start + 1);
for (var j = start; j < _i; j++) {
data[j] = data[start - 1] + (j - start + 1) * average;
}
start = null;
}
}
return data;
})(positions);
for (var x = 0; x < stops.length; x++) {
if (
cleanPosition &&
((x === 0 && positions[x] === 0) ||
(x === stops.length - 1 && positions[x] === 1))
) {
position = '';
} else {
position = ' ' + this.formatPosition(positions[x]);
}
output.push(colors[x] + position);
}
return output.join(', ');
},
parseStop: function parseStop(string) {
var matched = void 0;
if ((matched = RegExpStrings.STOP.exec(string)) !== null) {
var position = this.parsePosition(matched[2]);
return {
color: matched[1],
position: position
};
} else {
return false;
}
},
parsePosition: function parsePosition(string) {
if (typeof string === 'string' && string.substr(-1) === '%') {
string = parseFloat(string.slice(0, -1) / 100);
}
if (typeof string !== 'undefined' && string !== null) {
return parseFloat(string, 10);
} else {
return null;
}
},
formatPosition: function formatPosition(value) {
return parseInt(value * 100, 10) + '%';
},
parseAngle: function parseAngle(string, notStandard) {
if (typeof string === 'string' && string.includes('deg')) {
string = string.replace('deg', '');
}
if (!isNaN(string)) {
if (notStandard) {
string = this.fixOldAngle(string);
}
}
if (typeof string === 'string') {
var directions = string.split(' ');
var filtered = [];
for (var i in directions) {
if (isDirection(directions[i])) {
filtered.push(directions[i].toLowerCase());
}
}
var keyword = filtered.join(' ');
if (!string.includes('to ')) {
keyword = reverseDirection(keyword);
}
keyword = 'to ' + keyword;
if (keywordAngleMap.hasOwnProperty(keyword)) {
string = keywordAngleMap[keyword];
}
}
var value = parseFloat(string, 10);
if (value > 360) {
value %= 360;
} else if (value < 0) {
value %= -360;
if (value !== 0) {
value += 360;
}
}
return value;
},
fixOldAngle: function fixOldAngle(value) {
value = parseFloat(value);
value = Math.abs(450 - value) % 360;
value = parseFloat(value.toFixed(3));
return value;
},
formatAngle: function formatAngle(value, notStandard, useKeyword) {
value = parseInt(value, 10);
if (useKeyword && angleKeywordMap.hasOwnProperty(value)) {
value = angleKeywordMap[value];
if (notStandard) {
value = reverseDirection(value.substr(3));
}
} else {
if (notStandard) {
value = this.fixOldAngle(value);
}
value = value + 'deg';
}
return value;
}
};
var ColorStop = (function() {
function ColorStop(color, position, gradient) {
_classCallCheck(this, ColorStop);
this.color = (0, _jqueryAsColor2.default)(color, gradient.options.color);
this.position = GradientString.parsePosition(position);
this.id = ++gradient._stopIdCount;
this.gradient = gradient;
}
_createClass(ColorStop, [
{
key: 'setPosition',
value: function setPosition(string) {
var position = GradientString.parsePosition(string);
if (this.position !== position) {
this.position = position;
this.gradient.reorder();
}
}
},
{
key: 'setColor',
value: function setColor(string) {
this.color.fromString(string);
}
},
{
key: 'remove',
value: function remove() {
this.gradient.removeById(this.id);
}
}
]);
return ColorStop;
})();
var GradientTypes = {
LINEAR: {
parse: function parse(result) {
return {
r:
result[1].substr(-1) === '%'
? parseInt(result[1].slice(0, -1) * 2.55, 10)
: parseInt(result[1], 10),
g:
result[2].substr(-1) === '%'
? parseInt(result[2].slice(0, -1) * 2.55, 10)
: parseInt(result[2], 10),
b:
result[3].substr(-1) === '%'
? parseInt(result[3].slice(0, -1) * 2.55, 10)
: parseInt(result[3], 10),
a: 1
};
},
to: function to(gradient, instance, prefix) {
if (gradient.stops.length === 0) {
return instance.options.emptyString;
}
if (gradient.stops.length === 1) {
return gradient.stops[0].color.to(instance.options.degradationFormat);
}
var standard = instance.options.forceStandard;
var _prefix = instance._prefix;
if (!_prefix) {
standard = true;
}
if (
prefix &&
-1 !== _jquery2.default.inArray(prefix, instance.options.prefixes)
) {
standard = false;
_prefix = prefix;
}
var angle = GradientString.formatAngle(
gradient.angle,
!standard,
instance.options.angleUseKeyword
);
var stops = GradientString.formatStops(
gradient.stops,
instance.options.cleanPosition
);
var output = 'linear-gradient(' + angle + ', ' + stops + ')';
if (standard) {
return output;
} else {
return _prefix + output;
}
}
}
};
var AsGradient = (function() {
function AsGradient(string, options) {
_classCallCheck(this, AsGradient);
if (
(typeof string === 'undefined' ? 'undefined' : _typeof(string)) ===
'object' &&
typeof options === 'undefined'
) {
options = string;
string = undefined;
}
this.value = {
angle: 0,
stops: []
};
this.options = _jquery2.default.extend(true, {}, DEFAULTS, options);
this._type = 'LINEAR';
this._prefix = null;
this.length = this.value.stops.length;
this.current = 0;
this._stopIdCount = 0;
this.init(string);
}
_createClass(
AsGradient,
[
{
key: 'init',
value: function init(string) {
if (string) {
this.fromString(string);
}
}
},
{
key: 'val',
value: function val(value) {
if (typeof value === 'undefined') {
return this.toString();
} else {
this.fromString(value);
return this;
}
}
},
{
key: 'angle',
value: function angle(value) {
if (typeof value === 'undefined') {
return this.value.angle;
} else {
this.value.angle = GradientString.parseAngle(value);
return this;
}
}
},
{
key: 'append',
value: function append(color, position) {
return this.insert(color, position, this.length);
}
},
{
key: 'reorder',
value: function reorder() {
if (this.length < 2) {
return;
}
this.value.stops = this.value.stops.sort(function(a, b) {
return a.position - b.position;
});
}
},
{
key: 'insert',
value: function insert(color, position, index) {
if (typeof index === 'undefined') {
index = this.current;
}
var stop = new ColorStop(color, position, this);
this.value.stops.splice(index, 0, stop);
this.length = this.length + 1;
this.current = index;
return stop;
}
},
{
key: 'getById',
value: function getById(id) {
if (this.length > 0) {
for (var i in this.value.stops) {
if (id === this.value.stops[i].id) {
return this.value.stops[i];
}
}
}
return false;
}
},
{
key: 'removeById',
value: function removeById(id) {
var index = this.getIndexById(id);
if (index) {
this.remove(index);
}
}
},
{
key: 'getIndexById',
value: function getIndexById(id) {
var index = 0;
for (var i in this.value.stops) {
if (id === this.value.stops[i].id) {
return index;
}
index++;
}
return false;
}
},
{
key: 'getCurrent',
value: function getCurrent() {
return this.value.stops[this.current];
}
},
{
key: 'setCurrentById',
value: function setCurrentById(id) {
var index = 0;
for (var i in this.value.stops) {
if (this.value.stops[i].id !== id) {
index++;
} else {
this.current = index;
}
}
}
},
{
key: 'get',
value: function get(index) {
if (typeof index === 'undefined') {
index = this.current;
}
if (index >= 0 && index < this.length) {
this.current = index;
return this.value.stops[index];
} else {
return false;
}
}
},
{
key: 'remove',
value: function remove(index) {
if (typeof index === 'undefined') {
index = this.current;
}
if (index >= 0 && index < this.length) {
this.value.stops.splice(index, 1);
this.length = this.length - 1;
this.current = index - 1;
}
}
},
{
key: 'empty',
value: function empty() {
this.value.stops = [];
this.length = 0;
this.current = 0;
}
},
{
key: 'reset',
value: function reset() {
this.value._angle = 0;
this.empty();
this._prefix = null;
this._type = 'LINEAR';
}
},
{
key: 'type',
value: function type(_type) {
if (
typeof _type === 'string' &&
(_type = _type.toUpperCase()) &&
typeof GradientTypes[_type] !== 'undefined'
) {
this._type = _type;
return this;
} else {
return this._type;
}
}
},
{
key: 'fromString',
value: function fromString(string) {
var _this2 = this;
this.reset();
var result = GradientString.parseString(string);
if (result) {
this._prefix = result.prefix;
this.type(result.type);
if (result.value) {
this.value.angle = GradientString.parseAngle(
result.value.angle,
this._prefix !== null
);
_jquery2.default.each(result.value.stops, function(i, stop) {
_this2.append(stop.color, stop.position);
});
}
}
}
},
{
key: 'toString',
value: function toString(prefix) {
if (prefix === true) {
prefix = getPrefix();
}
return GradientTypes[this.type()].to(this.value, this, prefix);
}
},
{
key: 'matchString',
value: function matchString(string) {
return GradientString.matchString(string);
}
},
{
key: 'toStringWithAngle',
value: function toStringWithAngle(angle, prefix) {
var value = _jquery2.default.extend(true, {}, this.value);
value.angle = GradientString.parseAngle(angle);
if (prefix === true) {
prefix = getPrefix();
}
return GradientTypes[this.type()].to(value, this, prefix);
}
},
{
key: 'getPrefixedStrings',
value: function getPrefixedStrings() {
var strings = [];
for (var i in this.options.prefixes) {
if (Object.hasOwnProperty.call(this.options.prefixes, i)) {
strings.push(this.toString(this.options.prefixes[i]));
}
}
return strings;
}
}
],
[
{
key: 'setDefaults',
value: function setDefaults(options) {
_jquery2.default.extend(
true,
DEFAULTS,
_jquery2.default.isPlainObject(options) && options
);
}
}
]
);
return AsGradient;
})();
var info = {
version: '0.3.3'
};
var OtherAsGradient = _jquery2.default.asGradient;
var jQueryAsGradient = function jQueryAsGradient() {
for (
var _len = arguments.length, args = Array(_len), _key = 0;
_key < _len;
_key++
) {
args[_key] = arguments[_key];
}
return new (Function.prototype.bind.apply(
AsGradient,
[null].concat(args)
))();
};
_jquery2.default.asGradient = jQueryAsGradient;
_jquery2.default.asGradient.Constructor = AsGradient;
_jquery2.default.extend(
_jquery2.default.asGradient,
{
setDefaults: AsGradient.setDefaults,
noConflict: function noConflict() {
_jquery2.default.asGradient = OtherAsGradient;
return jQueryAsGradient;
}
},
GradientString,
info
);
var main = _jquery2.default.asGradient;
exports.default = main;
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,144 @@
{
"_from": "jquery-asGradient@^0.3.2",
"_id": "jquery-asGradient@0.3.3",
"_inBundle": false,
"_integrity": "sha512-puoCFLfe98dwCCketwVsk/aBAEyOq0/BqW07PV71GvmwpKwYX7L0GC1THa2/tkMuST/2mmxHmGWJkxQDzNSiAA==",
"_location": "/jquery-asGradient",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "jquery-asGradient@^0.3.2",
"name": "jquery-asGradient",
"escapedName": "jquery-asGradient",
"rawSpec": "^0.3.2",
"saveSpec": null,
"fetchSpec": "^0.3.2"
},
"_requiredBy": [
"/jquery-asColorPicker"
],
"_resolved": "https://registry.npmjs.org/jquery-asGradient/-/jquery-asGradient-0.3.3.tgz",
"_shasum": "89e69eae0242b53c8fe08563ae7bf47aa624f4f3",
"_spec": "jquery-asGradient@^0.3.2",
"_where": "D:\\themeforest\\adminX\\adminX\\node_modules\\jquery-asColorPicker",
"author": {
"name": "amazingSurge",
"email": "amazingSurge@gmail.com",
"url": "amazingSurge.com"
},
"bugs": {
"url": "https://github.com/amazingSurge/jquery-asGradient/issues"
},
"bundleDependencies": false,
"dependencies": {
"jquery": ">=2.2.0",
"jquery-asColor": "^0.3.3"
},
"deprecated": false,
"description": "A jquery plugin used to manipulate css image gradient.",
"devDependencies": {
"assets-manager": "*",
"babel-core": "*",
"babel-eslint": "*",
"babel-istanbul": "*",
"babel-plugin-transform-es2015-modules-umd": "*",
"babel-preset-es2015": "*",
"babel-preset-es2015-rollup": "*",
"babelify": "*",
"browserify": "*",
"browserify-babel-istanbul": "*",
"chai": "*",
"del": "*",
"graceful-fs": "*",
"gulp": "github:gulpjs/gulp#4.0",
"gulp-autoprefixer": "*",
"gulp-babel": "*",
"gulp-changed": "*",
"gulp-eslint": "*",
"gulp-extname": "*",
"gulp-filter": "*",
"gulp-header": "*",
"gulp-iconfont-css": "*",
"gulp-if": "*",
"gulp-nf-prettier": "*",
"gulp-notify": "*",
"gulp-plumber": "*",
"gulp-rename": "*",
"gulp-replace": "*",
"gulp-rollup": "*",
"gulp-size": "*",
"gulp-sourcemaps": "*",
"gulp-uglify": "*",
"gulp-util": "*",
"gulp-zip": "*",
"inquirer": "*",
"isparta": "*",
"karma": "*",
"karma-babel-preprocessor": "*",
"karma-browserify": "*",
"karma-chrome-launcher": "*",
"karma-coverage": "*",
"karma-firefox-launcher": "*",
"karma-mocha": "*",
"karma-mocha-reporter": "*",
"karma-phantomjs-launcher": "*",
"karma-sinon-chai": "*",
"merge-stream": "*",
"mkdirp": "*",
"mocha": "*",
"node-notifier": "*",
"path-exists": "*",
"phantomjs-prebuilt": "*",
"prettier": "*",
"release-it": "*",
"rollup-plugin-babel": "*",
"semver": "*",
"sinon": "*",
"sinon-chai": "*",
"through2": "*",
"yargs": "*"
},
"engines": {
"node": ">= 6.2.2",
"npm": ">= 3"
},
"files": [
"dist",
"src"
],
"homepage": "https://github.com/amazingSurge/jquery-asGradient",
"keywords": [
"jquery",
"jquery-plugin",
"ecosystem:jquery",
"ui",
"es6",
"Gradient"
],
"license": "LGPL-3.0",
"licenses": [
{
"type": "LGPL-3.0",
"url": "https://github.com/amazingSurge/jquery-asGradient/blob/master/LICENSE"
}
],
"main": "dist/jquery-asGradient.js",
"module": "dist/jquery-asGradient.es.js",
"name": "jquery-asGradient",
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/amazingSurge/jquery-asGradient.git"
},
"scripts": {
"build": "npm run prestart && gulp build",
"deploy": "gulp deploy",
"deploy:prepare": "gulp deploy:prepare",
"prestart": "npm install",
"release": "gulp release",
"start": "gulp serve",
"test": "gulp test"
},
"title": "jQuery asGradient",
"version": "0.3.3"
}

View File

@@ -0,0 +1,223 @@
import $ from 'jquery';
import DEFAULTS from './defaults';
import * as util from './util';
import GradientString from './gradientString';
import ColorStop from './colorStop';
import GradientTypes from './gradientTypes';
class AsGradient {
constructor(string, options) {
if (typeof string === 'object' && typeof options === 'undefined') {
options = string;
string = undefined;
}
this.value = {
angle: 0,
stops: []
};
this.options = $.extend(true, {}, DEFAULTS, options);
this._type = 'LINEAR';
this._prefix = null;
this.length = this.value.stops.length;
this.current = 0;
this._stopIdCount = 0;
this.init(string);
}
init(string) {
if (string) {
this.fromString(string);
}
}
val(value) {
if (typeof value === 'undefined') {
return this.toString();
} else {
this.fromString(value);
return this;
}
}
angle(value) {
if (typeof value === 'undefined') {
return this.value.angle;
} else {
this.value.angle = GradientString.parseAngle(value);
return this;
}
}
append(color, position) {
return this.insert(color, position, this.length);
}
reorder() {
if(this.length < 2){
return;
}
this.value.stops = this.value.stops.sort((a, b) => a.position - b.position);
}
insert(color, position, index) {
if (typeof index === 'undefined') {
index = this.current;
}
const stop = new ColorStop(color, position, this);
this.value.stops.splice(index, 0, stop);
this.length = this.length + 1;
this.current = index;
return stop;
}
getById(id) {
if(this.length > 0){
for(const i in this.value.stops){
if(id === this.value.stops[i].id){
return this.value.stops[i];
}
}
}
return false;
}
removeById(id) {
const index = this.getIndexById(id);
if(index){
this.remove(index);
}
}
getIndexById(id) {
let index = 0;
for(const i in this.value.stops){
if(id === this.value.stops[i].id){
return index;
}
index ++;
}
return false;
}
getCurrent() {
return this.value.stops[this.current];
}
setCurrentById(id) {
let index = 0;
for(const i in this.value.stops){
if(this.value.stops[i].id !== id){
index ++;
} else {
this.current = index;
}
}
}
get(index) {
if (typeof index === 'undefined') {
index = this.current;
}
if (index >= 0 && index < this.length) {
this.current = index;
return this.value.stops[index];
} else {
return false;
}
}
remove(index) {
if (typeof index === 'undefined') {
index = this.current;
}
if (index >= 0 && index < this.length) {
this.value.stops.splice(index, 1);
this.length = this.length - 1;
this.current = index - 1;
}
}
empty() {
this.value.stops = [];
this.length = 0;
this.current = 0;
}
reset() {
this.value._angle = 0;
this.empty();
this._prefix = null;
this._type = 'LINEAR';
}
type(type) {
if (typeof type === 'string' && (type = type.toUpperCase()) && typeof GradientTypes[type] !== 'undefined') {
this._type = type;
return this;
} else {
return this._type;
}
}
fromString(string) {
this.reset();
const result = GradientString.parseString(string);
if (result) {
this._prefix = result.prefix;
this.type(result.type);
if (result.value) {
this.value.angle = GradientString.parseAngle(result.value.angle, this._prefix !== null);
$.each(result.value.stops, (i, stop) => {
this.append(stop.color, stop.position);
});
}
}
}
toString(prefix) {
if(prefix === true){
prefix = util.getPrefix();
}
return GradientTypes[this.type()].to(this.value, this, prefix);
}
matchString(string) {
return GradientString.matchString(string);
}
toStringWithAngle(angle, prefix) {
const value = $.extend(true, {}, this.value);
value.angle = GradientString.parseAngle(angle);
if(prefix === true){
prefix = util.getPrefix();
}
return GradientTypes[this.type()].to(value, this, prefix);
}
getPrefixedStrings() {
const strings = [];
for (let i in this.options.prefixes) {
if(Object.hasOwnProperty.call(this.options.prefixes, i)){
strings.push(this.toString(this.options.prefixes[i]));
}
}
return strings;
}
static setDefaults(options) {
$.extend(true, DEFAULTS, $.isPlainObject(options) && options);
}
}
export default AsGradient;

View File

@@ -0,0 +1,27 @@
import Color from 'jquery-asColor';
import GradientString from './gradientString';
export default class ColorStop {
constructor(color, position, gradient) {
this.color = Color(color, gradient.options.color);
this.position = GradientString.parsePosition(position);
this.id = ++gradient._stopIdCount;
this.gradient = gradient;
}
setPosition(string) {
const position = GradientString.parsePosition(string);
if(this.position !== position){
this.position = position;
this.gradient.reorder();
}
}
setColor(string) {
this.color.fromString(string);
}
remove() {
this.gradient.removeById(this.id);
}
}

View File

@@ -0,0 +1,21 @@
export default {
prefixes: ['-webkit-', '-moz-', '-ms-', '-o-'],
forceStandard: true,
angleUseKeyword: true,
emptyString: '',
degradationFormat: false,
cleanPosition: true,
color: {
format: false, // rgb, rgba, hsl, hsla, hex
hexUseName: false,
reduceAlpha: true,
shortenHex: true,
zeroAlphaAsTransparent: false,
invalidValue: {
r: 0,
g: 0,
b: 0,
a: 1
}
}
};

View File

@@ -0,0 +1,234 @@
import $ from 'jquery';
import * as util from './util';
import keywordAngleMap from './keywordAngleMap';
const angleKeywordMap = util.flip(keywordAngleMap);
const RegExpStrings = (() => {
const color = /(?:rgba|rgb|hsla|hsl)\s*\([\s\d\.,%]+\)|#[a-z0-9]{3,6}|[a-z]+/i;
const position = /\d{1,3}%/i;
const angle = /(?:to ){0,1}(?:(?:top|left|right|bottom)\s*){1,2}|\d+deg/i;
const stop = new RegExp(`(${color.source})\\s*(${position.source}){0,1}`, 'i');
const stops = new RegExp(stop.source, 'gi');
const parameters = new RegExp(`(?:(${angle.source})){0,1}\\s*,{0,1}\\s*(.*?)\\s*`, 'i');
const full = new RegExp(`^(-webkit-|-moz-|-ms-|-o-){0,1}(linear|radial|repeating-linear)-gradient\\s*\\(\\s*(${parameters.source})\\s*\\)$`, 'i');
return {
FULL: full,
ANGLE: angle,
COLOR: color,
POSITION: position,
STOP: stop,
STOPS: stops,
PARAMETERS: new RegExp(`^${parameters.source}$`, 'i')
};
})();
export default {
matchString: function(string) {
const matched = this.parseString(string);
if(matched && matched.value && matched.value.stops && matched.value.stops.length > 1){
return true;
}
return false;
},
parseString: function(string) {
string = $.trim(string);
let matched;
if ((matched = RegExpStrings.FULL.exec(string)) !== null) {
let value = this.parseParameters(matched[3]);
return {
prefix: (typeof matched[1] === 'undefined') ? null : matched[1],
type: matched[2],
value: value
};
} else {
return false;
}
},
parseParameters: function(string) {
let matched;
if ((matched = RegExpStrings.PARAMETERS.exec(string)) !== null) {
let stops = this.parseStops(matched[2]);
return {
angle: (typeof matched[1] === 'undefined') ? 0 : matched[1],
stops: stops
};
} else {
return false;
}
},
parseStops: function(string) {
let matched;
const result = [];
if ((matched = string.match(RegExpStrings.STOPS)) !== null) {
$.each(matched, (i, item) => {
const stop = this.parseStop(item);
if (stop) {
result.push(stop);
}
});
return result;
} else {
return false;
}
},
formatStops: function(stops, cleanPosition) {
let stop;
const output = [];
let positions = [];
const colors = [];
let position;
for (let i = 0; i < stops.length; i++) {
stop = stops[i];
if (typeof stop.position === 'undefined' || stop.position === null) {
if (i === 0) {
position = 0;
} else if (i === stops.length - 1) {
position = 1;
} else {
position = undefined;
}
} else {
position = stop.position;
}
positions.push(position);
colors.push(stop.color.toString());
}
positions = ((data => {
let start = null;
let average;
for (let i = 0; i < data.length; i++) {
if (isNaN(data[i])) {
if (start === null) {
start = i;
continue;
}
} else if (start) {
average = (data[i] - data[start - 1]) / (i - start + 1);
for (let j = start; j < i; j++) {
data[j] = data[start - 1] + (j - start + 1) * average;
}
start = null;
}
}
return data;
}))(positions);
for (let x = 0; x < stops.length; x++) {
if (cleanPosition && ((x === 0 && positions[x] === 0) || (x === stops.length - 1 && positions[x] === 1))) {
position = '';
} else {
position = ` ${this.formatPosition(positions[x])}`;
}
output.push(colors[x] + position);
}
return output.join(', ');
},
parseStop: function(string) {
let matched;
if ((matched = RegExpStrings.STOP.exec(string)) !== null) {
let position = this.parsePosition(matched[2]);
return {
color: matched[1],
position: position
};
} else {
return false;
}
},
parsePosition: function(string) {
if (typeof string === 'string' && string.substr(-1) === '%') {
string = parseFloat(string.slice(0, -1) / 100);
}
if(typeof string !== 'undefined' && string !== null) {
return parseFloat(string, 10);
} else {
return null;
}
},
formatPosition: function(value) {
return `${parseInt(value * 100, 10)}%`;
},
parseAngle: function(string, notStandard) {
if (typeof string === 'string' && string.includes('deg')) {
string = string.replace('deg', '');
}
if (!isNaN(string)) {
if (notStandard) {
string = this.fixOldAngle(string);
}
}
if (typeof string === 'string') {
const directions = string.split(' ');
const filtered = [];
for (const i in directions) {
if (util.isDirection(directions[i])) {
filtered.push(directions[i].toLowerCase());
}
}
let keyword = filtered.join(' ');
if (!string.includes('to ')) {
keyword = util.reverseDirection(keyword);
}
keyword = `to ${keyword}`;
if (keywordAngleMap.hasOwnProperty(keyword)) {
string = keywordAngleMap[keyword];
}
}
let value = parseFloat(string, 10);
if (value > 360) {
value %= 360;
} else if (value < 0) {
value %= -360;
if (value !== 0) {
value += 360;
}
}
return value;
},
fixOldAngle: function(value) {
value = parseFloat(value);
value = Math.abs(450 - value) % 360;
value = parseFloat(value.toFixed(3));
return value;
},
formatAngle: function(value, notStandard, useKeyword) {
value = parseInt(value, 10);
if (useKeyword && angleKeywordMap.hasOwnProperty(value)) {
value = angleKeywordMap[value];
if (notStandard) {
value = util.reverseDirection(value.substr(3));
}
} else {
if (notStandard) {
value = this.fixOldAngle(value);
}
value = `${value}deg`;
}
return value;
}
}

View File

@@ -0,0 +1,44 @@
import $ from 'jquery';
import GradientString from './gradientString';
export default {
LINEAR: {
parse(result) {
return {
r: (result[1].substr(-1) === '%') ? parseInt(result[1].slice(0, -1) * 2.55, 10) : parseInt(result[1], 10),
g: (result[2].substr(-1) === '%') ? parseInt(result[2].slice(0, -1) * 2.55, 10) : parseInt(result[2], 10),
b: (result[3].substr(-1) === '%') ? parseInt(result[3].slice(0, -1) * 2.55, 10) : parseInt(result[3], 10),
a: 1
};
},
to(gradient, instance, prefix) {
if (gradient.stops.length === 0) {
return instance.options.emptyString;
}
if (gradient.stops.length === 1) {
return gradient.stops[0].color.to(instance.options.degradationFormat);
}
let standard = instance.options.forceStandard;
let _prefix = instance._prefix;
if (!_prefix) {
standard = true;
}
if (prefix && -1 !== $.inArray(prefix, instance.options.prefixes)) {
standard = false;
_prefix = prefix;
}
const angle = GradientString.formatAngle(gradient.angle, !standard, instance.options.angleUseKeyword);
const stops = GradientString.formatStops(gradient.stops, instance.options.cleanPosition);
const output = `linear-gradient(${angle}, ${stops})`;
if (standard) {
return output;
} else {
return _prefix + output;
}
}
}
};

View File

@@ -0,0 +1,3 @@
export default {
version:'0.3.3'
};

View File

@@ -0,0 +1,14 @@
export default {
'to top': 0,
'to right': 90,
'to bottom': 180,
'to left': 270,
'to right top': 45,
'to top right': 45,
'to bottom right': 135,
'to right bottom': 135,
'to left bottom': 225,
'to bottom left': 225,
'to top left': 315,
'to left top': 315
};

View File

@@ -0,0 +1,23 @@
import $ from 'jquery';
import AsGradient from './asGradient';
import info from './info';
import GradientString from './gradientString';
const OtherAsGradient = $.asGradient;
const jQueryAsGradient = function(...args) {
return new AsGradient(...args);
}
$.asGradient = jQueryAsGradient;
$.asGradient.Constructor = AsGradient;
$.extend($.asGradient, {
setDefaults: AsGradient.setDefaults,
noConflict: function() {
$.asGradient = OtherAsGradient;
return jQueryAsGradient;
}
}, GradientString, info);
export default $.asGradient;

View File

View File

@@ -0,0 +1,62 @@
/* eslint no-extend-native: "off" */
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
'use strict';
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
}
return this.indexOf(search, start) !== -1;
};
}
export function getPrefix() {
const ua = window.navigator.userAgent;
let prefix = '';
if (/MSIE/g.test(ua)) {
prefix = '-ms-';
} else if (/Firefox/g.test(ua)) {
prefix = '-moz-';
} else if (/(WebKit)/i.test(ua)) {
prefix = '-webkit-';
} else if (/Opera/g.test(ua)) {
prefix = '-o-';
}
return prefix;
}
export function flip(o) {
const flipped = {};
for (const i in o) {
if (o.hasOwnProperty(i)) {
flipped[o[i]] = i;
}
}
return flipped;
}
export function reverseDirection(direction) {
const mapping = {
'top': 'bottom',
'right': 'left',
'bottom': 'top',
'left': 'right',
'right top': 'left bottom',
'top right': 'bottom left',
'bottom right': 'top left',
'right bottom': 'left top',
'left bottom': 'right top',
'bottom left': 'top right',
'top left': 'bottom right',
'left top': 'right bottom'
};
return mapping.hasOwnProperty(direction) ? mapping[direction] : direction;
}
export function isDirection(n) {
const reg = /^(top|left|right|bottom)$/i;
return reg.test(n);
}