/design/ezexceed/javascript/models.js

https://github.com/KeyteqLabs/keymedia-extension · JavaScript · 238 lines · 190 code · 42 blank · 6 comment · 18 complexity · 1791f7c81daf635482698b007a2c6340 MD5 · raw file

  1. define(['backbone', 'jquery-safe'], function(Backbone, $)
  2. {
  3. var url = function() {
  4. var args = ['keymedia', 'media', this.id, this.get('version')];
  5. if (arguments.length > 0) {
  6. args = ['keymedia'].concat(_.toArray(arguments));
  7. }
  8. return args.join('/');
  9. };
  10. var Attribute = Backbone.Model.extend({
  11. urlRoot : null,
  12. medias : null,
  13. initialize : function(options)
  14. {
  15. _.bindAll(this);
  16. this.medias = new MediaCollection();
  17. },
  18. defaults : function()
  19. {
  20. return {
  21. content : false,
  22. media : new Media()
  23. };
  24. },
  25. url : url,
  26. parse : function(data)
  27. {
  28. if ('media' in data) {
  29. data.media = new Media(data.media, {parse: true});
  30. data.media.set('attr', this);
  31. }
  32. if ('content' in data) data.content = data.content;
  33. if ('toScale' in data) data.toScale = data.toScale;
  34. return data;
  35. },
  36. fetch : function(options)
  37. {
  38. options = options || ({});
  39. options.success = this.fetched;
  40. return Backbone.sync('read', this, options);
  41. },
  42. fetched : function(response)
  43. {
  44. this.set(this.parse(response));
  45. this.trigger('fetched');
  46. },
  47. onScale : function(response) {
  48. this.trigger('scale', response);
  49. },
  50. // Create a new vanity url for a version
  51. // name should be a string to put on the back of the object name
  52. // coords should be an array [x,y,x2,y2]
  53. // size shoudl be an array [width,height]
  54. addVanityUrl : function(name, coords, size, options)
  55. {
  56. options = (options || {});
  57. var data = {
  58. name : name,
  59. size : size
  60. };
  61. if (coords)
  62. data.coords = coords;
  63. if (_(options).has('media')) {
  64. data.mediaId = options.media.id;
  65. data.keymediaId = options.media.get('keymediaId');
  66. }
  67. else {
  68. var media = this.get('media');
  69. data.mediaId = media.id;
  70. data.keymediaId = media.get('keymediaId');
  71. }
  72. var id = this.id !== "ezoe" ? this.id : this.get('attributeId');
  73. var url = this.url('saveVersion', id, this.get('version'));
  74. return Backbone.sync('create', {url: url}, {data: data});
  75. },
  76. getOriginalUrl : function()
  77. {
  78. var args = ['keymedia', 'mediaUrl', this.id, this.get('version')];
  79. var url = args.join('/');
  80. return eZExceed.transformUrl(url);
  81. }
  82. });
  83. var Media = Backbone.Model.extend({
  84. urlRoot : '',
  85. initialize : function(options)
  86. {
  87. options = (options || {});
  88. _.bindAll(this);
  89. if ('urlRoot' in options) {
  90. this.urlRoot = options.urlRoot;
  91. delete options.urlRoot;
  92. }
  93. },
  94. parse : function(data)
  95. {
  96. data.tags = new Backbone.Collection(_.map(data.tags, function(tag) {
  97. return {
  98. id : tag,
  99. tag : tag
  100. };
  101. }));
  102. return data;
  103. },
  104. domain : function()
  105. {
  106. return 'http://' + this.get('host');
  107. },
  108. url : false,
  109. save : function()
  110. {
  111. var attr = this.get('attr');
  112. var url = attr.url('tag', attr.id, attr.get('version'), 'tag');
  113. var data = {
  114. id : this.id,
  115. tags : this.get('tags').pluck('tag')
  116. };
  117. return Backbone.sync('create', {url: url}, {data: data});
  118. },
  119. // Generate thumb url for a given size
  120. thumb : function(width, height, filetype)
  121. {
  122. filetype = (filetype || 'jpg');
  123. return this.domain() + '/' + width + 'x' + height + '/' + this.id + '.' + filetype;
  124. }
  125. });
  126. var MediaCollection = Backbone.Collection.extend({
  127. model : Media,
  128. // Must end in trailing slash
  129. urlRoot : '/',
  130. attr : null,
  131. total : 0,
  132. q : '',
  133. limit : 25,
  134. keymediaId : null,
  135. xhr : null,
  136. initialize : function(options)
  137. {
  138. _.bindAll(this);
  139. },
  140. url : function()
  141. {
  142. return ['keymedia', 'browse', this.id, this.version].join('/');
  143. },
  144. search : function(q, data)
  145. {
  146. var data = (data || {});
  147. if (typeof q === 'string') {
  148. this.q = q;
  149. data.q = q;
  150. }
  151. data.limit = this.limit;
  152. if (this.xhr && typeof this.xhr.abort === 'function') {
  153. this.xhr.abort();
  154. }
  155. this.xhr = this.fetch({data : data, reset: true});
  156. return this.xhr;
  157. },
  158. fetched : function()
  159. {
  160. this.trigger('fetched');
  161. this.xhr = null;
  162. },
  163. parse : function(data)
  164. {
  165. if ('keymediaId' in data) {
  166. this.keymediaId = data.keymediaId;
  167. }
  168. if ('results' in data) {
  169. this.total = data.results.total;
  170. data = data.results.hits;
  171. }
  172. return data;
  173. },
  174. page : function(q)
  175. {
  176. if (this.length < this.total) {
  177. var data = {
  178. limit : this.limit,
  179. offset : this.length,
  180. q : this.q
  181. };
  182. return Backbone.sync('read', {url: this.url()}, {data: data}).done(this.paged);
  183. }
  184. return false;
  185. },
  186. paged: function(data)
  187. {
  188. this.add(this.parse(data));
  189. }
  190. });
  191. return {
  192. media : Media,
  193. attribute : Attribute,
  194. collection : MediaCollection
  195. };
  196. });