gorg_slack_chat 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +37 -0
- data/app/assets/config/gorg_slack_chat_manifest.js +0 -0
- data/app/assets/images/gorg_slack_chat/gadzorg.png +0 -0
- data/app/assets/images/gorg_slack_chat/user-icon-small.jpg +0 -0
- data/app/assets/javascripts/gorg_slack_chat/caller.js +57 -0
- data/app/assets/stylesheets/gorg_slack_chat/custom-slack-chat.css +43 -0
- data/app/helpers/gorg_slack_chat_helper.rb +19 -0
- data/app/views/shared/_gorg_slack_chat.html.erb +15 -0
- data/app/views/shared/_tags_for_gorg_slack_chat.html.erb +11 -0
- data/config/routes.rb +2 -0
- data/lib/gorg_slack_chat/engine.rb +19 -0
- data/lib/gorg_slack_chat/version.rb +3 -0
- data/lib/gorg_slack_chat.rb +8 -0
- data/lib/tasks/gorg_slack_chat_tasks.rake +4 -0
- data/vendor/assets/javascripts/identicon.js +144 -0
- data/vendor/assets/javascripts/pnglib.js +214 -0
- data/vendor/assets/javascripts/slack-chat.js +518 -0
- data/vendor/assets/javascripts/sweetalert.min.js +1 -0
- data/vendor/assets/stylesheets/slack-chat.css +186 -0
- data/vendor/assets/stylesheets/sweetalert.css +935 -0
- metadata +122 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1023c9a442e002ff88cd85bb628d4666c1847b4a
|
4
|
+
data.tar.gz: 82e6e21dc9150fd00ce5a16c3c7c246506f247b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6d9d2d2522f63753c76f141cf064bf4701d0f1d026b259b09f5468acb4138207921716a9fc6ad5f3d3d16b939d2d99c2a27a116ed669bab78f399f6d1cb1157
|
7
|
+
data.tar.gz: 56e8892df981519bca39bde4939a7f465cc4c6fe94a0f7aa41c84daaed7f11d461fcc780be2324a47cba44ac9cb5e9268bd404dee4738f322222f94a764b0fe5
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Alexandre Narbonne
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# GorgSlackChat
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'gorg_slack_chat'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install gorg_slack_chat
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'GorgSlackChat'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task default: :test
|
File without changes
|
Binary file
|
Binary file
|
@@ -0,0 +1,57 @@
|
|
1
|
+
function attachSlackChat(emnt) {
|
2
|
+
var slackChatOptions;
|
3
|
+
emnt.slackChat('destroy');
|
4
|
+
$('[data-toggle=tooltip]').tooltip();
|
5
|
+
emnt.slackChat('destroy');
|
6
|
+
emnt.hide();
|
7
|
+
slackChatOptions = {
|
8
|
+
apiToken: gon.global.slack_chat_token,
|
9
|
+
channelId: gon.global.slack_chat_channel_id,
|
10
|
+
user: user_name,
|
11
|
+
userLink: user_link,
|
12
|
+
userId: user_id,
|
13
|
+
msgUserId: user_id,
|
14
|
+
//userImg: "data: image/png;base64,"+ (new Identicon('d3b07384d113edec49eaa6238ad5ff01', 40).toString()),
|
15
|
+
defaultUserImg: gon.global.slack_chat_user_img,
|
16
|
+
defaultSysImg: gon.global.slack_chat_default_sys_img,
|
17
|
+
defaultSysUser: 'Equipe Gadz.org',
|
18
|
+
chatBoxHeader: gon.global.slack_chat_chat_box_header,
|
19
|
+
botUser: 'ChatSupport - '+gon.global.slack_chat_app_name,
|
20
|
+
elementToDisable: $('.message-box'),
|
21
|
+
disableIfAway: false,
|
22
|
+
webCache: false,
|
23
|
+
debug: false,
|
24
|
+
privateChannel: false,
|
25
|
+
heightOffset: 150,
|
26
|
+
messageFetchCount: 25
|
27
|
+
};
|
28
|
+
emnt.slackChat(slackChatOptions);
|
29
|
+
return emnt.show();
|
30
|
+
}
|
31
|
+
|
32
|
+
$(document).ready(function() {
|
33
|
+
|
34
|
+
var button=$('.message-box');
|
35
|
+
|
36
|
+
var handler=function(e) {
|
37
|
+
e.preventDefault();
|
38
|
+
e.stopPropagation();
|
39
|
+
|
40
|
+
swal({
|
41
|
+
title: '',
|
42
|
+
text: "Cette conversation est publique, tous les utilisateurs de Gadz.org peuvent en voir le contenu. \n\n Des bénévoles pourront te répondre s'ils sont disponibles, mais si tu n'as pas de réponse dans les 10 minutes, tu peux nous envoyer un email à support@gadz.org.",
|
43
|
+
type: "warning",
|
44
|
+
confirmButtonText: "C'est compris !"
|
45
|
+
},
|
46
|
+
function(){
|
47
|
+
button.unbind('click',handler);
|
48
|
+
attachSlackChat(button);
|
49
|
+
button.trigger( "click" );
|
50
|
+
});
|
51
|
+
};
|
52
|
+
|
53
|
+
button.on('click',handler);
|
54
|
+
|
55
|
+
|
56
|
+
});
|
57
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
.message-box {
|
2
|
+
position: fixed;
|
3
|
+
bottom: 50px;
|
4
|
+
right: 50px;
|
5
|
+
font-size: 28px;
|
6
|
+
color: #fff;
|
7
|
+
background: #e22b2f;
|
8
|
+
border-radius: 50%;
|
9
|
+
padding: 5px;
|
10
|
+
z-index: 80000;
|
11
|
+
height: 50px;
|
12
|
+
width: 50px;
|
13
|
+
text-align: center;
|
14
|
+
}
|
15
|
+
|
16
|
+
.message-box:hover {
|
17
|
+
background: #BD3235;
|
18
|
+
}
|
19
|
+
|
20
|
+
.message-box > a,
|
21
|
+
.message-box > a:active,
|
22
|
+
.message-box > a:focus,
|
23
|
+
.message-box > a:hover {
|
24
|
+
color: #fff;
|
25
|
+
text-decoration: none;
|
26
|
+
}
|
27
|
+
|
28
|
+
.message-box > a .slack-message-count {
|
29
|
+
position: absolute;
|
30
|
+
top: 0;
|
31
|
+
right: -5px;
|
32
|
+
display: none;
|
33
|
+
}
|
34
|
+
|
35
|
+
@media screen and (max-width: 768px) {
|
36
|
+
.message-box {
|
37
|
+
bottom: 25px;
|
38
|
+
right: 25px;
|
39
|
+
font-size: 20px;
|
40
|
+
height: 40px;
|
41
|
+
width: 40px;
|
42
|
+
}
|
43
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module GorgSlackChatHelper
|
2
|
+
|
3
|
+
def tags_for_gorg_slack_chat
|
4
|
+
render partial:'shared/tags_for_gorg_slack_chat'
|
5
|
+
end
|
6
|
+
|
7
|
+
def gorg_slack_chat
|
8
|
+
session[:foo] = "bar" #Initialize session if not set
|
9
|
+
|
10
|
+
opts= {
|
11
|
+
user_name: "Utilisateur Anonyme - " + session.id[0..4].upcase,
|
12
|
+
user_id: "",
|
13
|
+
user_link: ""
|
14
|
+
}
|
15
|
+
|
16
|
+
render partial:'shared/gorg_slack_chat', :locals => opts
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<div class="message-box" data-toggle="tooltip" title="Yo">
|
2
|
+
<a href="javascript:void(0);" class="message-box-icon">
|
3
|
+
?
|
4
|
+
<div class="badge badge-danger slack-message-count"></div>
|
5
|
+
</a>
|
6
|
+
</div>
|
7
|
+
|
8
|
+
<script>
|
9
|
+
var user_name = "<%= user_name %>";
|
10
|
+
var user_id = "<%= user_id %>";
|
11
|
+
var user_link = "<%= user_link %>";
|
12
|
+
</script>
|
13
|
+
|
14
|
+
|
15
|
+
<%= javascript_include_tag 'gorg_slack_chat/caller.js' %>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<%= Gon::Base.render_data %>
|
2
|
+
|
3
|
+
<%= javascript_include_tag 'pnglib.js'%>
|
4
|
+
<%= javascript_include_tag 'identicon.js'%>
|
5
|
+
<%= javascript_include_tag 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js' %>
|
6
|
+
<%= javascript_include_tag 'sweetalert.min.js' %>
|
7
|
+
<%= stylesheet_link_tag 'sweetalert.css' %>
|
8
|
+
|
9
|
+
<%= javascript_include_tag 'slack-chat.js' %>
|
10
|
+
<%= stylesheet_link_tag 'slack-chat.css' %>
|
11
|
+
<%= stylesheet_link_tag 'gorg_slack_chat/custom-slack-chat.css' %>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module GorgSlackChat
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
initializer "gorg_slack_chat.assets.precompile" do |app|
|
4
|
+
app.config.assets.precompile += %w(gorg_slack_chat/caller.js slack-chat.js gorg_slack_chat/custom-slack-chat.css slack-chat.css identicon.js pnglib.js sweetalert.css sweetalert.min.js)
|
5
|
+
end
|
6
|
+
|
7
|
+
initializer "gorg_slack_chat.gon" do |app|
|
8
|
+
require 'gon'
|
9
|
+
|
10
|
+
Gon.global.slack_chat_token = GorgSlackChat.token
|
11
|
+
Gon.global.slack_chat_channel_id = GorgSlackChat.channel_id
|
12
|
+
Gon.global.slack_chat_app_name = GorgSlackChat.app_name
|
13
|
+
Gon.global.slack_chat_chat_box_header = "Besoin d'aide ? Demande à Gadz.org"
|
14
|
+
Gon.global.slack_chat_default_sys_img = "/assets/gorg_slack_chat/gadzorg.png"
|
15
|
+
Gon.global.slack_chat_user_img = "/assets/gorg_slack_chat/user-icon-small.jpg"
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
/**
|
2
|
+
* Identicon.js v1.3.0
|
3
|
+
* http://github.com/stewartlord/identicon.js
|
4
|
+
*
|
5
|
+
* Requires PNGLib
|
6
|
+
* http://www.xarg.org/download/pnglib.js
|
7
|
+
*
|
8
|
+
* Copyright 2013, Stewart Lord
|
9
|
+
* Released under the BSD license
|
10
|
+
* http://www.opensource.org/licenses/bsd-license.php
|
11
|
+
*/
|
12
|
+
|
13
|
+
(function() {
|
14
|
+
var PNGlib;
|
15
|
+
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
16
|
+
PNGlib = require('./pnglib');
|
17
|
+
} else {
|
18
|
+
PNGlib = window.PNGlib;
|
19
|
+
}
|
20
|
+
|
21
|
+
var Identicon = function(hash, options){
|
22
|
+
this.defaults = {
|
23
|
+
background: [240, 240, 240, 255],
|
24
|
+
hash: this.createHashFromString((new Date()).toISOString()),
|
25
|
+
margin: 0.08,
|
26
|
+
size: 64
|
27
|
+
};
|
28
|
+
|
29
|
+
this.options = typeof(options) === 'object' ? options : this.defaults;
|
30
|
+
|
31
|
+
// backward compatibility with old constructor (hash, size, margin)
|
32
|
+
if (arguments[1] && typeof(arguments[1]) === 'number') { this.options.size = arguments[1]; }
|
33
|
+
if (arguments[2]) { this.options.margin = arguments[2]; }
|
34
|
+
|
35
|
+
this.hash = hash || this.defaults.hash;
|
36
|
+
this.background = this.options.background || this.defaults.background;
|
37
|
+
this.foreground = this.options.foreground;
|
38
|
+
this.margin = this.options.margin || this.defaults.margin;
|
39
|
+
this.size = this.options.size || this.defaults.size;
|
40
|
+
};
|
41
|
+
|
42
|
+
Identicon.prototype = {
|
43
|
+
background: null,
|
44
|
+
foreground: null,
|
45
|
+
hash: null,
|
46
|
+
margin: null,
|
47
|
+
size: null,
|
48
|
+
|
49
|
+
render: function(){
|
50
|
+
var hash = this.hash,
|
51
|
+
size = this.size,
|
52
|
+
baseMargin = Math.floor(size * this.margin),
|
53
|
+
cell = Math.floor((size - (baseMargin * 2)) / 5),
|
54
|
+
margin = Math.floor((size - cell * 5) / 2),
|
55
|
+
image = new PNGlib(size, size, 256);
|
56
|
+
|
57
|
+
// light-grey background
|
58
|
+
var bg = image.color(this.background[0], this.background[1], this.background[2], this.background[3]),
|
59
|
+
fg;
|
60
|
+
|
61
|
+
if (this.foreground) {
|
62
|
+
fg = image.color(this.foreground[0], this.foreground[1], this.foreground[2]);
|
63
|
+
} else {
|
64
|
+
// foreground is last 7 chars as hue at 50% saturation, 70% brightness
|
65
|
+
var rgb = this.hsl2rgb(parseInt(hash.substr(-7), 16) / 0xfffffff, 0.5, 0.7);
|
66
|
+
fg = image.color(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255);
|
67
|
+
}
|
68
|
+
|
69
|
+
// the first 15 characters of the hash control the pixels (even/odd)
|
70
|
+
// they are drawn down the middle first, then mirrored outwards
|
71
|
+
var i, color;
|
72
|
+
for (i = 0; i < 15; i++) {
|
73
|
+
color = parseInt(hash.charAt(i), 16) % 2 ? bg : fg;
|
74
|
+
if (i < 5) {
|
75
|
+
this.rectangle(2 * cell + margin, i * cell + margin, cell, cell, color, image);
|
76
|
+
} else if (i < 10) {
|
77
|
+
this.rectangle(1 * cell + margin, (i - 5) * cell + margin, cell, cell, color, image);
|
78
|
+
this.rectangle(3 * cell + margin, (i - 5) * cell + margin, cell, cell, color, image);
|
79
|
+
} else if (i < 15) {
|
80
|
+
this.rectangle(0 * cell + margin, (i - 10) * cell + margin, cell, cell, color, image);
|
81
|
+
this.rectangle(4 * cell + margin, (i - 10) * cell + margin, cell, cell, color, image);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
return image;
|
86
|
+
},
|
87
|
+
|
88
|
+
rectangle: function(x, y, w, h, color, image) {
|
89
|
+
var i, j;
|
90
|
+
for (i = x; i < x + w; i++) {
|
91
|
+
for (j = y; j < y + h; j++) {
|
92
|
+
image.buffer[image.index(i, j)] = color;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
},
|
96
|
+
|
97
|
+
// adapted from: https://gist.github.com/aemkei/1325937
|
98
|
+
hsl2rgb: function(h, s, b){
|
99
|
+
h *= 6;
|
100
|
+
s = [
|
101
|
+
b += s *= b < .5 ? b : 1 - b,
|
102
|
+
b - h % 1 * s * 2,
|
103
|
+
b -= s *= 2,
|
104
|
+
b,
|
105
|
+
b + h % 1 * s,
|
106
|
+
b + s
|
107
|
+
];
|
108
|
+
|
109
|
+
return[
|
110
|
+
s[ ~~h % 6 ], // red
|
111
|
+
s[ (h|16) % 6 ], // green
|
112
|
+
s[ (h|8) % 6 ] // blue
|
113
|
+
];
|
114
|
+
},
|
115
|
+
|
116
|
+
toString: function(){
|
117
|
+
return this.render().getBase64();
|
118
|
+
},
|
119
|
+
|
120
|
+
// Creates a consistent-length hash from a string
|
121
|
+
createHashFromString: function(str) {
|
122
|
+
var hash = '0', salt = 'identicon', i, chr, len;
|
123
|
+
|
124
|
+
if (!str) {
|
125
|
+
return hash;
|
126
|
+
}
|
127
|
+
|
128
|
+
str += salt + str; // Better randomization for short inputs.
|
129
|
+
|
130
|
+
for (i = 0, len = str.length; i < len; i++) {
|
131
|
+
chr = str.charCodeAt(i);
|
132
|
+
hash = ((hash << 5) - hash) + chr;
|
133
|
+
hash |= 0; // Convert to 32bit integer
|
134
|
+
}
|
135
|
+
return hash.toString();
|
136
|
+
}
|
137
|
+
};
|
138
|
+
|
139
|
+
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
140
|
+
module.exports = Identicon;
|
141
|
+
} else {
|
142
|
+
window.Identicon = Identicon;
|
143
|
+
}
|
144
|
+
})();
|
@@ -0,0 +1,214 @@
|
|
1
|
+
/**
|
2
|
+
* A handy class to calculate color values.
|
3
|
+
*
|
4
|
+
* @version 1.0
|
5
|
+
* @author Robert Eisele <robert@xarg.org>
|
6
|
+
* @copyright Copyright (c) 2010, Robert Eisele
|
7
|
+
* @link http://www.xarg.org/2010/03/generate-client-side-png-files-using-javascript/
|
8
|
+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
9
|
+
*
|
10
|
+
*/
|
11
|
+
|
12
|
+
(function() {
|
13
|
+
|
14
|
+
// helper functions for that ctx
|
15
|
+
function write(buffer, offs) {
|
16
|
+
for (var i = 2; i < arguments.length; i++) {
|
17
|
+
for (var j = 0; j < arguments[i].length; j++) {
|
18
|
+
buffer[offs++] = arguments[i].charAt(j);
|
19
|
+
}
|
20
|
+
}
|
21
|
+
}
|
22
|
+
|
23
|
+
function byte2(w) {
|
24
|
+
return String.fromCharCode((w >> 8) & 255, w & 255);
|
25
|
+
}
|
26
|
+
|
27
|
+
function byte4(w) {
|
28
|
+
return String.fromCharCode((w >> 24) & 255, (w >> 16) & 255, (w >> 8) & 255, w & 255);
|
29
|
+
}
|
30
|
+
|
31
|
+
function byte2lsb(w) {
|
32
|
+
return String.fromCharCode(w & 255, (w >> 8) & 255);
|
33
|
+
}
|
34
|
+
|
35
|
+
// modified from original source to support NPM
|
36
|
+
var PNGlib = function(width,height,depth) {
|
37
|
+
|
38
|
+
this.width = width;
|
39
|
+
this.height = height;
|
40
|
+
this.depth = depth;
|
41
|
+
|
42
|
+
// pixel data and row filter identifier size
|
43
|
+
this.pix_size = height * (width + 1);
|
44
|
+
|
45
|
+
// deflate header, pix_size, block headers, adler32 checksum
|
46
|
+
this.data_size = 2 + this.pix_size + 5 * Math.floor((0xfffe + this.pix_size) / 0xffff) + 4;
|
47
|
+
|
48
|
+
// offsets and sizes of Png chunks
|
49
|
+
this.ihdr_offs = 0; // IHDR offset and size
|
50
|
+
this.ihdr_size = 4 + 4 + 13 + 4;
|
51
|
+
this.plte_offs = this.ihdr_offs + this.ihdr_size; // PLTE offset and size
|
52
|
+
this.plte_size = 4 + 4 + 3 * depth + 4;
|
53
|
+
this.trns_offs = this.plte_offs + this.plte_size; // tRNS offset and size
|
54
|
+
this.trns_size = 4 + 4 + depth + 4;
|
55
|
+
this.idat_offs = this.trns_offs + this.trns_size; // IDAT offset and size
|
56
|
+
this.idat_size = 4 + 4 + this.data_size + 4;
|
57
|
+
this.iend_offs = this.idat_offs + this.idat_size; // IEND offset and size
|
58
|
+
this.iend_size = 4 + 4 + 4;
|
59
|
+
this.buffer_size = this.iend_offs + this.iend_size; // total PNG size
|
60
|
+
|
61
|
+
this.buffer = new Array();
|
62
|
+
this.palette = new Object();
|
63
|
+
this.pindex = 0;
|
64
|
+
|
65
|
+
var _crc32 = new Array();
|
66
|
+
|
67
|
+
// initialize buffer with zero bytes
|
68
|
+
for (var i = 0; i < this.buffer_size; i++) {
|
69
|
+
this.buffer[i] = "\x00";
|
70
|
+
}
|
71
|
+
|
72
|
+
// initialize non-zero elements
|
73
|
+
write(this.buffer, this.ihdr_offs, byte4(this.ihdr_size - 12), 'IHDR', byte4(width), byte4(height), "\x08\x03");
|
74
|
+
write(this.buffer, this.plte_offs, byte4(this.plte_size - 12), 'PLTE');
|
75
|
+
write(this.buffer, this.trns_offs, byte4(this.trns_size - 12), 'tRNS');
|
76
|
+
write(this.buffer, this.idat_offs, byte4(this.idat_size - 12), 'IDAT');
|
77
|
+
write(this.buffer, this.iend_offs, byte4(this.iend_size - 12), 'IEND');
|
78
|
+
|
79
|
+
// initialize deflate header
|
80
|
+
var header = ((8 + (7 << 4)) << 8) | (3 << 6);
|
81
|
+
header+= 31 - (header % 31);
|
82
|
+
|
83
|
+
write(this.buffer, this.idat_offs + 8, byte2(header));
|
84
|
+
|
85
|
+
// initialize deflate block headers
|
86
|
+
for (var i = 0; (i << 16) - 1 < this.pix_size; i++) {
|
87
|
+
var size, bits;
|
88
|
+
if (i + 0xffff < this.pix_size) {
|
89
|
+
size = 0xffff;
|
90
|
+
bits = "\x00";
|
91
|
+
} else {
|
92
|
+
size = this.pix_size - (i << 16) - i;
|
93
|
+
bits = "\x01";
|
94
|
+
}
|
95
|
+
write(this.buffer, this.idat_offs + 8 + 2 + (i << 16) + (i << 2), bits, byte2lsb(size), byte2lsb(~size));
|
96
|
+
}
|
97
|
+
|
98
|
+
/* Create crc32 lookup table */
|
99
|
+
for (var i = 0; i < 256; i++) {
|
100
|
+
var c = i;
|
101
|
+
for (var j = 0; j < 8; j++) {
|
102
|
+
if (c & 1) {
|
103
|
+
c = -306674912 ^ ((c >> 1) & 0x7fffffff);
|
104
|
+
} else {
|
105
|
+
c = (c >> 1) & 0x7fffffff;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
_crc32[i] = c;
|
109
|
+
}
|
110
|
+
|
111
|
+
// compute the index into a png for a given pixel
|
112
|
+
this.index = function(x,y) {
|
113
|
+
var i = y * (this.width + 1) + x + 1;
|
114
|
+
var j = this.idat_offs + 8 + 2 + 5 * Math.floor((i / 0xffff) + 1) + i;
|
115
|
+
return j;
|
116
|
+
}
|
117
|
+
|
118
|
+
// convert a color and build up the palette
|
119
|
+
this.color = function(red, green, blue, alpha) {
|
120
|
+
|
121
|
+
alpha = alpha >= 0 ? alpha : 255;
|
122
|
+
var color = (((((alpha << 8) | red) << 8) | green) << 8) | blue;
|
123
|
+
|
124
|
+
if (typeof this.palette[color] == "undefined") {
|
125
|
+
if (this.pindex == this.depth) return "\x00";
|
126
|
+
|
127
|
+
var ndx = this.plte_offs + 8 + 3 * this.pindex;
|
128
|
+
|
129
|
+
this.buffer[ndx + 0] = String.fromCharCode(red);
|
130
|
+
this.buffer[ndx + 1] = String.fromCharCode(green);
|
131
|
+
this.buffer[ndx + 2] = String.fromCharCode(blue);
|
132
|
+
this.buffer[this.trns_offs+8+this.pindex] = String.fromCharCode(alpha);
|
133
|
+
|
134
|
+
this.palette[color] = String.fromCharCode(this.pindex++);
|
135
|
+
}
|
136
|
+
return this.palette[color];
|
137
|
+
}
|
138
|
+
|
139
|
+
// output a PNG string, Base64 encoded
|
140
|
+
this.getBase64 = function() {
|
141
|
+
|
142
|
+
var s = this.getDump();
|
143
|
+
|
144
|
+
var ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
145
|
+
var c1, c2, c3, e1, e2, e3, e4;
|
146
|
+
var l = s.length;
|
147
|
+
var i = 0;
|
148
|
+
var r = "";
|
149
|
+
|
150
|
+
do {
|
151
|
+
c1 = s.charCodeAt(i);
|
152
|
+
e1 = c1 >> 2;
|
153
|
+
c2 = s.charCodeAt(i+1);
|
154
|
+
e2 = ((c1 & 3) << 4) | (c2 >> 4);
|
155
|
+
c3 = s.charCodeAt(i+2);
|
156
|
+
if (l < i+2) { e3 = 64; } else { e3 = ((c2 & 0xf) << 2) | (c3 >> 6); }
|
157
|
+
if (l < i+3) { e4 = 64; } else { e4 = c3 & 0x3f; }
|
158
|
+
r+= ch.charAt(e1) + ch.charAt(e2) + ch.charAt(e3) + ch.charAt(e4);
|
159
|
+
} while ((i+= 3) < l);
|
160
|
+
return r;
|
161
|
+
}
|
162
|
+
|
163
|
+
// output a PNG string
|
164
|
+
this.getDump = function() {
|
165
|
+
|
166
|
+
// compute adler32 of output pixels + row filter bytes
|
167
|
+
var BASE = 65521; /* largest prime smaller than 65536 */
|
168
|
+
var NMAX = 5552; /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
|
169
|
+
var s1 = 1;
|
170
|
+
var s2 = 0;
|
171
|
+
var n = NMAX;
|
172
|
+
|
173
|
+
for (var y = 0; y < this.height; y++) {
|
174
|
+
for (var x = -1; x < this.width; x++) {
|
175
|
+
s1+= this.buffer[this.index(x, y)].charCodeAt(0);
|
176
|
+
s2+= s1;
|
177
|
+
if ((n-= 1) == 0) {
|
178
|
+
s1%= BASE;
|
179
|
+
s2%= BASE;
|
180
|
+
n = NMAX;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
}
|
184
|
+
s1%= BASE;
|
185
|
+
s2%= BASE;
|
186
|
+
write(this.buffer, this.idat_offs + this.idat_size - 8, byte4((s2 << 16) | s1));
|
187
|
+
|
188
|
+
// compute crc32 of the PNG chunks
|
189
|
+
function crc32(png, offs, size) {
|
190
|
+
var crc = -1;
|
191
|
+
for (var i = 4; i < size-4; i += 1) {
|
192
|
+
crc = _crc32[(crc ^ png[offs+i].charCodeAt(0)) & 0xff] ^ ((crc >> 8) & 0x00ffffff);
|
193
|
+
}
|
194
|
+
write(png, offs+size-4, byte4(crc ^ -1));
|
195
|
+
}
|
196
|
+
|
197
|
+
crc32(this.buffer, this.ihdr_offs, this.ihdr_size);
|
198
|
+
crc32(this.buffer, this.plte_offs, this.plte_size);
|
199
|
+
crc32(this.buffer, this.trns_offs, this.trns_size);
|
200
|
+
crc32(this.buffer, this.idat_offs, this.idat_size);
|
201
|
+
crc32(this.buffer, this.iend_offs, this.iend_size);
|
202
|
+
|
203
|
+
// convert PNG to string
|
204
|
+
return "\211PNG\r\n\032\n"+this.buffer.join('');
|
205
|
+
}
|
206
|
+
}
|
207
|
+
|
208
|
+
// modified from original source to support NPM
|
209
|
+
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
210
|
+
module.exports = PNGlib;
|
211
|
+
} else {
|
212
|
+
window.PNGlib = PNGlib;
|
213
|
+
}
|
214
|
+
})();
|