Dashboard sipadu mbip
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

croper.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. 'use strict';
  2. $(document).ready(function() {
  3. var console = window.console || { log: function() {} };
  4. var URL = window.URL || window.webkitURL;
  5. var $image = $('#image');
  6. var $download = $('#download');
  7. var $dataX = $('#dataX');
  8. var $dataY = $('#dataY');
  9. var $dataHeight = $('#dataHeight');
  10. var $dataWidth = $('#dataWidth');
  11. var $dataRotate = $('#dataRotate');
  12. var $dataScaleX = $('#dataScaleX');
  13. var $dataScaleY = $('#dataScaleY');
  14. var options = {
  15. aspectRatio: 16 / 9,
  16. preview: '.img-preview',
  17. crop: function(e) {
  18. $dataX.val(Math.round(e.x));
  19. $dataY.val(Math.round(e.y));
  20. $dataHeight.val(Math.round(e.height));
  21. $dataWidth.val(Math.round(e.width));
  22. $dataRotate.val(e.rotate);
  23. $dataScaleX.val(e.scaleX);
  24. $dataScaleY.val(e.scaleY);
  25. }
  26. };
  27. var originalImageURL = $image.attr('src');
  28. var uploadedImageURL;
  29. // Cropper
  30. $image.on({
  31. 'build.cropper': function(e) {
  32. /* console.log(e.type);*/
  33. },
  34. 'built.cropper': function(e) {
  35. /* console.log(e.type);*/
  36. },
  37. 'cropstart.cropper': function(e) {
  38. /* console.log(e.type, e.action);*/
  39. },
  40. 'cropmove.cropper': function(e) {
  41. /* console.log(e.type, e.action);*/
  42. },
  43. 'cropend.cropper': function(e) {
  44. /*console.log(e.type, e.action);*/
  45. },
  46. 'crop.cropper': function(e) {
  47. /* console.log(e.type, e.x, e.y, e.width, e.height, e.rotate, e.scaleX, e.scaleY);*/
  48. },
  49. 'zoom.cropper': function(e) {
  50. /* console.log(e.type, e.ratio);*/
  51. }
  52. }).cropper(options);
  53. // Buttons
  54. if (!$.isFunction(document.createElement('canvas').getContext)) {
  55. $('button[data-method="getCroppedCanvas"]').prop('disabled', true);
  56. }
  57. if (typeof document.createElement('cropper').style.transition === 'undefined') {
  58. $('button[data-method="rotate"]').prop('disabled', true);
  59. $('button[data-method="scale"]').prop('disabled', true);
  60. }
  61. // Download
  62. if (typeof $download[0].download === 'undefined') {
  63. $download.addClass('disabled');
  64. }
  65. // Options
  66. $('.docs-toggles').on('change', 'input', function() {
  67. var $this = $(this);
  68. var name = $this.attr('name');
  69. var type = $this.prop('type');
  70. var cropBoxData;
  71. var canvasData;
  72. if (!$image.data('cropper')) {
  73. return;
  74. }
  75. if (type === 'checkbox') {
  76. options[name] = $this.prop('checked');
  77. cropBoxData = $image.cropper('getCropBoxData');
  78. canvasData = $image.cropper('getCanvasData');
  79. options.built = function() {
  80. $image.cropper('setCropBoxData', cropBoxData);
  81. $image.cropper('setCanvasData', canvasData);
  82. };
  83. } else if (type === 'radio') {
  84. options[name] = $this.val();
  85. }
  86. $image.cropper('destroy').cropper(options);
  87. });
  88. // Methods
  89. $('.docs-buttons').on('click', '[data-method]', function() {
  90. var $this = $(this);
  91. var data = $this.data();
  92. var $target;
  93. var result;
  94. if ($this.prop('disabled') || $this.hasClass('disabled')) {
  95. return;
  96. }
  97. if ($image.data('cropper') && data.method) {
  98. data = $.extend({}, data); // Clone a new one
  99. if (typeof data.target !== 'undefined') {
  100. $target = $(data.target);
  101. if (typeof data.option === 'undefined') {
  102. try {
  103. data.option = JSON.parse($target.val());
  104. } catch (e) {
  105. console.log(e.message);
  106. }
  107. }
  108. }
  109. if (data.method === 'rotate') {
  110. $image.cropper('clear');
  111. }
  112. result = $image.cropper(data.method, data.option, data.secondOption);
  113. if (data.method === 'rotate') {
  114. $image.cropper('crop');
  115. }
  116. switch (data.method) {
  117. case 'scaleX':
  118. case 'scaleY':
  119. $(this).data('option', -data.option);
  120. break;
  121. case 'getCroppedCanvas':
  122. if (result) {
  123. // Bootstrap's Modal
  124. $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
  125. if (!$download.hasClass('disabled')) {
  126. $download.attr('href', result.toDataURL('image/jpeg'));
  127. }
  128. }
  129. break;
  130. case 'destroy':
  131. if (uploadedImageURL) {
  132. URL.revokeObjectURL(uploadedImageURL);
  133. uploadedImageURL = '';
  134. $image.attr('src', originalImageURL);
  135. }
  136. break;
  137. }
  138. if ($.isPlainObject(result) && $target) {
  139. try {
  140. $target.val(JSON.stringify(result));
  141. } catch (e) {
  142. console.log(e.message);
  143. }
  144. }
  145. }
  146. });
  147. // Keyboard
  148. $(document.body).on('keydown', function(e) {
  149. if (!$image.data('cropper') || this.scrollTop > 300) {
  150. return;
  151. }
  152. switch (e.which) {
  153. case 37:
  154. e.preventDefault();
  155. $image.cropper('move', -1, 0);
  156. break;
  157. case 38:
  158. e.preventDefault();
  159. $image.cropper('move', 0, -1);
  160. break;
  161. case 39:
  162. e.preventDefault();
  163. $image.cropper('move', 1, 0);
  164. break;
  165. case 40:
  166. e.preventDefault();
  167. $image.cropper('move', 0, 1);
  168. break;
  169. }
  170. });
  171. // Import image
  172. var $inputImage = $('#inputImage');
  173. if (URL) {
  174. $inputImage.change(function() {
  175. var files = this.files;
  176. var file;
  177. if (!$image.data('cropper')) {
  178. return;
  179. }
  180. if (files && files.length) {
  181. file = files[0];
  182. if (/^image\/\w+$/.test(file.type)) {
  183. if (uploadedImageURL) {
  184. URL.revokeObjectURL(uploadedImageURL);
  185. }
  186. uploadedImageURL = URL.createObjectURL(file);
  187. $image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);
  188. $inputImage.val('');
  189. } else {
  190. window.alert('Please choose an image file.');
  191. }
  192. }
  193. });
  194. } else {
  195. $inputImage.prop('disabled', true).parent().addClass('disabled');
  196. }
  197. });
  198. /* ------------------------------------------------------------------------------
  199. *
  200. * # Image Cropper extension
  201. *
  202. * Specific JS code additions for extension_image_cropper.html page
  203. *
  204. * Version: 1.1
  205. * Latest update: Jul 5, 2016
  206. *
  207. * ---------------------------------------------------------------------------- */
  208. $(function() {
  209. // Basic setup
  210. // ------------------------------
  211. // Default initialization
  212. $('.crop-basic').cropper();
  213. // Hidden overlay
  214. $('.crop-modal').cropper({
  215. modal: false
  216. });
  217. // Fixed position
  218. $('.crop-not-movable').cropper({
  219. cropBoxMovable: false,
  220. data: {
  221. x: 75,
  222. y: 50,
  223. width: 350,
  224. height: 250
  225. }
  226. });
  227. // Fixed size
  228. $('.crop-not-resizable').cropper({
  229. cropBoxResizable: false,
  230. data: {
  231. x: 10,
  232. y: 10,
  233. width: 300,
  234. height: 300
  235. }
  236. });
  237. // Disabled autocrop
  238. $('.crop-auto').cropper({
  239. autoCrop: false
  240. });
  241. // Disabled drag
  242. $('.crop-drag').cropper({
  243. movable: false
  244. });
  245. // 16:9 ratio
  246. $('.crop-16-9').cropper({
  247. aspectRatio: 16 / 9
  248. });
  249. // 4:3 ratio
  250. $('.crop-4-3').cropper({
  251. aspectRatio: 4 / 3
  252. });
  253. // Minimum zone size
  254. $('.crop-min').cropper({
  255. minCropBoxWidth: 150,
  256. minCropBoxHeight: 150
  257. });
  258. // Disabled zoom
  259. $('.crop-zoomable').cropper({
  260. zoomable: false
  261. });
  262. // Demo cropper
  263. // ------------------------------
  264. // Define variables
  265. /* var $cropper = $(".cropper"),
  266. $image = $('#demo-cropper-image'),
  267. $download = $('#download'),
  268. $dataX = $('#dataX'),
  269. $dataY = $('#dataY'),
  270. $dataHeight = $('#dataHeight'),
  271. $dataWidth = $('#dataWidth'),
  272. $dataScaleX = $('#dataScaleX'),
  273. $dataScaleY = $('#dataScaleY'),
  274. options = {
  275. aspectRatio: 1,
  276. preview: '.preview',
  277. crop: function (e) {
  278. $dataX.val(Math.round(e.x));
  279. $dataY.val(Math.round(e.y));
  280. $dataHeight.val(Math.round(e.height));
  281. $dataWidth.val(Math.round(e.width));
  282. $dataScaleX.val(e.scaleX);
  283. $dataScaleY.val(e.scaleY);
  284. }
  285. };
  286. // Initialize cropper with options
  287. $cropper.cropper(options);
  288. //
  289. // Toolbar
  290. //
  291. $('.demo-cropper-toolbar').on('click', '[data-method]', function () {
  292. var $this = $(this),
  293. data = $this.data(),
  294. $target,
  295. result;
  296. if ($image.data('cropper') && data.method) {
  297. data = $.extend({}, data);
  298. if (typeof data.target !== 'undefined') {
  299. $target = $(data.target);
  300. if (typeof data.option === 'undefined') {
  301. data.option = JSON.parse($target.val());
  302. }
  303. }
  304. result = $image.cropper(data.method, data.option, data.secondOption);
  305. switch (data.method) {
  306. case 'scaleX':
  307. case 'scaleY':
  308. $(this).data('option', -data.option);
  309. break;
  310. case 'getCroppedCanvas':
  311. if (result) {
  312. // Init modal
  313. $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
  314. // Download image
  315. $download.attr('href', result.toDataURL('image/jpeg'));
  316. }
  317. break;
  318. }
  319. }
  320. });
  321. //
  322. // Aspect ratio
  323. //
  324. $('.demo-cropper-ratio').on('change', 'input[type=radio]', function () {
  325. options[$(this).attr('name')] = $(this).val();
  326. $image.cropper('destroy').cropper(options);
  327. });
  328. //
  329. // Switching modes
  330. //
  331. // Crop and clear
  332. // var cropClear = document.querySelector('.clear-crop-switch');
  333. // var cropClearInit = new Switchery(cropClear);
  334. // cropClear.onchange = function() {
  335. // if(cropClear.checked) {
  336. // // Crop mode
  337. // $cropper.cropper('crop');
  338. // // Enable other options
  339. // enableDisableInit.enable();
  340. // destroyCreateInit.enable();
  341. // }
  342. // else {
  343. // // Clear move
  344. // $cropper.cropper('clear');
  345. // // Disable other options
  346. // enableDisableInit.disable();
  347. // destroyCreateInit.disable();
  348. // }
  349. // };
  350. // // Enable and disable
  351. // var enableDisable = document.querySelector('.enable-disable-switch');
  352. // var enableDisableInit = new Switchery(enableDisable);
  353. // enableDisable.onchange = function() {
  354. // if(enableDisable.checked) {
  355. // // Enable cropper
  356. // $cropper.cropper('enable');
  357. // // Enable other options
  358. // cropClearInit.enable();
  359. // destroyCreateInit.enable();
  360. // }
  361. // else {
  362. // // Disable cropper
  363. // $cropper.cropper('disable');
  364. // // Disable other options
  365. // cropClearInit.disable();
  366. // destroyCreateInit.disable();
  367. // }
  368. // };
  369. // // Destroy and create
  370. // var destroyCreate = document.querySelector('.destroy-create-switch');
  371. // var destroyCreateInit = new Switchery(destroyCreate);
  372. // destroyCreate.onchange = function() {
  373. // if(destroyCreate.checked) {
  374. // // Initialize again
  375. // $cropper.cropper({
  376. // aspectRatio: 1,
  377. // preview: ".preview",
  378. // data: {
  379. // x: 208,
  380. // y: 22
  381. // }
  382. // });
  383. // // Enable other options
  384. // cropClearInit.enable();
  385. // enableDisableInit.enable();
  386. // }
  387. // else {
  388. // // Destroy cropper
  389. // $cropper.cropper('destroy');
  390. // // Disable other options
  391. // cropClearInit.disable();
  392. // enableDisableInit.disable();
  393. // }
  394. // };
  395. //
  396. // Methods
  397. //
  398. // Get data
  399. $("#getData").on('click', function() {
  400. $("#showData1").val(JSON.stringify($cropper.cropper("getData")));
  401. });
  402. // Set data
  403. $("#setData").on('click', function() {
  404. $cropper.cropper("setData", {
  405. x: 291,
  406. y: 86,
  407. width: 158,
  408. height: 158
  409. });
  410. $("#showData1").val('Image data has been changed');
  411. });
  412. // Get container data
  413. $("#getContainerData").on('click', function() {
  414. $("#showData2").val(JSON.stringify($cropper.cropper("getContainerData")));
  415. });
  416. // Get image data
  417. $("#getImageData").on('click', function() {
  418. $("#showData2").val(JSON.stringify($cropper.cropper("getImageData")));
  419. });
  420. // Get canvas data
  421. $("#getCanvasData").on('click', function() {
  422. $("#showData3").val(JSON.stringify($cropper.cropper("getCanvasData")));
  423. });
  424. // Set canvas data
  425. $("#setCanvasData").on('click', function() {
  426. $cropper.cropper("setCanvasData", {
  427. left: -50,
  428. top: 0,
  429. width: 750,
  430. height: 750
  431. });
  432. $("#showData3").val('Canvas data has been changed');
  433. });
  434. // Get crop box data
  435. $("#getCropBoxData").on('click', function() {
  436. $("#showData4").val(JSON.stringify($cropper.cropper("getCropBoxData")));
  437. });
  438. // Set crop box data
  439. $("#setCropBoxData").on('click', function() {
  440. $cropper.cropper("setCropBoxData", {
  441. left: 395,
  442. top: 68,
  443. width: 183,
  444. height: 183
  445. });
  446. $("#showData4").val('Crop box data has been changed');
  447. });
  448. */
  449. });