jail 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +57 -0
  3. data/Rakefile +40 -0
  4. data/app/assets/javascripts/jail/application.js +15 -0
  5. data/app/assets/stylesheets/jail/application.css +28 -0
  6. data/app/controllers/jail/application_controller.rb +4 -0
  7. data/app/controllers/jail/githubs_controller.rb +20 -0
  8. data/app/models/jail/github.rb +75 -0
  9. data/app/views/jail/githubs/index.html.erb +10 -0
  10. data/app/views/jail/githubs/show.html.erb +10 -0
  11. data/app/views/layouts/jail/application.html.erb +20 -0
  12. data/config/prisoners.yml +49 -0
  13. data/config/routes.rb +7 -0
  14. data/lib/jail.rb +6 -0
  15. data/lib/jail/engine.rb +5 -0
  16. data/lib/jail/version.rb +3 -0
  17. data/lib/tasks/jail_tasks.rake +4 -0
  18. data/test/dummy/README.rdoc +261 -0
  19. data/test/dummy/Rakefile +7 -0
  20. data/test/dummy/app/assets/javascripts/application.js +15 -0
  21. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  22. data/test/dummy/app/controllers/application_controller.rb +3 -0
  23. data/test/dummy/app/helpers/application_helper.rb +2 -0
  24. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/test/dummy/config.ru +4 -0
  26. data/test/dummy/config/application.rb +59 -0
  27. data/test/dummy/config/boot.rb +10 -0
  28. data/test/dummy/config/database.yml +25 -0
  29. data/test/dummy/config/environment.rb +5 -0
  30. data/test/dummy/config/environments/development.rb +37 -0
  31. data/test/dummy/config/environments/production.rb +67 -0
  32. data/test/dummy/config/environments/test.rb +37 -0
  33. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  34. data/test/dummy/config/initializers/inflections.rb +15 -0
  35. data/test/dummy/config/initializers/jail.rb +3 -0
  36. data/test/dummy/config/initializers/mime_types.rb +5 -0
  37. data/test/dummy/config/initializers/secret_token.rb +7 -0
  38. data/test/dummy/config/initializers/session_store.rb +8 -0
  39. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  40. data/test/dummy/config/locales/en.yml +5 -0
  41. data/test/dummy/config/routes.rb +4 -0
  42. data/test/dummy/db/development.sqlite3 +0 -0
  43. data/test/dummy/log/development.log +22817 -0
  44. data/test/dummy/public/404.html +26 -0
  45. data/test/dummy/public/422.html +26 -0
  46. data/test/dummy/public/500.html +25 -0
  47. data/test/dummy/public/favicon.ico +0 -0
  48. data/test/dummy/script/rails +6 -0
  49. data/test/dummy/tmp/cache/assets/C78/2D0/sprockets%2F049c9453e2d03a13463ad15568c8521f +0 -0
  50. data/test/dummy/tmp/cache/assets/CBE/8B0/sprockets%2F45f037820eca943af359e247967580cf +0 -0
  51. data/test/dummy/tmp/cache/assets/CFA/570/sprockets%2Ffe84858b375458bd418cb85c8f84803c +0 -0
  52. data/test/dummy/tmp/cache/assets/D15/AE0/sprockets%2F3fd1b8f0c577d6bd951f40857132eb74 +0 -0
  53. data/test/dummy/tmp/cache/assets/D29/F40/sprockets%2F15ce02e64710b4fa1d3a21f6270fdc74 +0 -0
  54. data/test/dummy/tmp/cache/assets/D34/BF0/sprockets%2F18a1d0fec1cd74447e07b02d058a289d +0 -0
  55. data/test/dummy/tmp/cache/assets/D46/B30/sprockets%2F36e940335f9fc2de06c6d8aad556d822 +0 -0
  56. data/test/dummy/tmp/cache/assets/D4B/BC0/sprockets%2Fa3a0b270a0bb2747cad33e424662fba2 +0 -0
  57. data/test/dummy/tmp/cache/assets/D76/A70/sprockets%2F360c75fdc8ed695061e3b08da593d7cc +0 -0
  58. data/test/dummy/tmp/cache/assets/D81/500/sprockets%2F23e9c52aedd634ee57e66663cffa1868 +0 -0
  59. data/test/dummy/tmp/cache/assets/D9E/630/sprockets%2F0efe12c5dc1902e8cf8f6844b7bca812 +0 -0
  60. data/test/dummy/tmp/cache/assets/DDF/C90/sprockets%2Fc4dff5559e7f91debdf5da74921c028e +0 -0
  61. data/test/dummy/tmp/cache/assets/E64/6F0/sprockets%2F8f0e4dd1aa8c3ddea851bf771dce8f8c +0 -0
  62. data/test/dummy/tmp/pids/server.pid +1 -0
  63. data/test/dummy/vendor/assets/images/Jcrop.gif +0 -0
  64. data/test/dummy/vendor/assets/javascripts/cookies.js +106 -0
  65. data/test/dummy/vendor/assets/javascripts/jquery.Jcrop.js +1699 -0
  66. data/test/dummy/vendor/assets/stylesheets/jquery.Jcrop.css +86 -0
  67. data/test/fixtures/jqplug/jqgithubs.yml +11 -0
  68. data/test/functional/jqplug/githubs_controller_test.rb +51 -0
  69. data/test/functional/jqplug/jqgithubs_controller_test.rb +51 -0
  70. data/test/integration/navigation_test.rb +10 -0
  71. data/test/jqplug_test.rb +7 -0
  72. data/test/test_helper.rb +15 -0
  73. data/test/unit/helpers/jqplug/githubs_helper_test.rb +6 -0
  74. data/test/unit/helpers/jqplug/jqgithubs_helper_test.rb +6 -0
  75. data/test/unit/jqplug/jqgithub_test.rb +9 -0
  76. metadata +269 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1 @@
