Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
9 lars 1
var FormImageCrop = function () {
2
 
3
    var demo1 = function() {
4
        $('#demo1').Jcrop();
5
    }
6
 
7
    var demo2 = function() {
8
        var jcrop_api;
9
 
10
        $('#demo2').Jcrop({
11
          onChange:   showCoords,
12
          onSelect:   showCoords,
13
          onRelease:  clearCoords
14
        },function(){
15
          jcrop_api = this;
16
        });
17
 
18
        $('#coords').on('change','input',function(e){
19
          var x1 = $('#x1').val(),
20
              x2 = $('#x2').val(),
21
              y1 = $('#y1').val(),
22
              y2 = $('#y2').val();
23
          jcrop_api.setSelect([x1,y1,x2,y2]);
24
        });
25
 
26
        // Simple event handler, called from onChange and onSelect
27
        // event handlers, as per the Jcrop invocation above
28
        function showCoords(c)
29
        {
30
            $('#x1').val(c.x);
31
            $('#y1').val(c.y);
32
            $('#x2').val(c.x2);
33
            $('#y2').val(c.y2);
34
            $('#w').val(c.w);
35
            $('#h').val(c.h);
36
        };
37
 
38
        function clearCoords()
39
        {
40
            $('#coords input').val('');
41
        };
42
    }
43
 
44
    var demo3 = function() {
45
        // Create variables (in this scope) to hold the API and image size
46
        var jcrop_api,
47
            boundx,
48
            boundy,
49
            // Grab some information about the preview pane
50
            $preview = $('#preview-pane'),
51
            $pcnt = $('#preview-pane .preview-container'),
52
            $pimg = $('#preview-pane .preview-container img'),
53
 
54
            xsize = $pcnt.width(),
55
            ysize = $pcnt.height();
56
 
57
            console.log('init',[xsize,ysize]);
58
 
59
        $('#demo3').Jcrop({
60
          onChange: updatePreview,
61
          onSelect: updatePreview,
62
          aspectRatio: xsize / ysize
63
        },function(){
64
          // Use the API to get the real image size
65
          var bounds = this.getBounds();
66
          boundx = bounds[0];
67
          boundy = bounds[1];
68
          // Store the API in the jcrop_api variable
69
          jcrop_api = this;
70
          // Move the preview into the jcrop container for css positioning
71
          $preview.appendTo(jcrop_api.ui.holder);
72
        });
73
 
74
        function updatePreview(c)
75
        {
76
          if (parseInt(c.w) > 0)
77
          {
78
            var rx = xsize / c.w;
79
            var ry = ysize / c.h;
80
 
81
            $pimg.css({
82
              width: Math.round(rx * boundx) + 'px',
83
              height: Math.round(ry * boundy) + 'px',
84
              marginLeft: '-' + Math.round(rx * c.x) + 'px',
85
              marginTop: '-' + Math.round(ry * c.y) + 'px'
86
            });
87
          }
88
        };
89
    }
90
 
91
    var demo4 = function() {
92
        var jcrop_api;
93
 
94
        $('#demo4').Jcrop({
95
          bgFade:     true,
96
          bgOpacity: .2,
97
          setSelect: [ 60, 70, 540, 330 ]
98
        },function(){
99
          jcrop_api = this;
100
        });
101
 
102
        $('#fadetog').change(function(){
103
          jcrop_api.setOptions({
104
            bgFade: this.checked
105
          });
106
        }).attr('checked', true);
107
 
108
        $('#shadetog').change(function(){
109
          if (this.checked) $('#shadetxt').slideDown();
110
            else $('#shadetxt').slideUp();
111
          jcrop_api.setOptions({
112
            shade: this.checked
113
          });
114
        }).attr('checked', false);
115
 
116
        // Define page sections
117
        var sections = {
118
          bgc_buttons: 'Change bgColor',
119
          bgo_buttons: 'Change bgOpacity',
120
          anim_buttons: 'Animate Selection'
121
        };
122
        // Define animation buttons
123
        var ac = {
124
          anim1: [217,122,382,284],
125
          anim2: [20,20,580,380],
126
          anim3: [24,24,176,376],
127
          anim4: [347,165,550,355],
128
          anim5: [136,55,472,183]
129
        };
130
        // Define bgOpacity buttons
131
        var bgo = {
132
          Low: .2,
133
          Mid: .5,
134
          High: .8,
135
          Full: 1
136
        };
137
        // Define bgColor buttons
138
        var bgc = {
139
          R: '#900',
140
          B: '#4BB6F0',
141
          Y: '#F0B207',
142
          G: '#46B81C',
143
          W: 'white',
144
          K: 'black'
145
        };
146
        // Create fieldset targets for buttons
147
        for(i in sections)
148
          insertSection(i,sections[i]);
149
 
150
        function create_btn(c) {
151
          var $o = $('<button />').addClass('btn small');
152
          if (c) $o.append(c);
153
          return $o;
154
        }
155
 
156
        var a_count = 1;
157
        // Create animation buttons
158
        for(i in ac) {
159
          $('#anim_buttons .btn-group')
160
            .append(
161
              create_btn(a_count++).click(animHandler(ac[i])),
162
              ' '
163
            );
164
        }
165
 
166
        $('#anim_buttons .btn-group').append(
167
          create_btn('Bye!').click(function(e){
168
            $(e.target).addClass('active');
169
            jcrop_api.animateTo(
170
              [300,200,300,200],
171
              function(){
172
                this.release();
173
                $(e.target).closest('.btn-group').find('.active').removeClass('active');
174
              }
175
            );
176
            return false;
177
          })
178
        );
179
 
180
        // Create bgOpacity buttons
181
        for(i in bgo) {
182
          $('#bgo_buttons .btn-group').append(
183
            create_btn(i).click(setoptHandler('bgOpacity',bgo[i])),
184
            ' '
185
          );
186
        }
187
        // Create bgColor buttons
188
        for(i in bgc) {
189
          $('#bgc_buttons .btn-group').append(
190
            create_btn(i).css({
191
              background: bgc[i],
192
              color: ((i == 'K') || (i == 'R'))?'white':'black'
193
            }).click(setoptHandler('bgColor',bgc[i])), ' '
194
          );
195
        }
196
        // Function to insert named sections into interface
197
        function insertSection(k,v) {
198
          $('#interface').prepend(
199
            $('<fieldset></fieldset>').attr('id',k).append(
200
              $('<h4></h4>').append(v),
201
              '<div class="btn-toolbar"><div class="btn-group"></div></div>'
202
            )
203
          );
204
        };
205
        // Handler for option-setting buttons
206
        function setoptHandler(k,v) {
207
          return function(e) {
208
            $(e.target).closest('.btn-group').find('.active').removeClass('active');
209
            $(e.target).addClass('active');
210
            var opt = { };
211
            opt[k] = v;
212
            jcrop_api.setOptions(opt);
213
            return false;
214
          };
215
        };
216
        // Handler for animation buttons
217
        function animHandler(v) {
218
          return function(e) {
219
            $(e.target).addClass('active');
220
            jcrop_api.animateTo(v,function(){
221
              $(e.target).closest('.btn-group').find('.active').removeClass('active');
222
            });
223
            return false;
224
          };
225
        };
226
 
227
        $('#bgo_buttons .btn:first,#bgc_buttons .btn:last').addClass('active');
228
        $('#interface').show();
229
    }
230
 
231
    var demo5 = function() {
232
        // The variable jcrop_api will hold a reference to the
233
        // Jcrop API once Jcrop is instantiated.
234
        var jcrop_api;
235
 
236
        // In this example, since Jcrop may be attached or detached
237
        // at the whim of the user, I've wrapped the call into a function
238
        initJcrop();
239
 
240
        // The function is pretty simple
241
        function initJcrop()//{{{
242
        {
243
          // Hide any interface elements that require Jcrop
244
          // (This is for the local user interface portion.)
245
          $('.requiresjcrop').hide();
246
 
247
          // Invoke Jcrop in typical fashion
248
          $('#demo5').Jcrop({
249
            onRelease: releaseCheck
250
          },function(){
251
 
252
            jcrop_api = this;
253
            jcrop_api.animateTo([100,100,400,300]);
254
 
255
            // Setup and dipslay the interface for "enabled"
256
            $('#can_click,#can_move,#can_size').attr('checked','checked');
257
 
258
            $('#ar_lock,#size_lock,#bg_swap').attr('checked',false);
259
 
260
            $('.requiresjcrop').show();
261
 
262
          });
263
 
264
        };
265
        //}}}
266
 
267
        // Use the API to find cropping dimensions
268
        // Then generate a random selection
269
        // This function is used by setSelect and animateTo buttons
270
        // Mainly for demonstration purposes
271
        function getRandom() {
272
          var dim = jcrop_api.getBounds();
273
          return [
274
            Math.round(Math.random() * dim[0]),
275
            Math.round(Math.random() * dim[1]),
276
            Math.round(Math.random() * dim[0]),
277
            Math.round(Math.random() * dim[1])
278
          ];
279
        };
280
 
281
        // This function is bound to the onRelease handler...
282
        // In certain circumstances (such as if you set minSize
283
        // and aspectRatio together), you can inadvertently lose
284
        // the selection. This callback re-enables creating selections
285
        // in such a case. Although the need to do this is based on a
286
        // buggy behavior, it's recommended that you in some way trap
287
        // the onRelease callback if you use allowSelect: false
288
        function releaseCheck()
289
        {
290
          jcrop_api.setOptions({ allowSelect: true });
291
          $('#can_click').attr('checked',false);
292
        };
293
 
294
        // Attach interface buttons
295
        // This may appear to be a lot of code but it's simple stuff
296
        $('#setSelect').click(function(e) {
297
          // Sets a random selection
298
          jcrop_api.setSelect(getRandom());
299
        });
300
        $('#animateTo').click(function(e) {
301
          // Animates to a random selection
302
          jcrop_api.animateTo(getRandom());
303
        });
304
        $('#release').click(function(e) {
305
          // Release method clears the selection
306
          jcrop_api.release();
307
        });
308
        $('#disable').click(function(e) {
309
          // Disable Jcrop instance
310
          jcrop_api.disable();
311
          // Update the interface to reflect disabled state
312
          $('#enable').show();
313
          $('.requiresjcrop').hide();
314
        });
315
        $('#enable').click(function(e) {
316
          // Re-enable Jcrop instance
317
          jcrop_api.enable();
318
          // Update the interface to reflect enabled state
319
          $('#enable').hide();
320
          $('.requiresjcrop').show();
321
        });
322
        $('#rehook').click(function(e) {
323
          // This button is visible when Jcrop has been destroyed
324
          // It performs the re-attachment and updates the UI
325
          $('#rehook,#enable').hide();
326
          initJcrop();
327
          $('#unhook,.requiresjcrop').show();
328
          return false;
329
        });
330
        $('#unhook').click(function(e) {
331
          // Destroy Jcrop widget, restore original state
332
          jcrop_api.destroy();
333
          // Update the interface to reflect un-attached state
334
          $('#unhook,#enable,.requiresjcrop').hide();
335
          $('#rehook').show();
336
          return false;
337
        });
338
 
339
        // Hook up the three image-swapping buttons
340
        $('#img1').click(function(e) {
341
          $(this).addClass('active').closest('.btn-group')
342
            .find('button.active').not(this).removeClass('active');
343
 
344
          jcrop_api.setImage('../../assets/global/plugins/jcrop/demos/demo_files/sago.jpg');
345
          jcrop_api.setOptions({ bgOpacity: .6 });
346
          return false;
347
        });
348
        $('#img2').click(function(e) {
349
          $(this).addClass('active').closest('.btn-group')
350
            .find('button.active').not(this).removeClass('active');
351
 
352
          jcrop_api.setImage('../../assets/global/plugins/jcrop/demos/demo_files/pool.jpg');
353
          jcrop_api.setOptions({ bgOpacity: .6 });
354
          return false;
355
        });
356
        $('#img3').click(function(e) {
357
          $(this).addClass('active').closest('.btn-group')
358
            .find('button.active').not(this).removeClass('active');
359
 
360
          jcrop_api.setImage('../../assets/global/plugins/jcrop/demos/demo_files/sago.jpg',function(){
361
            this.setOptions({
362
              bgOpacity: 1,
363
              outerImage: '../../assets/global/plugins/jcrop/demos/demo_files/sagomod.jpg'
364
            });
365
            this.animateTo(getRandom());
366
          });
367
          return false;
368
        });
369
 
370
        // The checkboxes simply set options based on it's checked value
371
        // Options are changed by passing a new options object
372
 
373
        // Also, to prevent strange behavior, they are initially checked
374
        // This matches the default initial state of Jcrop
375
 
376
        $('#can_click').change(function(e) {
377
          jcrop_api.setOptions({ allowSelect: !!this.checked });
378
          jcrop_api.focus();
379
        });
380
        $('#can_move').change(function(e) {
381
          jcrop_api.setOptions({ allowMove: !!this.checked });
382
          jcrop_api.focus();
383
        });
384
        $('#can_size').change(function(e) {
385
          jcrop_api.setOptions({ allowResize: !!this.checked });
386
          jcrop_api.focus();
387
        });
388
        $('#ar_lock').change(function(e) {
389
          jcrop_api.setOptions(this.checked?
390
            { aspectRatio: 4/3 }: { aspectRatio: 0 });
391
          jcrop_api.focus();
392
        });
393
        $('#size_lock').change(function(e) {
394
          jcrop_api.setOptions(this.checked? {
395
            minSize: [ 80, 80 ],
396
            maxSize: [ 350, 350 ]
397
          }: {
398
            minSize: [ 0, 0 ],
399
            maxSize: [ 0, 0 ]
400
          });
401
          jcrop_api.focus();
402
        });
403
 
404
    }
405
 
406
    var demo6 = function() {
407
        var api;
408
 
409
        $('#demo6').Jcrop({
410
          // start off with jcrop-light class
411
          bgOpacity: 0.5,
412
          bgColor: 'white',
413
          addClass: 'jcrop-light'
414
        },function(){
415
          api = this;
416
          api.setSelect([130,65,130+350,65+285]);
417
          api.setOptions({ bgFade: true });
418
          api.ui.selection.addClass('jcrop-selection');
419
        });
420
 
421
        $('#buttonbar').on('click','button',function(e){
422
          var $t = $(this), $g = $t.closest('.btn-group');
423
          $g.find('button.active').removeClass('active');
424
          $t.addClass('active');
425
          $g.find('[data-setclass]').each(function(){
426
            var $th = $(this), c = $th.data('setclass'),
427
              a = $th.hasClass('active');
428
            if (a) {
429
              api.ui.holder.addClass(c);
430
              switch(c){
431
                case 'jcrop-light':
432
                  api.setOptions({ bgColor: 'white', bgOpacity: 0.5 });
433
                  break;
434
 
435
                case 'jcrop-dark':
436
                  api.setOptions({ bgColor: 'black', bgOpacity: 0.4 });
437
                  break;
438
 
439
                case 'jcrop-normal':
440
                  api.setOptions({
441
                    bgColor: $.Jcrop.defaults.bgColor,
442
                    bgOpacity: $.Jcrop.defaults.bgOpacity
443
                  });
444
                  break;
445
              }
446
            }
447
            else api.ui.holder.removeClass(c);
448
          });
449
        });
450
    }
451
 
452
    var demo7 = function() {
453
        // I did JSON.stringify(jcrop_api.tellSelect()) on a crop I liked:
454
        var c = {"x":13,"y":7,"x2":487,"y2":107,"w":474,"h":100};
455
 
456
        $('#demo7').Jcrop({
457
          bgFade: true,
458
          setSelect: [c.x,c.y,c.x2,c.y2]
459
        });
460
    }
461
 
462
    var demo8 = function() {
463
        $('#demo8').Jcrop({
464
          aspectRatio: 1,
465
          onSelect: updateCoords
466
        });
467
 
468
        function updateCoords(c)
469
          {
470
            $('#crop_x').val(c.x);
471
            $('#crop_y').val(c.y);
472
            $('#crop_w').val(c.w);
473
            $('#crop_h').val(c.h);
474
          };
475
 
476
          $('#demo8_form').submit(function(){
477
            if (parseInt($('#crop_w').val())) return true;
478
            alert('Please select a crop region then press submit.');
479
            return false;
480
            });
481
 
482
    }
483
 
484
    var handleResponsive = function() {
485
      if ($(window).width() <= 1024 && $(window).width() >= 678) {
486
        $('.responsive-1024').each(function(){
487
          $(this).attr("data-class", $(this).attr("class"));
488
          $(this).attr("class", 'responsive-1024 col-md-12');
489
        });
490
      } else {
491
        $('.responsive-1024').each(function(){
492
          if ($(this).attr("data-class")) {
493
            $(this).attr("class", $(this).attr("data-class"));
494
            $(this).removeAttr("data-class");
495
          }
496
        });
497
      }
498
    }
499
 
500
    return {
501
        //main function to initiate the module
502
        init: function () {
503
 
504
            if (!jQuery().Jcrop) {;
505
                return;
506
            }
507
 
508
            App.addResizeHandler(handleResponsive);
509
            handleResponsive();
510
 
511
            demo1();
512
            demo2();
513
            demo3();
514
            demo4();
515
            demo5();
516
            demo6();
517
            demo7();
518
            demo8();
519
        }
520
 
521
    };
522
 
523
}();
524
 
525
jQuery(document).ready(function() {
526
    FormImageCrop.init();
527
});