Skip to content

Commit b4d67cc

Browse files
committed
first commit
0 parents  commit b4d67cc

8 files changed

+442
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DS_Store

LICENSE.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Copyright 2012 John Krauss. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions
5+
are met:
6+
7+
1. Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above
11+
copyright notice, this list of conditions and the following
12+
disclaimer in the documentation and/or other materials provided
13+
with the distribution.
14+
15+
THIS SOFTWARE IS PROVIDED BY JOHN KRAUSS ''AS IS'' AND ANY EXPRESS
16+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
ARE DISCLAIMED. IN NO EVENT SHALL JOHN KRAUSS OR CONTRIBUTORS BE
19+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22+
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26+
DAMAGE.
27+
28+
The views and conclusions contained in the software and
29+
documentation are those of the authors and should not be
30+
interpreted as representing official policies, either expressed or
31+
implied, of John Krauss.

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all:
2+
uglifyjs jquery-download.js > jquery-download.min.js
3+
4+
clean:
5+
rm jquery-download.min.js

README.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# jquery-download
2+
3+
This jQuery plugin uses the `data:` URI to download the DOM of
4+
arbitrary elements into files. One file will be downloaded for each
5+
selected element. Since the DOM is captured, any javascript-generated
6+
content will be included in the download.
7+
8+
[See it in action.](http://talos.github.com/jquery-download/demo.html)
9+
10+
### Usage
11+
12+
`failCallback`: (Optional) The browser may not support the data: URI.
13+
If this is the case, this function will be called once with the text
14+
ofeach element not downloaded.
15+
16+
```javascript
17+
$(selector).download(failCallback);
18+
```
19+
20+
There is also a utility function you can use to check whether the
21+
browser supports the data URI. This will return `true` if there is
22+
support, and `false` otherwise.
23+
24+
```javascript
25+
$().download('support');
26+
```
27+
28+
### Examples
29+
30+
This will download into a browser-named file the contents of the
31+
current page view:
32+
33+
$('html').download();
34+
35+
This would download every svg on the page into a separate file:
36+
37+
$('svg').download();
38+
39+
This takes advantage of `failCallback` in case the browser doesn't
40+
support the `data:` uri.
41+
42+
var failCallback = function(text) {
43+
alert("Could not download " + text);
44+
};
45+
$('#elem').download(failCallback);
46+
47+
### Links
48+
49+
Fork it from
50+
51+
http://www.github.com/talos/jquery-download
52+
53+
CDN it at
54+
55+
http://talos.github.com/jquery-download/jquery-download.js
56+
57+
http://talos.github.com/jquery-download/jquery-download.min.js
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
This jQuery plugin arbitrarly CSS transforms elements to a specific
69+
pixel size. All the contents of the element will scale with it!
70+
71+
[See it in action.](http://talos.github.com/jquery-rescale/demo.html)
72+
73+
### Usage
74+
75+
`w` The width to scale to. Required.
76+
77+
`h` The height to scale to. Required.
78+
79+
`distort` Whether to allow differing x and y scale
80+
factors. Is true by default, meaning the element could be
81+
distorted. If false, the smallest of the two scales will be
82+
used for both axes, so that neither w or h is ever exceeded.
83+
84+
`scaleDirection` If negative, only scaling down will be
85+
allowed. If greater than 0, only scaling up will be allowed.
86+
Otherwise, either direction is allowed. Defaults to 0.
87+
88+
```javascript
89+
$(selector).rescale(w, h, distort, scaleDirection);
90+
```
91+
92+
### Examples
93+
94+
```javascript
95+
$('.rescale').rescale(100, 100);
96+
```
97+
98+
Would scale all elements with the class 'scale' to 100 by 100
99+
pixels. If you don't want the elements to be distorted, pass
100+
'false' as the distort argument:
101+
102+
```javascript
103+
$('.rescale').rescale(100, 100, false);
104+
```
105+
106+
Each element will keep its original aspect ratio, but no dimension
107+
will exceed 100 pixels.
108+
109+
If you want to only scale elements up, or only scale them down, you
110+
can pass a fourth argument. If it is negative, elements will only
111+
be scaled down:
112+
113+
```javascript
114+
$('.rescale').rescale(100, 100, false, -1);
115+
```
116+
117+
Any elements with both dimensions already smaller than 100 pixels
118+
will be unmodified.
119+
120+
To only scale elements up, pass a positive value:
121+
122+
```javascript
123+
$('.rescale').rescale(100, 100, false, 1);
124+
```
125+
126+
Now, elements exceeding 100px * 100px will be unmodified, but any
127+
smaller than that will be scaled up.
128+
129+
Play around with demo.html to see it in action.
130+
131+
### Links
132+
133+
Fork it from:
134+
135+
http://www.github.com/talos/jquery-rescale
136+
137+
CDN it from:
138+
139+
http://talos.github.com/jquery-rescale/jquery-rescale.js
140+
141+
http://talos.github.com/jquery-rescale/jquery-rescale.min.js

demo.html

+99
Large diffs are not rendered by default.

jquery-download.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
Copyright 2012 John Krauss. All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions
6+
are met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY JOHN KRAUSS ''AS IS'' AND ANY EXPRESS
17+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
ARE DISCLAIMED. IN NO EVENT SHALL JOHN KRAUSS OR CONTRIBUTORS BE
20+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23+
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27+
DAMAGE.
28+
29+
The views and conclusions contained in the software and
30+
documentation are those of the authors and should not be
31+
interpreted as representing official policies, either expressed or
32+
implied, of John Krauss.
33+
**/
34+
(function($) {
35+
/**
36+
Download some text to the client computer. Only works in
37+
browsers that support the 'data:' scheme.
38+
39+
@param text The text to download.
40+
**/
41+
var download = function(text) {
42+
window.location.href =
43+
'data:application/x-download;charset=utf-8,' +
44+
encodeURIComponent(text);
45+
},
46+
47+
// Test to see if data URI is supported
48+
// Thanks to http://weston.ruter.net/2009/05/07/detecting-support-for-data-uris
49+
data = new Image(),
50+
supported = false;
51+
data.onload = data.onerror = function() {
52+
supported = (this.width === 1 && this.height === 1) ? true : false;
53+
};
54+
data.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
55+
56+
/**
57+
Download the HTML of selected elements as text to the client
58+
computer, one file per element. Only works in browsers that
59+
support the 'data:' scheme.
60+
**/
61+
$.fn.download = function(arg) {
62+
if(arg === 'support') {
63+
return supported;
64+
} else {
65+
var texts = [],
66+
interval;
67+
68+
$.each(this, function(i, el) {
69+
var $el = $(el),
70+
$container = $('<div />'),
71+
text;
72+
73+
$el.clone().appendTo($container);
74+
text = $container.html();
75+
76+
if(supported === true) {
77+
texts.push(text);
78+
} else {
79+
// If user supplied failCallback, call it.
80+
if($.isFunction(arg)) {
81+
arg(text);
82+
}
83+
}
84+
});
85+
86+
// Interval to shuffle through window.location.href
87+
interval = setInterval(function() {
88+
if(texts.length > 0) {
89+
download(texts.pop());
90+
} else {
91+
clearInterval(interval);
92+
}
93+
}, 100);
94+
95+
return this;
96+
}
97+
};
98+
})(jQuery);

jquery-download.min.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
Copyright 2012 John Krauss. All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions
6+
are met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY JOHN KRAUSS ''AS IS'' AND ANY EXPRESS
17+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
ARE DISCLAIMED. IN NO EVENT SHALL JOHN KRAUSS OR CONTRIBUTORS BE
20+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22+
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23+
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27+
DAMAGE.
28+
29+
The views and conclusions contained in the software and
30+
documentation are those of the authors and should not be
31+
interpreted as representing official policies, either expressed or
32+
implied, of John Krauss.
33+
**/(function(a){var b=function(a){window.location.href="data:application/x-download;charset=utf-8,"+encodeURIComponent(a)},c=new Image,d=!1;c.onload=c.onerror=function(){d=this.width===1&&this.height===1?!0:!1},c.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==",a.fn.download=function(c){if(c==="support")return d;var e=[],f;return a.each(this,function(b,f){var g=a(f),h=a("<div />"),i;g.clone().appendTo(h),i=h.html(),d===!0?e.push(i):a.isFunction(c)&&c(i)}),f=setInterval(function(){e.length>0?b(e.pop()):clearInterval(f)},100),this}})(jQuery);

package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "jquery-download",
3+
"version": "0.1.0",
4+
"title": "jQuery-download",
5+
"author": {
6+
"name": "John Krauss",
7+
"url": "https://github.com/talos"
8+
},
9+
"licenses": [
10+
{
11+
"type": "BSD",
12+
"url": "LICENSE.txt"
13+
}
14+
],
15+
"dependencies": {
16+
"jquery": "1"
17+
},
18+
"description": "This plugin uses the data: uri to allow users to download arbitrary pieces of DOM, including SVGs.",
19+
"keywords": [
20+
"download",
21+
"data"
22+
],
23+
"homepage": "https://github.com/talos/jquery-download",
24+
"maintainers": [
25+
{
26+
"name": "John Krauss",
27+
"url": "https://github.com/talos"
28+
}
29+
],
30+
"files": [
31+
"jquery-download.js",
32+
"jquery-download.min.js"
33+
]
34+
}

0 commit comments

Comments
 (0)