1
+ 24607
@@ -0,0 +1,106 @@
1
+ /*!
2
+ * Cookies.js - 0.2.0
3
+ * Friday, June 15 2012 @ 7:38 PM EST
4
+ *
5
+ * Copyright (c) 2012, Scott Hamper
6
+ * Licensed under the MIT license,
7
+ * http://www.opensource.org/licenses/MIT
8
+ */
9
+ (function (document, undefined) {
10
+ 'use strict';
11
+
12
+ var Cookies = function (key, value, options) {
13
+ return arguments.length === 1 ?
14
+ Cookies.get(key) : Cookies.set(key, value, options);
15
+ };
16
+
17
+ Cookies.get = function (key) {
18
+ if (document.cookie !== Cookies._cacheString) {
19
+ Cookies._populateCache();
20
+ }
21
+
22
+ return Cookies._cache[key];
23
+ };
24
+
25
+ Cookies.defaults = {
26
+ path: '/'
27
+ };
28
+
29
+ Cookies.set = function (key, value, options) {
30
+ var options = {
31
+ path: options && options.path || Cookies.defaults.path,
32
+ domain: options && options.domain || Cookies.defaults.domain,
33
+ expires: options && options.expires || Cookies.defaults.expires,
34
+ secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure
35
+ };
36
+
37
+ if (value === undefined) {
38
+ options.expires = -1;
39
+ }
40
+
41
+ switch (typeof options.expires) {
42
+ // If a number is passed in, make it work like 'max-age'
43
+ case 'number': options.expires = new Date(new Date().getTime() + options.expires * 1000); break;
44
+ // Allow multiple string formats for dates
45
+ case 'string': options.expires = new Date(options.expires); break;
46
+ }
47
+
48
+ // Escape only the characters that should be escaped as defined by RFC6265
49
+ var cookieString = encodeURIComponent(key) + '=' + (value + '').replace(/[^!#-+\--:<-[\]-~]/g, encodeURIComponent);
50
+ cookieString += options.path ? ';path=' + options.path : '';
51
+ cookieString += options.domain ? ';domain=' + options.domain : '';
52
+ cookieString += options.expires ? ';expires=' + options.expires.toGMTString() : '';
53
+ cookieString += options.secure ? ';secure' : '';
54
+
55
+ document.cookie = cookieString;
56
+
57
+ return Cookies;
58
+ };
59
+
60
+ Cookies.expire = function (key, options) {
61
+ return Cookies.set(key, undefined, options);
62
+ };
63
+
64
+ Cookies._populateCache = function () {
65
+ Cookies._cache = {};
66
+ Cookies._cacheString = document.cookie;
67
+
68
+ var cookiesArray = Cookies._cacheString.split('; ');
69
+ for (var i = 0; i < cookiesArray.length; i++) {
70
+ // The cookie value can contain a '=', so cannot use 'split'
71
+ var separatorIndex = cookiesArray[i].indexOf('=');
72
+ var key = decodeURIComponent(cookiesArray[i].substr(0, separatorIndex));
73
+ var value = decodeURIComponent(cookiesArray[i].substr(separatorIndex + 1));
74
+
75
+ // The first instance of a key in the document.cookie string
76
+ // is the most locally scoped cookie with the specified key.
77
+ // The value of this key will be sent to the web server, so we'll
78
+ // just ignore any other instances of the key.
79
+ if (Cookies._cache[key] === undefined) {
80
+ Cookies._cache[key] = value;
81
+ }
82
+ }
83
+ };
84
+
85
+ Cookies.enabled = (function () {
86
+ var isEnabled = Cookies.set('cookies.js', '1').get('cookies.js') === '1';
87
+ Cookies.expire('cookies.js');
88
+ return isEnabled;
89
+ })();
90
+
91
+ // AMD support
92
+ if (typeof define === 'function' && define.amd) {
93
+ define(function () { return Cookies; });
94
+ // CommonJS and Node.js module support.
95
+ } else if (typeof exports !== 'undefined') {
96
+ // Support Node.js specific `module.exports` (which can be a function)
97
+ if (typeof module != 'undefined' && module.exports) {
98
+ exports = module.exports = Cookies;
99
+ }
100
+ // But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
101
+ exports.Cookies = Cookies;
102
+ } else {
103
+ window.Cookies = Cookies;
104
+ }
105
+
106
+ })(document);
@@ -0,0 +1,1699 @@
1
+ /**
2
+ * jquery.Jcrop.js v0.9.10
3
+ * jQuery Image Cropping Plugin - released under MIT License
4
+ * Author: Kelly Hallman <khallman@gmail.com>
5
+ * http://github.com/tapmodo/Jcrop
6
+ * Copyright (c) 2008-2012 Tapmodo Interactive LLC {{{
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person
9
+ * obtaining a copy of this software and associated documentation
10
+ * files (the "Software"), to deal in the Software without
11
+ * restriction, including without limitation the rights to use,
12
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the
14
+ * Software is furnished to do so, subject to the following
15
+ * conditions:
16
+ *
17
+ * The above copyright notice and this permission notice shall be
18
+ * included in all copies or substantial portions of the Software.
19
+ *
20
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27
+ * OTHER DEALINGS IN THE SOFTWARE.
28
+ *
29
+ * }}}
30
+ */
31
+
32
+ (function ($) {
33
+
34
+ $.Jcrop = function (obj, opt) {
35
+ var options = $.extend({}, $.Jcrop.defaults),
36
+ docOffset, lastcurs, ie6mode = false;
37
+
38
+ // Internal Methods {{{
39
+ function px(n) {
40
+ return Math.round(n) + 'px';
41
+ }
42
+ function cssClass(cl) {
43
+ return options.baseClass + '-' + cl;
44
+ }
45
+ function supportsColorFade() {
46
+ return $.fx.step.hasOwnProperty('backgroundColor');
47
+ }
48
+ function getPos(obj) //{{{
49
+ {
50
+ var pos = $(obj).offset();
51
+ return [pos.left, pos.top];
52
+ }
53
+ //}}}
54
+ function mouseAbs(e) //{{{
55
+ {
56
+ return [(e.pageX - docOffset[0]), (e.pageY - docOffset[1])];
57
+ }
58
+ //}}}
59
+ function setOptions(opt) //{{{
60
+ {
61
+ if (typeof(opt) !== 'object') opt = {};
62
+ options = $.extend(options, opt);
63
+
64
+ $.each(['onChange','onSelect','onRelease','onDblClick'],function(i,e) {
65
+ if (typeof(options[e]) !== 'function') options[e] = function () {};
66
+ });
67
+ }
68
+ //}}}
69
+ function startDragMode(mode, pos) //{{{
70
+ {
71
+ docOffset = getPos($img);
72
+ Tracker.setCursor(mode === 'move' ? mode : mode + '-resize');
73
+
74
+ if (mode === 'move') {
75
+ return Tracker.activateHandlers(createMover(pos), doneSelect);
76
+ }
77
+
78
+ var fc = Coords.getFixed();
79
+ var opp = oppLockCorner(mode);
80
+ var opc = Coords.getCorner(oppLockCorner(opp));
81
+
82
+ Coords.setPressed(Coords.getCorner(opp));
83
+ Coords.setCurrent(opc);
84
+
85
+ Tracker.activateHandlers(dragmodeHandler(mode, fc), doneSelect);
86
+ }
87
+ //}}}
88
+ function dragmodeHandler(mode, f) //{{{
89
+ {
90
+ return function (pos) {
91
+ if (!options.aspectRatio) {
92
+ switch (mode) {
93
+ case 'e':
94
+ pos[1] = f.y2;
95
+ break;
96
+ case 'w':
97
+ pos[1] = f.y2;
98
+ break;
99
+ case 'n':
100
+ pos[0] = f.x2;
101
+ break;
102
+ case 's':
103
+ pos[0] = f.x2;
104
+ break;
105
+ }
106
+ } else {
107
+ switch (mode) {
108
+ case 'e':
109
+ pos[1] = f.y + 1;
110
+ break;
111
+ case 'w':
112
+ pos[1] = f.y + 1;
113
+ break;
114
+ case 'n':
115
+ pos[0] = f.x + 1;
116
+ break;
117
+ case 's':
118
+ pos[0] = f.x + 1;
119
+ break;
120
+ }
121
+ }
122
+ Coords.setCurrent(pos);
123
+ Selection.update();
124
+ };
125
+ }
126
+ //}}}
127
+ function createMover(pos) //{{{
128
+ {
129
+ var lloc = pos;
130
+ KeyManager.watchKeys();
131
+
132
+ return function (pos) {
133
+ Coords.moveOffset([pos[0] - lloc[0], pos[1] - lloc[1]]);
134
+ lloc = pos;
135
+
136
+ Selection.update();
137
+ };
138
+ }
139
+ //}}}
140
+ function oppLockCorner(ord) //{{{
141
+ {
142
+ switch (ord) {
143
+ case 'n':
144
+ return 'sw';
145
+ case 's':
146
+ return 'nw';
147
+ case 'e':
148
+ return 'nw';
149
+ case 'w':
150
+ return 'ne';
151
+ case 'ne':
152
+ return 'sw';
153
+ case 'nw':
154
+ return 'se';
155
+ case 'se':
156
+ return 'nw';
157
+ case 'sw':
158
+ return 'ne';
159
+ }
160
+ }
161
+ //}}}
162
+ function createDragger(ord) //{{{
163
+ {
164
+ return function (e) {
165
+ if (options.disabled) {
166
+ return false;
167
+ }
168
+ if ((ord === 'move') && !options.allowMove) {
169
+ return false;
170
+ }
171
+
172
+ // Fix position of crop area when dragged the very first time.
173
+ // Necessary when crop image is in a hidden element when page is loaded.
174
+ docOffset = getPos($img);
175
+
176
+ btndown = true;
177
+ startDragMode(ord, mouseAbs(e));
178
+ e.stopPropagation();
179
+ e.preventDefault();
180
+ return false;
181
+ };
182
+ }
183
+ //}}}
184
+ function presize($obj, w, h) //{{{
185
+ {
186
+ var nw = $obj.width(),
187
+ nh = $obj.height();
188
+ if ((nw > w) && w > 0) {
189
+ nw = w;
190
+ nh = (w / $obj.width()) * $obj.height();
191
+ }
192
+ if ((nh > h) && h > 0) {
193
+ nh = h;
194
+ nw = (h / $obj.height()) * $obj.width();
195
+ }
196
+ xscale = $obj.width() / nw;
197
+ yscale = $obj.height() / nh;
198
+ $obj.width(nw).height(nh);
199
+ }
200
+ //}}}
201
+ function unscale(c) //{{{
202
+ {
203
+ return {
204
+ x: c.x * xscale,
205
+ y: c.y * yscale,
206
+ x2: c.x2 * xscale,
207
+ y2: c.y2 * yscale,
208
+ w: c.w * xscale,
209
+ h: c.h * yscale
210
+ };
211
+ }
212
+ //}}}
213
+ function doneSelect(pos) //{{{
214
+ {
215
+ var c = Coords.getFixed();
216
+ if ((c.w > options.minSelect[0]) && (c.h > options.minSelect[1])) {
217
+ Selection.enableHandles();
218
+ Selection.done();
219
+ } else {
220
+ Selection.release();
221
+ }
222
+ Tracker.setCursor(options.allowSelect ? 'crosshair' : 'default');
223
+ }
224
+ //}}}
225
+ function newSelection(e) //{{{
226
+ {
227
+ if (options.disabled) {
228
+ return false;
229
+ }
230
+ if (!options.allowSelect) {
231
+ return false;
232
+ }
233
+ btndown = true;
234
+ docOffset = getPos($img);
235
+ Selection.disableHandles();
236
+ Tracker.setCursor('crosshair');
237
+ var pos = mouseAbs(e);
238
+ Coords.setPressed(pos);
239
+ Selection.update();
240
+ Tracker.activateHandlers(selectDrag, doneSelect);
241
+ KeyManager.watchKeys();
242
+
243
+ e.stopPropagation();
244
+ e.preventDefault();
245
+ return false;
246
+ }
247
+ //}}}
248
+ function selectDrag(pos) //{{{
249
+ {
250
+ Coords.setCurrent(pos);
251
+ Selection.update();
252
+ }
253
+ //}}}
254
+ function newTracker() //{{{
255
+ {
256
+ var trk = $('<div></div>').addClass(cssClass('tracker'));
257
+ if ($.browser.msie) {
258
+ trk.css({
259
+ opacity: 0,
260
+ backgroundColor: 'white'
261
+ });
262
+ }
263
+ return trk;
264
+ }
265
+ //}}}
266
+
267
+ // }}}
268
+ // Initialization {{{
269
+ // Sanitize some options {{{
270
+ if ($.browser.msie && ($.browser.version.split('.')[0] === '6')) {
271
+ ie6mode = true;
272
+ }
273
+ if (typeof(obj) !== 'object') {
274
+ obj = $(obj)[0];
275
+ }
276
+ if (typeof(opt) !== 'object') {
277
+ opt = {};
278
+ }
279
+ // }}}
280
+ setOptions(opt);
281
+ // Initialize some jQuery objects {{{
282
+ // The values are SET on the image(s) for the interface
283
+ // If the original image has any of these set, they will be reset
284
+ // However, if you destroy() the Jcrop instance the original image's
285
+ // character in the DOM will be as you left it.
286
+ var img_css = {
287
+ border: 'none',
288
+ visibility: 'visible',
289
+ margin: 0,
290
+ padding: 0,
291
+ position: 'absolute',
292
+ top: 0,
293
+ left: 0
294
+ };
295
+
296
+ var $origimg = $(obj),
297
+ img_mode = true;
298
+
299
+ if (obj.tagName == 'IMG') {
300
+ // Fix size of crop image.
301
+ // Necessary when crop image is within a hidden element when page is loaded.
302
+ if ($origimg[0].width != 0 && $origimg[0].height != 0) {
303
+ // Obtain dimensions from contained img element.
304
+ $origimg.width($origimg[0].width);
305
+ $origimg.height($origimg[0].height);
306
+ } else {
307
+ // Obtain dimensions from temporary image in case the original is not loaded yet (e.g. IE 7.0).
308
+ var tempImage = new Image();
309
+ tempImage.src = $origimg[0].src;
310
+ $origimg.width(tempImage.width);
311
+ $origimg.height(tempImage.height);
312
+ }
313
+
314
+ var $img = $origimg.clone().removeAttr('id').css(img_css).show();
315
+
316
+ $img.width($origimg.width());
317
+ $img.height($origimg.height());
318
+ $origimg.after($img).hide();
319
+
320
+ } else {
321
+ $img = $origimg.css(img_css).show();
322
+ img_mode = false;
323
+ if (options.shade === null) { options.shade = true; }
324
+ }
325
+
326
+ presize($img, options.boxWidth, options.boxHeight);
327
+
328
+ var boundx = $img.width(),
329
+ boundy = $img.height(),
330
+
331
+
332
+ $div = $('<div />').width(boundx).height(boundy).addClass(cssClass('holder')).css({
333
+ position: 'relative',
334
+ backgroundColor: options.bgColor
335
+ }).insertAfter($origimg).append($img);
336
+
337
+ if (options.addClass) {
338
+ $div.addClass(options.addClass);
339
+ }
340
+
341
+ var $img2 = $('<div />'),
342
+
343
+ $img_holder = $('<div />')
344
+ .width('100%').height('100%').css({
345
+ zIndex: 310,
346
+ position: 'absolute',
347
+ overflow: 'hidden'
348
+ }),
349
+
350
+ $hdl_holder = $('<div />')
351
+ .width('100%').height('100%').css('zIndex', 320),
352
+
353
+ $sel = $('<div />')
354
+ .css({
355
+ position: 'absolute',
356
+ zIndex: 600
357
+ }).dblclick(function(){
358
+ var c = Coords.getFixed();
359
+ options.onDblClick.call(api,c);
360
+ }).insertBefore($img).append($img_holder, $hdl_holder);
361
+
362
+ if (img_mode) {
363
+
364
+ $img2 = $('<img />')
365
+ .attr('src', $img.attr('src')).css(img_css).width(boundx).height(boundy),
366
+
367
+ $img_holder.append($img2);
368
+
369
+ }
370
+
371
+ if (ie6mode) {
372
+ $sel.css({
373
+ overflowY: 'hidden'
374
+ });
375
+ }
376
+
377
+ var bound = options.boundary;
378
+ var $trk = newTracker().width(boundx + (bound * 2)).height(boundy + (bound * 2)).css({
379
+ position: 'absolute',
380
+ top: px(-bound),
381
+ left: px(-bound),
382
+ zIndex: 290
383
+ }).mousedown(newSelection);
384
+
385
+ /* }}} */
386
+ // Set more variables {{{
387
+ var bgcolor = options.bgColor,
388
+ bgopacity = options.bgOpacity,
389
+ xlimit, ylimit, xmin, ymin, xscale, yscale, enabled = true,
390
+ btndown, animating, shift_down;
391
+
392
+ docOffset = getPos($img);
393
+ // }}}
394
+ // }}}
395
+ // Internal Modules {{{
396
+ // Touch Module {{{
397
+ var Touch = (function () {
398
+ // Touch support detection function adapted (under MIT License)
399
+ // from code by Jeffrey Sambells - http://github.com/iamamused/
400
+ function hasTouchSupport() {
401
+ var support = {},
402
+ events = ['touchstart', 'touchmove', 'touchend'],
403
+ el = document.createElement('div'), i;
404
+
405
+ try {
406
+ for(i=0; i<events.length; i++) {
407
+ var eventName = events[i];
408
+ eventName = 'on' + eventName;
409
+ var isSupported = (eventName in el);
410
+ if (!isSupported) {
411
+ el.setAttribute(eventName, 'return;');
412
+ isSupported = typeof el[eventName] == 'function';
413
+ }
414
+ support[events[i]] = isSupported;
415
+ }
416
+ return support.touchstart && support.touchend && support.touchmove;
417
+ }
418
+ catch(err) {
419
+ return false;
420
+ }
421
+ }
422
+
423
+ function detectSupport() {
424
+ if ((options.touchSupport === true) || (options.touchSupport === false)) return options.touchSupport;
425
+ else return hasTouchSupport();
426
+ }
427
+ return {
428
+ createDragger: function (ord) {
429
+ return function (e) {
430
+ e.pageX = e.originalEvent.changedTouches[0].pageX;
431
+ e.pageY = e.originalEvent.changedTouches[0].pageY;
432
+ if (options.disabled) {
433
+ return false;
434
+ }
435
+ if ((ord === 'move') && !options.allowMove) {
436
+ return false;
437
+ }
438
+ btndown = true;
439
+ startDragMode(ord, mouseAbs(e));
440
+ e.stopPropagation();
441
+ e.preventDefault();
442
+ return false;
443
+ };
444
+ },
445
+ newSelection: function (e) {
446
+ e.pageX = e.originalEvent.changedTouches[0].pageX;
447
+ e.pageY = e.originalEvent.changedTouches[0].pageY;
448
+ return newSelection(e);
449
+ },
450
+ isSupported: hasTouchSupport,
451
+ support: detectSupport()
452
+ };
453
+ }());
454
+ // }}}
455
+ // Coords Module {{{
456
+ var Coords = (function () {
457
+ var x1 = 0,
458
+ y1 = 0,
459
+ x2 = 0,
460
+ y2 = 0,
461
+ ox, oy;
462
+
463
+ function setPressed(pos) //{{{
464
+ {
465
+ pos = rebound(pos);
466
+ x2 = x1 = pos[0];
467
+ y2 = y1 = pos[1];
468
+ }
469
+ //}}}
470
+ function setCurrent(pos) //{{{
471
+ {
472
+ pos = rebound(pos);
473
+ ox = pos[0] - x2;
474
+ oy = pos[1] - y2;
475
+ x2 = pos[0];
476
+ y2 = pos[1];
477
+ }
478
+ //}}}
479
+ function getOffset() //{{{
480
+ {
481
+ return [ox, oy];
482
+ }
483
+ //}}}
484
+ function moveOffset(offset) //{{{
485
+ {
486
+ var ox = offset[0],
487
+ oy = offset[1];
488
+
489
+ if (0 > x1 + ox) {
490
+ ox -= ox + x1;
491
+ }
492
+ if (0 > y1 + oy) {
493
+ oy -= oy + y1;
494
+ }
495
+
496
+ if (boundy < y2 + oy) {
497
+ oy += boundy - (y2 + oy);
498
+ }
499
+ if (boundx < x2 + ox) {
500
+ ox += boundx - (x2 + ox);
501
+ }
502
+
503
+ x1 += ox;
504
+ x2 += ox;
505
+ y1 += oy;
506
+ y2 += oy;
507
+ }
508
+ //}}}
509
+ function getCorner(ord) //{{{
510
+ {
511
+ var c = getFixed();
512
+ switch (ord) {
513
+ case 'ne':
514
+ return [c.x2, c.y];
515
+ case 'nw':
516
+ return [c.x, c.y];
517
+ case 'se':
518
+ return [c.x2, c.y2];
519
+ case 'sw':
520
+ return [c.x, c.y2];
521
+ }
522
+ }
523
+ //}}}
524
+ function getFixed() //{{{
525
+ {
526
+ if (!options.aspectRatio) {
527
+ return getRect();
528
+ }
529
+ // This function could use some optimization I think...
530
+ var aspect = options.aspectRatio,
531
+ min_x = options.minSize[0] / xscale,
532
+
533
+
534
+ //min_y = options.minSize[1]/yscale,
535
+ max_x = options.maxSize[0] / xscale,
536
+ max_y = options.maxSize[1] / yscale,
537
+ rw = x2 - x1,
538
+ rh = y2 - y1,
539
+ rwa = Math.abs(rw),
540
+ rha = Math.abs(rh),
541
+ real_ratio = rwa / rha,
542
+ xx, yy, w, h;
543
+
544
+ if (max_x === 0) {
545
+ max_x = boundx * 10;
546
+ }
547
+ if (max_y === 0) {
548
+ max_y = boundy * 10;
549
+ }
550
+ if (real_ratio < aspect) {
551
+ yy = y2;
552
+ w = rha * aspect;
553
+ xx = rw < 0 ? x1 - w : w + x1;
554
+
555
+ if (xx < 0) {
556
+ xx = 0;
557
+ h = Math.abs((xx - x1) / aspect);
558
+ yy = rh < 0 ? y1 - h : h + y1;
559
+ } else if (xx > boundx) {
560
+ xx = boundx;
561
+ h = Math.abs((xx - x1) / aspect);
562
+ yy = rh < 0 ? y1 - h : h + y1;
563
+ }
564
+ } else {
565
+ xx = x2;
566
+ h = rwa / aspect;
567
+ yy = rh < 0 ? y1 - h : y1 + h;
568
+ if (yy < 0) {
569
+ yy = 0;
570
+ w = Math.abs((yy - y1) * aspect);
571
+ xx = rw < 0 ? x1 - w : w + x1;
572
+ } else if (yy > boundy) {
573
+ yy = boundy;
574
+ w = Math.abs(yy - y1) * aspect;
575
+ xx = rw < 0 ? x1 - w : w + x1;
576
+ }
577
+ }
578
+
579
+ // Magic %-)
580
+ if (xx > x1) { // right side
581
+ if (xx - x1 < min_x) {
582
+ xx = x1 + min_x;
583
+ } else if (xx - x1 > max_x) {
584
+ xx = x1 + max_x;
585
+ }
586
+ if (yy > y1) {
587
+ yy = y1 + (xx - x1) / aspect;
588
+ } else {
589
+ yy = y1 - (xx - x1) / aspect;
590
+ }
591
+ } else if (xx < x1) { // left side
592
+ if (x1 - xx < min_x) {
593
+ xx = x1 - min_x;
594
+ } else if (x1 - xx > max_x) {
595
+ xx = x1 - max_x;
596
+ }
597
+ if (yy > y1) {
598
+ yy = y1 + (x1 - xx) / aspect;
599
+ } else {
600
+ yy = y1 - (x1 - xx) / aspect;
601
+ }
602
+ }
603
+
604
+ if (xx < 0) {
605
+ x1 -= xx;
606
+ xx = 0;
607
+ } else if (xx > boundx) {
608
+ x1 -= xx - boundx;
609
+ xx = boundx;
610
+ }
611
+
612
+ if (yy < 0) {
613
+ y1 -= yy;
614
+ yy = 0;
615
+ } else if (yy > boundy) {
616
+ y1 -= yy - boundy;
617
+ yy = boundy;
618
+ }
619
+
620
+ return makeObj(flipCoords(x1, y1, xx, yy));
621
+ }
622
+ //}}}
623
+ function rebound(p) //{{{
624
+ {
625
+ if (p[0] < 0) {
626
+ p[0] = 0;
627
+ }
628
+ if (p[1] < 0) {
629
+ p[1] = 0;
630
+ }
631
+
632
+ if (p[0] > boundx) {
633
+ p[0] = boundx;
634
+ }
635
+ if (p[1] > boundy) {
636
+ p[1] = boundy;
637
+ }
638
+
639
+ return [p[0], p[1]];
640
+ }
641
+ //}}}
642
+ function flipCoords(x1, y1, x2, y2) //{{{
643
+ {
644
+ var xa = x1,
645
+ xb = x2,
646
+ ya = y1,
647
+ yb = y2;
648
+ if (x2 < x1) {
649
+ xa = x2;
650
+ xb = x1;
651
+ }
652
+ if (y2 < y1) {
653
+ ya = y2;
654
+ yb = y1;
655
+ }
656
+ return [xa, ya, xb, yb];
657
+ }
658
+ //}}}
659
+ function getRect() //{{{
660
+ {
661
+ var xsize = x2 - x1,
662
+ ysize = y2 - y1,
663
+ delta;
664
+
665
+ if (xlimit && (Math.abs(xsize) > xlimit)) {
666
+ x2 = (xsize > 0) ? (x1 + xlimit) : (x1 - xlimit);
667
+ }
668
+ if (ylimit && (Math.abs(ysize) > ylimit)) {
669
+ y2 = (ysize > 0) ? (y1 + ylimit) : (y1 - ylimit);
670
+ }
671
+
672
+ if (ymin / yscale && (Math.abs(ysize) < ymin / yscale)) {
673
+ y2 = (ysize > 0) ? (y1 + ymin / yscale) : (y1 - ymin / yscale);
674
+ }
675
+ if (xmin / xscale && (Math.abs(xsize) < xmin / xscale)) {
676
+ x2 = (xsize > 0) ? (x1 + xmin / xscale) : (x1 - xmin / xscale);
677
+ }
678
+
679
+ if (x1 < 0) {
680
+ x2 -= x1;
681
+ x1 -= x1;
682
+ }
683
+ if (y1 < 0) {
684
+ y2 -= y1;
685
+ y1 -= y1;
686
+ }
687
+ if (x2 < 0) {
688
+ x1 -= x2;
689
+ x2 -= x2;
690
+ }
691
+ if (y2 < 0) {
692
+ y1 -= y2;
693
+ y2 -= y2;
694
+ }
695
+ if (x2 > boundx) {
696
+ delta = x2 - boundx;
697
+ x1 -= delta;
698
+ x2 -= delta;
699
+ }
700
+ if (y2 > boundy) {
701
+ delta = y2 - boundy;
702
+ y1 -= delta;
703
+ y2 -= delta;
704
+ }
705
+ if (x1 > boundx) {
706
+ delta = x1 - boundy;
707
+ y2 -= delta;
708
+ y1 -= delta;
709
+ }
710
+ if (y1 > boundy) {
711
+ delta = y1 - boundy;
712
+ y2 -= delta;
713
+ y1 -= delta;
714
+ }
715
+
716
+ return makeObj(flipCoords(x1, y1, x2, y2));
717
+ }
718
+ //}}}
719
+ function makeObj(a) //{{{
720
+ {
721
+ return {
722
+ x: a[0],
723
+ y: a[1],
724
+ x2: a[2],
725
+ y2: a[3],
726
+ w: a[2] - a[0],
727
+ h: a[3] - a[1]
728
+ };
729
+ }
730
+ //}}}
731
+
732
+ return {
733
+ flipCoords: flipCoords,
734
+ setPressed: setPressed,
735
+ setCurrent: setCurrent,
736
+ getOffset: getOffset,
737
+ moveOffset: moveOffset,
738
+ getCorner: getCorner,
739
+ getFixed: getFixed
740
+ };
741
+ }());
742
+
743
+ //}}}
744
+ // Shade Module {{{
745
+ var Shade = (function() {
746
+ var enabled = false,
747
+ holder = $('<div />').css({
748
+ position: 'absolute',
749
+ zIndex: 240,
750
+ opacity: 0
751
+ }),
752
+ shades = {
753
+ top: createShade(),
754
+ left: createShade().height(boundy),
755
+ right: createShade().height(boundy),
756
+ bottom: createShade()
757
+ };
758
+
759
+ function resizeShades(w,h) {
760
+ shades.left.css({ height: px(h) });
761
+ shades.right.css({ height: px(h) });
762
+ }
763
+ function updateAuto()
764
+ {
765
+ return updateShade(Coords.getFixed());
766
+ }
767
+ function updateShade(c)
768
+ {
769
+ shades.top.css({
770
+ left: px(c.x),
771
+ width: px(c.w),
772
+ height: px(c.y)
773
+ });
774
+ shades.bottom.css({
775
+ top: px(c.y2),
776
+ left: px(c.x),
777
+ width: px(c.w),
778
+ height: px(boundy-c.y2)
779
+ });
780
+ shades.right.css({
781
+ left: px(c.x2),
782
+ width: px(boundx-c.x2)
783
+ });
784
+ shades.left.css({
785
+ width: px(c.x)
786
+ });
787
+ }
788
+ function createShade() {
789
+ return $('<div />').css({
790
+ position: 'absolute',
791
+ backgroundColor: options.shadeColor||options.bgColor
792
+ }).appendTo(holder);
793
+ }
794
+ function enableShade() {
795
+ if (!enabled) {
796
+ enabled = true;
797
+ holder.insertBefore($img);
798
+ updateAuto();
799
+ Selection.setBgOpacity(1,0,1);
800
+ $img2.hide();
801
+
802
+ setBgColor(options.shadeColor||options.bgColor,1);
803
+ if (Selection.isAwake())
804
+ {
805
+ setOpacity(options.bgOpacity,1);
806
+ }
807
+ else setOpacity(1,1);
808
+ }
809
+ }
810
+ function setBgColor(color,now) {
811
+ colorChangeMacro(getShades(),color,now);
812
+ }
813
+ function disableShade() {
814
+ if (enabled) {
815
+ holder.remove();
816
+ $img2.show();
817
+ enabled = false;
818
+ if (Selection.isAwake()) {
819
+ Selection.setBgOpacity(options.bgOpacity,1,1);
820
+ } else {
821
+ Selection.setBgOpacity(1,1,1);
822
+ Selection.disableHandles();
823
+ }
824
+ colorChangeMacro($div,0,1);
825
+ }
826
+ }
827
+ function setOpacity(opacity,now) {
828
+ if (enabled) {
829
+ if (options.bgFade && !now) {
830
+ holder.animate({
831
+ opacity: 1-opacity
832
+ },{
833
+ queue: false,
834
+ duration: options.fadeTime
835
+ });
836
+ }
837
+ else holder.css({opacity:1-opacity});
838
+ }
839
+ }
840
+ function refreshAll() {
841
+ options.shade ? enableShade() : disableShade();
842
+ if (Selection.isAwake()) setOpacity(options.bgOpacity);
843
+ }
844
+ function getShades() {
845
+ return holder.children();
846
+ }
847
+
848
+ return {
849
+ update: updateAuto,
850
+ updateRaw: updateShade,
851
+ getShades: getShades,
852
+ setBgColor: setBgColor,
853
+ enable: enableShade,
854
+ disable: disableShade,
855
+ resize: resizeShades,
856
+ refresh: refreshAll,
857
+ opacity: setOpacity
858
+ };
859
+ }());
860
+ // }}}
861
+ // Selection Module {{{
862
+ var Selection = (function () {
863
+ var awake,
864
+ hdep = 370,
865
+ borders = {},
866
+ handle = {},
867
+ dragbar = {},
868
+ seehandles = false;
869
+
870
+ // Private Methods
871
+ function insertBorder(type) //{{{
872
+ {
873
+ var jq = $('<div />').css({
874
+ position: 'absolute',
875
+ opacity: options.borderOpacity
876
+ }).addClass(cssClass(type));
877
+ $img_holder.append(jq);
878
+ return jq;
879
+ }
880
+ //}}}
881
+ function dragDiv(ord, zi) //{{{
882
+ {
883
+ var jq = $('<div />').mousedown(createDragger(ord)).css({
884
+ cursor: ord + '-resize',
885
+ position: 'absolute',
886
+ zIndex: zi
887
+ }).addClass('ord-'+ord);
888
+
889
+ if (Touch.support) {
890
+ jq.bind('touchstart.jcrop', Touch.createDragger(ord));
891
+ }
892
+
893
+ $hdl_holder.append(jq);
894
+ return jq;
895
+ }
896
+ //}}}
897
+ function insertHandle(ord) //{{{
898
+ {
899
+ var hs = options.handleSize;
900
+ return dragDiv(ord, hdep++).css({
901
+ opacity: options.handleOpacity
902
+ }).width(hs).height(hs).addClass(cssClass('handle'));
903
+ }
904
+ //}}}
905
+ function insertDragbar(ord) //{{{
906
+ {
907
+ return dragDiv(ord, hdep++).addClass('jcrop-dragbar');
908
+ }
909
+ //}}}
910
+ function createDragbars(li) //{{{
911
+ {
912
+ var i;
913
+ for (i = 0; i < li.length; i++) {
914
+ dragbar[li[i]] = insertDragbar(li[i]);
915
+ }
916
+ }
917
+ //}}}
918
+ function createBorders(li) //{{{
919
+ {
920
+ var cl,i;
921
+ for (i = 0; i < li.length; i++) {
922
+ switch(li[i]){
923
+ case'n': cl='hline'; break;
924
+ case's': cl='hline bottom'; break;
925
+ case'e': cl='vline right'; break;
926
+ case'w': cl='vline'; break;
927
+ }
928
+ borders[li[i]] = insertBorder(cl);
929
+ }
930
+ }
931
+ //}}}
932
+ function createHandles(li) //{{{
933
+ {
934
+ var i;
935
+ for (i = 0; i < li.length; i++) {
936
+ handle[li[i]] = insertHandle(li[i]);
937
+ }
938
+ }
939
+ //}}}
940
+ function moveto(x, y) //{{{
941
+ {
942
+ if (!options.shade) {
943
+ $img2.css({
944
+ top: px(-y),
945
+ left: px(-x)
946
+ });
947
+ }
948
+ $sel.css({
949
+ top: px(y),
950
+ left: px(x)
951
+ });
952
+ }
953
+ //}}}
954
+ function resize(w, h) //{{{
955
+ {
956
+ $sel.width(Math.round(w)).height(Math.round(h));
957
+ }
958
+ //}}}
959
+ function refresh() //{{{
960
+ {
961
+ var c = Coords.getFixed();
962
+
963
+ Coords.setPressed([c.x, c.y]);
964
+ Coords.setCurrent([c.x2, c.y2]);
965
+
966
+ updateVisible();
967
+ }
968
+ //}}}
969
+
970
+ // Internal Methods
971
+ function updateVisible(select) //{{{
972
+ {
973
+ if (awake) {
974
+ return update(select);
975
+ }
976
+ }
977
+ //}}}
978
+ function update(select) //{{{
979
+ {
980
+ var c = Coords.getFixed();
981
+
982
+ resize(c.w, c.h);
983
+ moveto(c.x, c.y);
984
+ if (options.shade) Shade.updateRaw(c);
985
+
986
+ awake || show();
987
+
988
+ if (select) {
989
+ options.onSelect.call(api, unscale(c));
990
+ } else {
991
+ options.onChange.call(api, unscale(c));
992
+ }
993
+ }
994
+ //}}}
995
+ function setBgOpacity(opacity,force,now) //{{{
996
+ {
997
+ if (!awake && !force) return;
998
+ if (options.bgFade && !now) {
999
+ $img.animate({
1000
+ opacity: opacity
1001
+ },{
1002
+ queue: false,
1003
+ duration: options.fadeTime
1004
+ });
1005
+ } else {
1006
+ $img.css('opacity', opacity);
1007
+ }
1008
+ }
1009
+ //}}}
1010
+ function show() //{{{
1011
+ {
1012
+ $sel.show();
1013
+
1014
+ if (options.shade) Shade.opacity(bgopacity);
1015
+ else setBgOpacity(bgopacity,true);
1016
+
1017
+ awake = true;
1018
+ }
1019
+ //}}}
1020
+ function release() //{{{
1021
+ {
1022
+ disableHandles();
1023
+ $sel.hide();
1024
+
1025
+ if (options.shade) Shade.opacity(1);
1026
+ else setBgOpacity(1);
1027
+
1028
+ awake = false;
1029
+ options.onRelease.call(api);
1030
+ }
1031
+ //}}}
1032
+ function showHandles() //{{{
1033
+ {
1034
+ if (seehandles) {
1035
+ $hdl_holder.show();
1036
+ }
1037
+ }
1038
+ //}}}
1039
+ function enableHandles() //{{{
1040
+ {
1041
+ seehandles = true;
1042
+ if (options.allowResize) {
1043
+ $hdl_holder.show();
1044
+ return true;
1045
+ }
1046
+ }
1047
+ //}}}
1048
+ function disableHandles() //{{{
1049
+ {
1050
+ seehandles = false;
1051
+ $hdl_holder.hide();
1052
+ }
1053
+ //}}}
1054
+ function animMode(v) //{{{
1055
+ {
1056
+ if (v) {
1057
+ animating = true;
1058
+ disableHandles();
1059
+ } else {
1060
+ animating = false;
1061
+ enableHandles();
1062
+ }
1063
+ }
1064
+ //}}}
1065
+ function done() //{{{
1066
+ {
1067
+ animMode(false);
1068
+ refresh();
1069
+ }
1070
+ //}}}
1071
+ // Insert draggable elements {{{
1072
+ // Insert border divs for outline
1073
+
1074
+ if (options.dragEdges && $.isArray(options.createDragbars))
1075
+ createDragbars(options.createDragbars);
1076
+
1077
+ if ($.isArray(options.createHandles))
1078
+ createHandles(options.createHandles);
1079
+
1080
+ if (options.drawBorders && $.isArray(options.createBorders))
1081
+ createBorders(options.createBorders);
1082
+
1083
+ //}}}
1084
+
1085
+ // This is a hack for iOS5 to support drag/move touch functionality
1086
+ $(document).bind('touchstart.jcrop-ios',function(e) {
1087
+ if ($(e.currentTarget).hasClass('jcrop-tracker')) e.stopPropagation();
1088
+ });
1089
+
1090
+ var $track = newTracker().mousedown(createDragger('move')).css({
1091
+ cursor: 'move',
1092
+ position: 'absolute',
1093
+ zIndex: 360
1094
+ });
1095
+
1096
+ if (Touch.support) {
1097
+ $track.bind('touchstart.jcrop', Touch.createDragger('move'));
1098
+ }
1099
+
1100
+ $img_holder.append($track);
1101
+ disableHandles();
1102
+
1103
+ return {
1104
+ updateVisible: updateVisible,
1105
+ update: update,
1106
+ release: release,
1107
+ refresh: refresh,
1108
+ isAwake: function () {
1109
+ return awake;
1110
+ },
1111
+ setCursor: function (cursor) {
1112
+ $track.css('cursor', cursor);
1113
+ },
1114
+ enableHandles: enableHandles,
1115
+ enableOnly: function () {
1116
+ seehandles = true;
1117
+ },
1118
+ showHandles: showHandles,
1119
+ disableHandles: disableHandles,
1120
+ animMode: animMode,
1121
+ setBgOpacity: setBgOpacity,
1122
+ done: done
1123
+ };
1124
+ }());
1125
+
1126
+ //}}}
1127
+ // Tracker Module {{{
1128
+ var Tracker = (function () {
1129
+ var onMove = function () {},
1130
+ onDone = function () {},
1131
+ trackDoc = options.trackDocument;
1132
+
1133
+ function toFront() //{{{
1134
+ {
1135
+ $trk.css({
1136
+ zIndex: 450
1137
+ });
1138
+ if (Touch.support) {
1139
+ $(document)
1140
+ .bind('touchmove.jcrop', trackTouchMove)
1141
+ .bind('touchend.jcrop', trackTouchEnd);
1142
+ }
1143
+ if (trackDoc) {
1144
+ $(document)
1145
+ .bind('mousemove.jcrop',trackMove)
1146
+ .bind('mouseup.jcrop',trackUp);
1147
+ }
1148
+ }
1149
+ //}}}
1150
+ function toBack() //{{{
1151
+ {
1152
+ $trk.css({
1153
+ zIndex: 290
1154
+ });
1155
+ $(document).unbind('.jcrop');
1156
+ }
1157
+ //}}}
1158
+ function trackMove(e) //{{{
1159
+ {
1160
+ onMove(mouseAbs(e));
1161
+ return false;
1162
+ }
1163
+ //}}}
1164
+ function trackUp(e) //{{{
1165
+ {
1166
+ e.preventDefault();
1167
+ e.stopPropagation();
1168
+
1169
+ if (btndown) {
1170
+ btndown = false;
1171
+
1172
+ onDone(mouseAbs(e));
1173
+
1174
+ if (Selection.isAwake()) {
1175
+ options.onSelect.call(api, unscale(Coords.getFixed()));
1176
+ }
1177
+
1178
+ toBack();
1179
+ onMove = function () {};
1180
+ onDone = function () {};
1181
+ }
1182
+
1183
+ return false;
1184
+ }
1185
+ //}}}
1186
+ function activateHandlers(move, done) //{{{
1187
+ {
1188
+ btndown = true;
1189
+ onMove = move;
1190
+ onDone = done;
1191
+ toFront();
1192
+ return false;
1193
+ }
1194
+ //}}}
1195
+ function trackTouchMove(e) //{{{
1196
+ {
1197
+ e.pageX = e.originalEvent.changedTouches[0].pageX;
1198
+ e.pageY = e.originalEvent.changedTouches[0].pageY;
1199
+ return trackMove(e);
1200
+ }
1201
+ //}}}
1202
+ function trackTouchEnd(e) //{{{
1203
+ {
1204
+ e.pageX = e.originalEvent.changedTouches[0].pageX;
1205
+ e.pageY = e.originalEvent.changedTouches[0].pageY;
1206
+ return trackUp(e);
1207
+ }
1208
+ //}}}
1209
+ function setCursor(t) //{{{
1210
+ {
1211
+ $trk.css('cursor', t);
1212
+ }
1213
+ //}}}
1214
+
1215
+ if (!trackDoc) {
1216
+ $trk.mousemove(trackMove).mouseup(trackUp).mouseout(trackUp);
1217
+ }
1218
+
1219
+ $img.before($trk);
1220
+ return {
1221
+ activateHandlers: activateHandlers,
1222
+ setCursor: setCursor
1223
+ };
1224
+ }());
1225
+ //}}}
1226
+ // KeyManager Module {{{
1227
+ var KeyManager = (function () {
1228
+ var $keymgr = $('<input type="radio" />').css({
1229
+ position: 'fixed',
1230
+ left: '-120px',
1231
+ width: '12px'
1232
+ }).addClass('jcrop-keymgr'),
1233
+
1234
+ $keywrap = $('<div />').css({
1235
+ position: 'absolute',
1236
+ overflow: 'hidden'
1237
+ }).append($keymgr);
1238
+
1239
+ function watchKeys() //{{{
1240
+ {
1241
+ if (options.keySupport) {
1242
+ $keymgr.show();
1243
+ $keymgr.focus();
1244
+ }
1245
+ }
1246
+ //}}}
1247
+ function onBlur(e) //{{{
1248
+ {
1249
+ $keymgr.hide();
1250
+ }
1251
+ //}}}
1252
+ function doNudge(e, x, y) //{{{
1253
+ {
1254
+ if (options.allowMove) {
1255
+ Coords.moveOffset([x, y]);
1256
+ Selection.updateVisible(true);
1257
+ }
1258
+ e.preventDefault();
1259
+ e.stopPropagation();
1260
+ }
1261
+ //}}}
1262
+ function parseKey(e) //{{{
1263
+ {
1264
+ if (e.ctrlKey || e.metaKey) {
1265
+ return true;
1266
+ }
1267
+ shift_down = e.shiftKey ? true : false;
1268
+ var nudge = shift_down ? 10 : 1;
1269
+
1270
+ switch (e.keyCode) {
1271
+ case 37:
1272
+ doNudge(e, -nudge, 0);
1273
+ break;
1274
+ case 39:
1275
+ doNudge(e, nudge, 0);
1276
+ break;
1277
+ case 38:
1278
+ doNudge(e, 0, -nudge);
1279
+ break;
1280
+ case 40:
1281
+ doNudge(e, 0, nudge);
1282
+ break;
1283
+ case 27:
1284
+ if (options.allowSelect) Selection.release();
1285
+ break;
1286
+ case 9:
1287
+ return true;
1288
+ }
1289
+
1290
+ return false;
1291
+ }
1292
+ //}}}
1293
+
1294
+ if (options.keySupport) {
1295
+ $keymgr.keydown(parseKey).blur(onBlur);
1296
+ if (ie6mode || !options.fixedSupport) {
1297
+ $keymgr.css({
1298
+ position: 'absolute',
1299
+ left: '-20px'
1300
+ });
1301
+ $keywrap.append($keymgr).insertBefore($img);
1302
+ } else {
1303
+ $keymgr.insertBefore($img);
1304
+ }
1305
+ }
1306
+
1307
+
1308
+ return {
1309
+ watchKeys: watchKeys
1310
+ };
1311
+ }());
1312
+ //}}}
1313
+ // }}}
1314
+ // API methods {{{
1315
+ function setClass(cname) //{{{
1316
+ {
1317
+ $div.removeClass().addClass(cssClass('holder')).addClass(cname);
1318
+ }
1319
+ //}}}
1320
+ function animateTo(a, callback) //{{{
1321
+ {
1322
+ var x1 = a[0] / xscale,
1323
+ y1 = a[1] / yscale,
1324
+ x2 = a[2] / xscale,
1325
+ y2 = a[3] / yscale;
1326
+
1327
+ if (animating) {
1328
+ return;
1329
+ }
1330
+
1331
+ var animto = Coords.flipCoords(x1, y1, x2, y2),
1332
+ c = Coords.getFixed(),
1333
+ initcr = [c.x, c.y, c.x2, c.y2],
1334
+ animat = initcr,
1335
+ interv = options.animationDelay,
1336
+ ix1 = animto[0] - initcr[0],
1337
+ iy1 = animto[1] - initcr[1],
1338
+ ix2 = animto[2] - initcr[2],
1339
+ iy2 = animto[3] - initcr[3],
1340
+ pcent = 0,
1341
+ velocity = options.swingSpeed;
1342
+
1343
+ x1 = animat[0];
1344
+ y1 = animat[1];
1345
+ x2 = animat[2];
1346
+ y2 = animat[3];
1347
+
1348
+ Selection.animMode(true);
1349
+ var anim_timer;
1350
+
1351
+ function queueAnimator() {
1352
+ window.setTimeout(animator, interv);
1353
+ }
1354
+ var animator = (function () {
1355
+ return function () {
1356
+ pcent += (100 - pcent) / velocity;
1357
+
1358
+ animat[0] = Math.round(x1 + ((pcent / 100) * ix1));
1359
+ animat[1] = Math.round(y1 + ((pcent / 100) * iy1));
1360
+ animat[2] = Math.round(x2 + ((pcent / 100) * ix2));
1361
+ animat[3] = Math.round(y2 + ((pcent / 100) * iy2));
1362
+
1363
+ if (pcent >= 99.8) {
1364
+ pcent = 100;
1365
+ }
1366
+ if (pcent < 100) {
1367
+ setSelectRaw(animat);
1368
+ queueAnimator();
1369
+ } else {
1370
+ Selection.done();
1371
+ Selection.animMode(false);
1372
+ if (typeof(callback) === 'function') {
1373
+ callback.call(api);
1374
+ }
1375
+ }
1376
+ };
1377
+ }());
1378
+ queueAnimator();
1379
+ }
1380
+ //}}}
1381
+ function setSelect(rect) //{{{
1382
+ {
1383
+ setSelectRaw([rect[0] / xscale, rect[1] / yscale, rect[2] / xscale, rect[3] / yscale]);
1384
+ options.onSelect.call(api, unscale(Coords.getFixed()));
1385
+ Selection.enableHandles();
1386
+ }
1387
+ //}}}
1388
+ function setSelectRaw(l) //{{{
1389
+ {
1390
+ Coords.setPressed([l[0], l[1]]);
1391
+ Coords.setCurrent([l[2], l[3]]);
1392
+ Selection.update();
1393
+ }
1394
+ //}}}
1395
+ function tellSelect() //{{{
1396
+ {
1397
+ return unscale(Coords.getFixed());
1398
+ }
1399
+ //}}}
1400
+ function tellScaled() //{{{
1401
+ {
1402
+ return Coords.getFixed();
1403
+ }
1404
+ //}}}
1405
+ function setOptionsNew(opt) //{{{
1406
+ {
1407
+ setOptions(opt);
1408
+ interfaceUpdate();
1409
+ }
1410
+ //}}}
1411
+ function disableCrop() //{{{
1412
+ {
1413
+ options.disabled = true;
1414
+ Selection.disableHandles();
1415
+ Selection.setCursor('default');
1416
+ Tracker.setCursor('default');
1417
+ }
1418
+ //}}}
1419
+ function enableCrop() //{{{
1420
+ {
1421
+ options.disabled = false;
1422
+ interfaceUpdate();
1423
+ }
1424
+ //}}}
1425
+ function cancelCrop() //{{{
1426
+ {
1427
+ Selection.done();
1428
+ Tracker.activateHandlers(null, null);
1429
+ }
1430
+ //}}}
1431
+ function destroy() //{{{
1432
+ {
1433
+ $div.remove();
1434
+ $origimg.show();
1435
+ $(obj).removeData('Jcrop');
1436
+ }
1437
+ //}}}
1438
+ function setImage(src, callback) //{{{
1439
+ {
1440
+ Selection.release();
1441
+ disableCrop();
1442
+ var img = new Image();
1443
+ img.onload = function () {
1444
+ var iw = img.width;
1445
+ var ih = img.height;
1446
+ var bw = options.boxWidth;
1447
+ var bh = options.boxHeight;
1448
+ $img.width(iw).height(ih);
1449
+ $img.attr('src', src);
1450
+ $img2.attr('src', src);
1451
+ presize($img, bw, bh);
1452
+ boundx = $img.width();
1453
+ boundy = $img.height();
1454
+ $img2.width(boundx).height(boundy);
1455
+ $trk.width(boundx + (bound * 2)).height(boundy + (bound * 2));
1456
+ $div.width(boundx).height(boundy);
1457
+ Shade.resize(boundx,boundy);
1458
+ enableCrop();
1459
+
1460
+ if (typeof(callback) === 'function') {
1461
+ callback.call(api);
1462
+ }
1463
+ };
1464
+ img.src = src;
1465
+ }
1466
+ //}}}
1467
+ function colorChangeMacro($obj,color,now) {
1468
+ var mycolor = color || options.bgColor;
1469
+ if (options.bgFade && supportsColorFade() && options.fadeTime && !now) {
1470
+ $obj.animate({
1471
+ backgroundColor: mycolor
1472
+ }, {
1473
+ queue: false,
1474
+ duration: options.fadeTime
1475
+ });
1476
+ } else {
1477
+ $obj.css('backgroundColor', mycolor);
1478
+ }
1479
+ }
1480
+ function interfaceUpdate(alt) //{{{
1481
+ // This method tweaks the interface based on options object.
1482
+ // Called when options are changed and at end of initialization.
1483
+ {
1484
+ if (options.allowResize) {
1485
+ if (alt) {
1486
+ Selection.enableOnly();
1487
+ } else {
1488
+ Selection.enableHandles();
1489
+ }
1490
+ } else {
1491
+ Selection.disableHandles();
1492
+ }
1493
+
1494
+ Tracker.setCursor(options.allowSelect ? 'crosshair' : 'default');
1495
+ Selection.setCursor(options.allowMove ? 'move' : 'default');
1496
+
1497
+ if (options.hasOwnProperty('trueSize')) {
1498
+ xscale = options.trueSize[0] / boundx;
1499
+ yscale = options.trueSize[1] / boundy;
1500
+ }
1501
+
1502
+ if (options.hasOwnProperty('setSelect')) {
1503
+ setSelect(options.setSelect);
1504
+ Selection.done();
1505
+ delete(options.setSelect);
1506
+ }
1507
+
1508
+ Shade.refresh();
1509
+
1510
+ if (options.bgColor != bgcolor) {
1511
+ colorChangeMacro(
1512
+ options.shade? Shade.getShades(): $div,
1513
+ options.shade?
1514
+ (options.shadeColor || options.bgColor):
1515
+ options.bgColor
1516
+ );
1517
+ bgcolor = options.bgColor;
1518
+ }
1519
+
1520
+ if (bgopacity != options.bgOpacity) {
1521
+ bgopacity = options.bgOpacity;
1522
+ if (options.shade) Shade.refresh();
1523
+ else Selection.setBgOpacity(bgopacity);
1524
+ }
1525
+
1526
+ xlimit = options.maxSize[0] || 0;
1527
+ ylimit = options.maxSize[1] || 0;
1528
+ xmin = options.minSize[0] || 0;
1529
+ ymin = options.minSize[1] || 0;
1530
+
1531
+ if (options.hasOwnProperty('outerImage')) {
1532
+ $img.attr('src', options.outerImage);
1533
+ delete(options.outerImage);
1534
+ }
1535
+
1536
+ Selection.refresh();
1537
+ }
1538
+ //}}}
1539
+ //}}}
1540
+
1541
+ if (Touch.support) $trk.bind('touchstart.jcrop', Touch.newSelection);
1542
+
1543
+ $hdl_holder.hide();
1544
+ interfaceUpdate(true);
1545
+
1546
+ var api = {
1547
+ setImage: setImage,
1548
+ animateTo: animateTo,
1549
+ setSelect: setSelect,
1550
+ setOptions: setOptionsNew,
1551
+ tellSelect: tellSelect,
1552
+ tellScaled: tellScaled,
1553
+ setClass: setClass,
1554
+
1555
+ disable: disableCrop,
1556
+ enable: enableCrop,
1557
+ cancel: cancelCrop,
1558
+ release: Selection.release,
1559
+ destroy: destroy,
1560
+
1561
+ focus: KeyManager.watchKeys,
1562
+
1563
+ getBounds: function () {
1564
+ return [boundx * xscale, boundy * yscale];
1565
+ },
1566
+ getWidgetSize: function () {
1567
+ return [boundx, boundy];
1568
+ },
1569
+ getScaleFactor: function () {
1570
+ return [xscale, yscale];
1571
+ },
1572
+ getOptions: function() {
1573
+ // careful: internal values are returned
1574
+ return options;
1575
+ },
1576
+
1577
+ ui: {
1578
+ holder: $div,
1579
+ selection: $sel
1580
+ }
1581
+ };
1582
+
1583
+ if ($.browser.msie)
1584
+ $div.bind('selectstart', function () { return false; });
1585
+
1586
+ $origimg.data('Jcrop', api);
1587
+ return api;
1588
+ };
1589
+ $.fn.Jcrop = function (options, callback) //{{{
1590
+ {
1591
+ var api;
1592
+ // Iterate over each object, attach Jcrop
1593
+ this.each(function () {
1594
+ // If we've already attached to this object
1595
+ if ($(this).data('Jcrop')) {
1596
+ // The API can be requested this way (undocumented)
1597
+ if (options === 'api') return $(this).data('Jcrop');
1598
+ // Otherwise, we just reset the options...
1599
+ else $(this).data('Jcrop').setOptions(options);
1600
+ }
1601
+ // If we haven't been attached, preload and attach
1602
+ else {
1603
+ if (this.tagName == 'IMG')
1604
+ $.Jcrop.Loader(this,function(){
1605
+ $(this).css({display:'block',visibility:'hidden'});
1606
+ api = $.Jcrop(this, options);
1607
+ if ($.isFunction(callback)) callback.call(api);
1608
+ });
1609
+ else {
1610
+ $(this).css({display:'block',visibility:'hidden'});
1611
+ api = $.Jcrop(this, options);
1612
+ if ($.isFunction(callback)) callback.call(api);
1613
+ }
1614
+ }
1615
+ });
1616
+
1617
+ // Return "this" so the object is chainable (jQuery-style)
1618
+ return this;
1619
+ };
1620
+ //}}}
1621
+ // $.Jcrop.Loader - basic image loader {{{
1622
+
1623
+ $.Jcrop.Loader = function(imgobj,success,error){
1624
+ var $img = $(imgobj), img = $img[0];
1625
+
1626
+ function completeCheck(){
1627
+ if (img.complete) {
1628
+ $img.unbind('.jcloader');
1629
+ if ($.isFunction(success)) success.call(img);
1630
+ }
1631
+ else window.setTimeout(completeCheck,50);
1632
+ }
1633
+
1634
+ $img
1635
+ .bind('load.jcloader',completeCheck)
1636
+ .bind('error.jcloader',function(e){
1637
+ $img.unbind('.jcloader');
1638
+ if ($.isFunction(error)) error.call(img);
1639
+ });
1640
+
1641
+ if (img.complete && $.isFunction(success)){
1642
+ $img.unbind('.jcloader');
1643
+ success.call(img);
1644
+ }
1645
+ };
1646
+
1647
+ //}}}
1648
+ // Global Defaults {{{
1649
+ $.Jcrop.defaults = {
1650
+
1651
+ // Basic Settings
1652
+ allowSelect: true,
1653
+ allowMove: true,
1654
+ allowResize: true,
1655
+
1656
+ trackDocument: true,
1657
+
1658
+ // Styling Options
1659
+ baseClass: 'jcrop',
1660
+ addClass: null,
1661
+ bgColor: 'black',
1662
+ bgOpacity: 0.6,
1663
+ bgFade: false,
1664
+ borderOpacity: 0.4,
1665
+ handleOpacity: 0.5,
1666
+ handleSize: 7,
1667
+
1668
+ aspectRatio: 0,
1669
+ keySupport: true,
1670
+ createHandles: ['n','s','e','w','nw','ne','se','sw'],
1671
+ createDragbars: ['n','s','e','w'],
1672
+ createBorders: ['n','s','e','w'],
1673
+ drawBorders: true,
1674
+ dragEdges: true,
1675
+ fixedSupport: true,
1676
+ touchSupport: null,
1677
+
1678
+ shade: null,
1679
+
1680
+ boxWidth: 0,
1681
+ boxHeight: 0,
1682
+ boundary: 2,
1683
+ fadeTime: 400,
1684
+ animationDelay: 20,
1685
+ swingSpeed: 3,
1686
+
1687
+ minSelect: [0, 0],
1688
+ maxSize: [0, 0],
1689
+ minSize: [0, 0],
1690
+
1691
+ // Callbacks / Event Handlers
1692
+ onChange: function () {},
1693
+ onSelect: function () {},
1694
+ onDblClick: function () {},
1695
+ onRelease: function () {}
1696
+ };
1697
+
1698
+ // }}}
1699
+ }(jQuery));