Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aui-image-cropper-debug.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. AUI.add('aui-image-cropper', function(A) {
  2. var Lang = A.Lang,
  3. isBoolean = Lang.isBoolean,
  4. isNumber = Lang.isNumber,
  5. toInt = Lang.toInt,
  6. NAME = 'image-cropper',
  7. CSS_CROP = A.getClassName(NAME, 'crop'),
  8. CSS_CROP_OUTLINE = A.getClassName(NAME, 'crop', 'outline'),
  9. CSS_OVERLAY = A.getClassName(NAME, 'overlay');
  10. CSS_OVERLAY_HOVER = A.getClassName(NAME, 'crop', 'hover');
  11. var ImageCropper = A.Component.create(
  12. {
  13. NAME: NAME,
  14. ATTRS: {
  15. cropHeight: {
  16. value: 100,
  17. validator: isNumber
  18. },
  19. cropWidth: {
  20. value: 100,
  21. validator: isNumber
  22. },
  23. minWidth: {
  24. value: undefined
  25. },
  26. minHeight: {
  27. value: undefined
  28. },
  29. movable: {
  30. value: true,
  31. validator: isBoolean
  32. },
  33. preserveRatio: {
  34. value: false,
  35. validator: isBoolean
  36. },
  37. region: {
  38. getter: '_getCropRegion',
  39. value: {}
  40. },
  41. resizable: {
  42. value: true,
  43. validator: isBoolean
  44. },
  45. x: {
  46. value: 0,
  47. setter: Math.round,
  48. validator: isNumber
  49. },
  50. y: {
  51. value: 0,
  52. setter: Math.round,
  53. validator: isNumber
  54. }
  55. },
  56. UI_ATTRS: [
  57. 'cropHeight',
  58. 'cropWidth',
  59. 'minWidth',
  60. 'minHeight',
  61. 'movable',
  62. 'resizable',
  63. 'x',
  64. 'y'
  65. ],
  66. prototype: {
  67. renderUI: function() {
  68. var instance = this;
  69. var boundingBox = instance.get('boundingBox');
  70. var imageNode = instance.get('srcNode');
  71. instance.cropNode = A.Node.create('<div class="' + CSS_CROP + '"></div>');
  72. instance.cropNode.append(A.Node.create('<div class="' + CSS_CROP_OUTLINE + '"></div>'));
  73. instance.overlay = A.Node.create('<div class="' + CSS_OVERLAY + '"></div>');
  74. A.all([instance.cropNode, instance.overlay]).appendTo(boundingBox);
  75. instance._boundingBox = boundingBox;
  76. instance._renderDrag();
  77. instance._renderResize();
  78. },
  79. bindUI: function() {
  80. var instance = this;
  81. instance._fireCropEventTask = A.debounce(instance._fireCropEvent, 10, instance);
  82. instance.publish(
  83. 'crop',
  84. {
  85. defaultFn: instance._defCropFn
  86. }
  87. );
  88. instance.on(['drag:start', 'resize:start'], A.debounce(instance._syncRegion, 25));
  89. instance.after(['drag:drag', 'resize:resize'], instance._fireCropEvent, instance);
  90. instance.after(
  91. ['xChange', 'yChange', 'cropWidthChange', 'cropHeightChange'],
  92. function(event) {
  93. instance._fireCropEventTask(event);
  94. instance._syncCropNodeUI();
  95. }
  96. );
  97. instance._createHover();
  98. },
  99. syncUI: function() {
  100. var instance = this;
  101. instance._uiSetPreserveRatio(instance.get('preserveRatio'));
  102. instance.syncImageUI();
  103. instance._syncCropNodeUI();
  104. },
  105. destructor: function() {
  106. var instance = this;
  107. instance._destroyDrag();
  108. instance._destroyResize();
  109. },
  110. syncImageUI: function() {
  111. var instance = this;
  112. var imageNode = instance.get('srcNode');
  113. var overlayNode = instance.overlay;
  114. instance.cropNode.setStyle('backgroundImage', 'url(' + imageNode.attr('src') + ')');
  115. instance._constrainValues();
  116. instance._syncXY();
  117. var origRegion = instance._getConstraintRegion();
  118. var drag = instance.drag;
  119. var resize = instance.resize;
  120. if (drag) {
  121. drag.con.set('constrain', origRegion);
  122. }
  123. if (resize) {
  124. resize.con.set('constrain', origRegion);
  125. }
  126. },
  127. _constrainValues: function() {
  128. var instance = this;
  129. var imageNode = instance.get('srcNode');
  130. var cropHeight = instance.get('cropHeight');
  131. var cropWidth = instance.get('cropWidth');
  132. var x = instance.get('x');
  133. var y = instance.get('y');
  134. var imageWidth = imageNode.width();
  135. var imageHeight = imageNode.height();
  136. // Find valid y
  137. y = Math.max(y, 0);
  138. if (y + cropHeight > imageHeight) {
  139. y = Math.max(imageHeight - cropHeight, 0);
  140. }
  141. instance.set('y', y);
  142. // Find valid cropHeight
  143. if (y + cropHeight > imageHeight) {
  144. cropHeight = Math.max(imageHeight - y, 0);
  145. }
  146. instance.set('cropHeight', cropHeight);
  147. // Find valid x
  148. x = Math.max(x, 0);
  149. if (x + cropWidth > imageWidth) {
  150. x = Math.max(imageWidth - cropWidth, 0);
  151. }
  152. instance.set('x', x);
  153. // Find valid cropWidth
  154. if (x + cropWidth > imageWidth) {
  155. cropWidth = Math.max(imageWidth - x, 0);
  156. }
  157. instance.set('cropWidth', cropWidth);
  158. },
  159. _createHover: function() {
  160. var instance = this;
  161. instance._destroyHover();
  162. instance._hoverHandles = instance.cropNode.on(
  163. 'hover',
  164. instance._hoverOverlay,
  165. instance._unHoverOverlay,
  166. instance
  167. );
  168. },
  169. _defCropFn: function(event) {
  170. var instance = this;
  171. var cropType = event.cropType;
  172. if (cropType == 'drag:drag') {
  173. instance._syncXY();
  174. }
  175. else if (cropType == 'resize:resize') {
  176. instance._syncCropSize();
  177. }
  178. },
  179. _destroyDrag: function(object) {
  180. var instance = this;
  181. if (instance.drag) {
  182. instance.drag.destroy();
  183. delete instance.drag;
  184. }
  185. },
  186. _destroyHover: function() {
  187. var instance = this;
  188. if (instance._hoverHandles) {
  189. instance._hoverHandles.detach();
  190. instance._hoverHandles = null;
  191. }
  192. },
  193. _destroyResize: function(object) {
  194. var instance = this;
  195. if (instance.resize) {
  196. instance.resize.destroy();
  197. delete instance.resize;
  198. }
  199. },
  200. _fireCropEvent: function(event) {
  201. var instance = this;
  202. instance.fire('crop', {cropType: event.type});
  203. },
  204. _getConstraintRegion: function(force) {
  205. var instance = this;
  206. var region = !force ? instance._origRegion : null;
  207. if (!region) {
  208. var imageNode = instance.get('srcNode');
  209. var cropNode = instance.cropNode;
  210. var imageXY = imageNode.getXY();
  211. var imageX = imageXY[0];
  212. var imageY = imageXY[1];
  213. region = {
  214. bottom: imageY + imageNode.height() + cropNode.getBorderWidth('b'),
  215. left: imageX - cropNode.getBorderWidth('l'),
  216. right: imageX + imageNode.width() + cropNode.getBorderWidth('r'),
  217. top: imageY - cropNode.getBorderWidth('t')
  218. };
  219. if (!instance._origRegion) {
  220. instance._origRegion = region;
  221. }
  222. }
  223. return region;
  224. },
  225. _getCropRegion: function() {
  226. var instance = this;
  227. return {
  228. height: instance.get('cropHeight'),
  229. width: instance.get('cropWidth'),
  230. x: instance.get('x'),
  231. y: instance.get('y')
  232. };
  233. },
  234. _hoverOverlay: function() {
  235. var instance = this;
  236. if (!instance._isDragging() && !instance._isResizing()) {
  237. instance._boundingBox.addClass(CSS_OVERLAY_HOVER);
  238. }
  239. },
  240. _isDragging: function() {
  241. var instance = this;
  242. var drag = instance.drag;
  243. return drag && drag.get('dragging');
  244. },
  245. _isResizing: function() {
  246. var instance = this;
  247. var resize = instance.resize;
  248. return resize && resize.get('resizing');
  249. },
  250. _renderDrag: function() {
  251. var instance = this;
  252. var drag = new A.DD.Drag(
  253. {
  254. node: instance.cropNode
  255. }
  256. ).plug(
  257. A.Plugin.DDConstrained,
  258. {
  259. constrain: instance._getConstraintRegion()
  260. }
  261. );
  262. drag.addTarget(instance);
  263. instance.drag = drag;
  264. },
  265. _renderResize: function() {
  266. var instance = this;
  267. var resize = new A.Resize(
  268. {
  269. node: instance.cropNode
  270. }
  271. ).plug(
  272. A.Plugin.ResizeConstrained,
  273. {
  274. constrain: instance._getConstraintRegion(),
  275. preserveRatio: instance.get('preserveRatio'),
  276. minHeight: instance.get('minHeight'),
  277. minWidth: instance.get('minWidth')
  278. }
  279. );
  280. resize.addTarget(instance);
  281. instance.resize = resize;
  282. },
  283. _syncCropNodeUI: function() {
  284. var instance = this;
  285. instance.cropNode.setStyle('backgroundPosition', (-instance.get('x')) + 'px ' + (-instance.get('y')) + 'px');
  286. },
  287. _syncCropSize: function(event) {
  288. var instance = this;
  289. var cropNode = instance.cropNode;
  290. instance.set('cropHeight', cropNode.height());
  291. instance.set('cropWidth', cropNode.width());
  292. },
  293. _syncRegion: function(event) {
  294. var instance = this;
  295. var region = instance._getConstraintRegion(true);
  296. var origRegion = instance._origRegion;
  297. if (
  298. region.bottom != origRegion.bottom ||
  299. region.left != origRegion.left ||
  300. region.right != origRegion.right ||
  301. region.top != origRegion.top
  302. ) {
  303. var drag = instance.drag;
  304. var resize = instance.resize;
  305. if (drag) {
  306. drag.con.set('constrain', region);
  307. }
  308. if (resize) {
  309. resize.con.set('constrain', region);
  310. }
  311. instance._origRegion = region;
  312. }
  313. },
  314. _syncXY: function(event) {
  315. var instance = this;
  316. var cropNode = instance.cropNode;
  317. instance.set('x', toInt(cropNode.getStyle('left')) + cropNode.getBorderWidth('l'));
  318. instance.set('y', toInt(cropNode.getStyle('top')) + cropNode.getBorderWidth('t'));
  319. },
  320. _uiSetCropHeight: function(value) {
  321. var instance = this;
  322. instance.cropNode.height(value);
  323. },
  324. _uiSetCropWidth: function(value) {
  325. var instance = this;
  326. instance.cropNode.width(value);
  327. },
  328. _uiSetDisabled: function(value) {
  329. var instance = this;
  330. ImageCropper.superclass._uiSetDisabled.apply(instance, arguments);
  331. var enabled = !value;
  332. instance.cropNode.toggle(enabled);
  333. if (enabled) {
  334. instance._createHover();
  335. }
  336. else {
  337. instance._destroyHover();
  338. }
  339. },
  340. _uiSetMinHeight: function(value) {
  341. var instance = this;
  342. var resize = instance.resize;
  343. if (resize) {
  344. resize.con.set('minHeight', value);
  345. }
  346. },
  347. _uiSetMinWidth: function(value) {
  348. var instance = this;
  349. var resize = instance.resize;
  350. if (resize) {
  351. resize.con.set('minWidth', value);
  352. }
  353. },
  354. _uiSetMovable: function(value) {
  355. var instance = this;
  356. instance.drag.set('lock', !value);
  357. },
  358. _uiSetPreserveRatio: function(value) {
  359. var instance = this;
  360. var resize = instance.resize;
  361. if (resize) {
  362. resize.con.set('preserveRatio', value);
  363. }
  364. },
  365. _uiSetResizable: function(value) {
  366. var instance = this;
  367. if (value) {
  368. if (instance._stopResizeHandle) {
  369. instance._stopResizeHandle.detach();
  370. }
  371. }
  372. else if (!instance._stopResizeHandle) {
  373. instance._stopResizeHandle = instance.resize.on(
  374. 'resize:resize',
  375. function (event) {
  376. event.halt();
  377. }
  378. );
  379. }
  380. },
  381. _uiSetX: function(value) {
  382. var instance = this;
  383. var imageNode = instance.get('srcNode');
  384. var cropNode = instance.cropNode;
  385. cropNode.setStyle('left', value - cropNode.getBorderWidth('l'));
  386. },
  387. _uiSetY: function(value) {
  388. var instance = this;
  389. var imageNode = instance.get('srcNode');
  390. var cropNode = instance.cropNode;
  391. cropNode.setStyle('top', value - cropNode.getBorderWidth('t'));
  392. },
  393. _unHoverOverlay: function() {
  394. var instance = this;
  395. if (!instance._isDragging() && !instance._isResizing()) {
  396. instance._boundingBox.removeClass(CSS_OVERLAY_HOVER);
  397. }
  398. }
  399. }
  400. }
  401. );
  402. A.ImageCropper = ImageCropper;
  403. }, '@VERSION@' ,{skinnable:true, requires:['widget','aui-base','resize','dd-constrain']});