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.
 
 
 
 
 
 

353 rivejä
9.0 KiB

  1. // Jcrop API methods
  2. $.extend(Jcrop.prototype,{
  3. //init: function(){{{
  4. init: function(){
  5. this.event = new this.opt.eventManagerComponent(this);
  6. this.ui.keyboard = new this.opt.keyboardComponent(this);
  7. this.ui.manager = new this.opt.stagemanagerComponent(this);
  8. this.applyFilters();
  9. if ($.Jcrop.supportsTouch)
  10. new $.Jcrop.component.Touch(this);
  11. this.initEvents();
  12. },
  13. //}}}
  14. // applySizeConstraints: function(){{{
  15. applySizeConstraints: function(){
  16. var o = this.opt,
  17. img = this.opt.imgsrc;
  18. if (img){
  19. var iw = img.naturalWidth || img.width,
  20. ih = img.naturalHeight || img.height,
  21. bw = o.boxWidth || iw,
  22. bh = o.boxHeight || ih;
  23. if (img && ((iw > bw) || (ih > bh))){
  24. var bx = Jcrop.getLargestBox(iw/ih,bw,bh);
  25. $(img).width(bx[0]).height(bx[1]);
  26. this.resizeContainer(bx[0],bx[1]);
  27. this.opt.xscale = iw / bx[0];
  28. this.opt.yscale = ih / bx[1];
  29. }
  30. }
  31. if (this.opt.trueSize){
  32. var dw = this.opt.trueSize[0];
  33. var dh = this.opt.trueSize[1];
  34. var cs = this.getContainerSize();
  35. this.opt.xscale = dw / cs[0];
  36. this.opt.yscale = dh / cs[1];
  37. }
  38. },
  39. // }}}
  40. initComponent: function(name){
  41. if (Jcrop.component[name]) {
  42. var args = Array.prototype.slice.call(arguments);
  43. var obj = new Jcrop.component[name];
  44. args.shift();
  45. args.unshift(this);
  46. obj.init.apply(obj,args);
  47. return obj;
  48. }
  49. },
  50. // setOptions: function(opt){{{
  51. setOptions: function(opt,proptype){
  52. if (!$.isPlainObject(opt)) opt = {};
  53. $.extend(this.opt,opt);
  54. // Handle a setSelect value
  55. if (this.opt.setSelect) {
  56. // If there is no current selection
  57. // passing setSelect will create one
  58. if (!this.ui.multi.length)
  59. this.newSelection();
  60. // Use these values to update the current selection
  61. this.setSelect(this.opt.setSelect);
  62. // Set to null so it doesn't get called again
  63. this.opt.setSelect = null;
  64. }
  65. this.event.trigger('configupdate');
  66. return this;
  67. },
  68. // }}}
  69. //destroy: function(){{{
  70. destroy: function(){
  71. if (this.opt.imgsrc) {
  72. this.container.before(this.opt.imgsrc);
  73. this.container.remove();
  74. $(this.opt.imgsrc).removeData('Jcrop').show();
  75. } else {
  76. // @todo: more elegant destroy() process for non-image containers
  77. this.container.remove();
  78. }
  79. },
  80. // }}}
  81. // applyFilters: function(){{{
  82. applyFilters: function(){
  83. var obj;
  84. for(var i=0,f=this.opt.applyFilters,l=f.length; i<l; i++){
  85. if ($.Jcrop.filter[f[i]])
  86. obj = new $.Jcrop.filter[f[i]];
  87. obj.core = this;
  88. if (obj.init) obj.init();
  89. this.filter[f[i]] = obj;
  90. }
  91. },
  92. // }}}
  93. // getDefaultFilters: function(){{{
  94. getDefaultFilters: function(){
  95. var rv = [];
  96. for(var i=0,f=this.opt.applyFilters,l=f.length; i<l; i++)
  97. if(this.filter.hasOwnProperty(f[i]))
  98. rv.push(this.filter[f[i]]);
  99. rv.sort(function(x,y){ return x.priority - y.priority; });
  100. return rv;
  101. },
  102. // }}}
  103. // setSelection: function(sel){{{
  104. setSelection: function(sel){
  105. var m = this.ui.multi;
  106. var n = [];
  107. for(var i=0;i<m.length;i++) {
  108. if (m[i] !== sel) n.push(m[i]);
  109. m[i].toBack();
  110. }
  111. n.unshift(sel);
  112. this.ui.multi = n;
  113. this.ui.selection = sel;
  114. sel.toFront();
  115. return sel;
  116. },
  117. // }}}
  118. // getSelection: function(raw){{{
  119. getSelection: function(raw){
  120. var b = this.ui.selection.get();
  121. return b;
  122. },
  123. // }}}
  124. // newSelection: function(){{{
  125. newSelection: function(sel){
  126. if (!sel)
  127. sel = new this.opt.selectionComponent();
  128. sel.init(this);
  129. this.setSelection(sel);
  130. return sel;
  131. },
  132. // }}}
  133. // hasSelection: function(sel){{{
  134. hasSelection: function(sel){
  135. for(var i=0;i<this.ui.multi;i++)
  136. if (sel === this.ui.multi[i]) return true;
  137. },
  138. // }}}
  139. // removeSelection: function(sel){{{
  140. removeSelection: function(sel){
  141. var i, n = [], m = this.ui.multi;
  142. for(var i=0;i<m.length;i++){
  143. if (sel !== m[i])
  144. n.push(m[i]);
  145. else m[i].remove();
  146. }
  147. return this.ui.multi = n;
  148. },
  149. // }}}
  150. //addFilter: function(filter){{{
  151. addFilter: function(filter){
  152. for(var i=0,m=this.ui.multi,l=m.length; i<l; i++)
  153. m[i].addFilter(filter);
  154. return this;
  155. },
  156. //}}}
  157. // removeFiltersByTag: function(tag){{{
  158. removeFilter: function(filter){
  159. for(var i=0,m=this.ui.multi,l=m.length; i<l; i++)
  160. m[i].removeFilter(filter);
  161. return this;
  162. },
  163. // }}}
  164. // blur: function(){{{
  165. blur: function(){
  166. this.ui.selection.blur();
  167. return this;
  168. },
  169. // }}}
  170. // focus: function(){{{
  171. focus: function(){
  172. this.ui.selection.focus();
  173. return this;
  174. },
  175. // }}}
  176. //initEvents: function(){{{
  177. initEvents: function(){
  178. var t = this;
  179. t.container.on('selectstart',function(e){ return false; })
  180. .on('mousedown','.'+t.opt.css_drag,t.startDrag());
  181. },
  182. //}}}
  183. // maxSelect: function(){{{
  184. maxSelect: function(){
  185. this.setSelect([0,0,this.elw,this.elh]);
  186. },
  187. // }}}
  188. // nudge: function(x,y){{{
  189. nudge: function(x,y){
  190. var s = this.ui.selection, b = s.get();
  191. b.x += x;
  192. b.x2 += x;
  193. b.y += y;
  194. b.y2 += y;
  195. if (b.x < 0) { b.x2 = b.w; b.x = 0; }
  196. else if (b.x2 > this.elw) { b.x2 = this.elw; b.x = b.x2 - b.w; }
  197. if (b.y < 0) { b.y2 = b.h; b.y = 0; }
  198. else if (b.y2 > this.elh) { b.y2 = this.elh; b.y = b.y2 - b.h; }
  199. s.element.trigger('cropstart',[s,this.unscale(b)]);
  200. s.updateRaw(b,'move');
  201. s.element.trigger('cropend',[s,this.unscale(b)]);
  202. },
  203. // }}}
  204. // refresh: function(){{{
  205. refresh: function(){
  206. for(var i=0,s=this.ui.multi,l=s.length;i<l;i++)
  207. s[i].refresh();
  208. },
  209. // }}}
  210. // blurAll: function(){{{
  211. blurAll: function(){
  212. var m = this.ui.multi;
  213. for(var i=0;i<m.length;i++) {
  214. if (m[i] !== sel) n.push(m[i]);
  215. m[i].toBack();
  216. }
  217. },
  218. // }}}
  219. // scale: function(b){{{
  220. scale: function(b){
  221. var xs = this.opt.xscale,
  222. ys = this.opt.yscale;
  223. return {
  224. x: b.x / xs,
  225. y: b.y / ys,
  226. x2: b.x2 / xs,
  227. y2: b.y2 / ys,
  228. w: b.w / xs,
  229. h: b.h / ys
  230. };
  231. },
  232. // }}}
  233. // unscale: function(b){{{
  234. unscale: function(b){
  235. var xs = this.opt.xscale,
  236. ys = this.opt.yscale;
  237. return {
  238. x: b.x * xs,
  239. y: b.y * ys,
  240. x2: b.x2 * xs,
  241. y2: b.y2 * ys,
  242. w: b.w * xs,
  243. h: b.h * ys
  244. };
  245. },
  246. // }}}
  247. // requestDelete: function(){{{
  248. requestDelete: function(){
  249. if ((this.ui.multi.length > 1) && (this.ui.selection.canDelete))
  250. return this.deleteSelection();
  251. },
  252. // }}}
  253. // deleteSelection: function(){{{
  254. deleteSelection: function(){
  255. if (this.ui.selection) {
  256. this.removeSelection(this.ui.selection);
  257. if (this.ui.multi.length) this.ui.multi[0].focus();
  258. this.ui.selection.refresh();
  259. }
  260. },
  261. // }}}
  262. // animateTo: function(box){{{
  263. animateTo: function(box){
  264. if (this.ui.selection)
  265. this.ui.selection.animateTo(box);
  266. return this;
  267. },
  268. // }}}
  269. // setselect: function(box){{{
  270. setSelect: function(box){
  271. if (this.ui.selection)
  272. this.ui.selection.update(Jcrop.wrapFromXywh(box));
  273. return this;
  274. },
  275. // }}}
  276. //startDrag: function(){{{
  277. startDrag: function(){
  278. var t = this;
  279. return function(e){
  280. var $targ = $(e.target);
  281. var selection = $targ.closest('.'+t.opt.css_selection).data('selection');
  282. var ord = $targ.data('ord');
  283. t.container.trigger('cropstart',[selection,t.unscale(selection.get())]);
  284. selection.startDrag(e,ord);
  285. return false;
  286. };
  287. },
  288. //}}}
  289. // getContainerSize: function(){{{
  290. getContainerSize: function(){
  291. return [ this.container.width(), this.container.height() ];
  292. },
  293. // }}}
  294. // resizeContainer: function(w,h){{{
  295. resizeContainer: function(w,h){
  296. this.container.width(w).height(h);
  297. this.refresh();
  298. },
  299. // }}}
  300. // setImage: function(src,cb){{{
  301. setImage: function(src,cb){
  302. var t = this, targ = t.opt.imgsrc;
  303. if (!targ) return false;
  304. new $.Jcrop.component.ImageLoader(src,null,function(w,h){
  305. t.resizeContainer(w,h);
  306. targ.src = src;
  307. $(targ).width(w).height(h);
  308. t.applySizeConstraints();
  309. t.refresh();
  310. t.container.trigger('cropimage',[t,targ]);
  311. if (typeof cb == 'function')
  312. cb.call(t,w,h);
  313. });
  314. },
  315. // }}}
  316. // update: function(b){{{
  317. update: function(b){
  318. if (this.ui.selection)
  319. this.ui.selection.update(b);
  320. }
  321. // }}}
  322. });