Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

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