PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/com.poetry/WebContent/js/main.js

https://github.com/bylee/poetry
JavaScript | 313 lines | 270 code | 43 blank | 0 comment | 22 complexity | 5282fd30595db89960e19f605e5de6fe MD5 | raw file
Possible License(s): LGPL-3.0
  1. window.Model = Backbone.Model.extend( {
  2. } );
  3. window.View = Backbone.View.extend( {
  4. template: function( model ) {
  5. var temp = this.defaultTemplate;
  6. if ( this.templateId ) {
  7. temp = $(this.templateId).html();
  8. if ( !temp ) {
  9. temp = this.defaultTemplate;
  10. }
  11. }
  12. if ( !model ) {
  13. return temp;
  14. }
  15. var result = Mustache.render( temp, model.toJSON() );
  16. console.log( "create template :%s", result );
  17. return result;
  18. }
  19. } );
  20. window.Collection = Backbone.Collection.extend( { } );
  21. window.MenuItem = Model.extend( {
  22. } );
  23. window.MenuItemView = View.extend( {
  24. tagName: "li",
  25. templateId: "#menu-item-template",
  26. events: { "click" : "select" },
  27. render: function() {
  28. $(this.el).html( this.template( this.model ) );
  29. return this;
  30. },
  31. select: function( e ) {
  32. this.menu.select( this );
  33. return false;
  34. }
  35. } );
  36. window.Menu = Collection.extend( {
  37. model: MenuItem
  38. } );
  39. window.MenuView = View.extend( {
  40. templateId: "#menu-template",
  41. initialize: function() {
  42. this.collection.bind( 'add', this.add, this );
  43. this.collection.bind( 'remove', this.remove );
  44. $( this.el ).append( $( this.template( this.model ) ) );
  45. this.collection.each( this.add, this );
  46. },
  47. render: function () {
  48. return this;
  49. },
  50. add: function( model ) {
  51. itemView = new MenuItemView( { model: model } );
  52. itemView.menu = this;
  53. $(this.el).find( "ul" ).append( itemView.render().el );
  54. if ( !this.selectedItem ) {
  55. this.select( itemView );
  56. }
  57. },
  58. select: function( item ) {
  59. $( this.el ).find( "li" ).removeClass( "active" );
  60. $( item.el ).addClass( "active " );
  61. this.selectedItem = item;
  62. $.get( item.model.get( "url" ), function( data ){
  63. $( "#contents" ).html( data );
  64. } );
  65. }
  66. } );
  67. window.ImageUpload = Model.extend( {
  68. } );
  69. window.ImageUploadView = View.extend( {
  70. templateId: "#imageUpload-template",
  71. addChangeListener: function( listener )
  72. {
  73. }
  74. } );
  75. window.Calendar = Model.extend( {
  76. defaults: { date: new Date() },
  77. initialize: function() {
  78. if ( !this.get( "date" ) ) {
  79. this.set( { "date": this.defaults.date } );
  80. }
  81. },
  82. setDate: function( date ) {
  83. this.set( { "date": date } );
  84. },
  85. getDate: function() {
  86. return this.get( "date" );
  87. }
  88. } );
  89. window.CalendarView = View.extend( {
  90. format: "yyyy-mm-dd",
  91. templateId: "#date-template",
  92. defaultTemplate:'<input name="date" type="text">',
  93. initialize: function() {
  94. $( this.el ).append( $( this.template( this.model ) ) );
  95. _.bindAll( this, "changeDate" );
  96. this.model.bind( "change:date", this.changeDate );
  97. this.picker = $( 'input' ).datepicker( { "format" : this.format } );
  98. this.picker.on( "changeDate", _.bind( function( ev ) {
  99. this.model.set( { "date" : ev.date, silent : true } );
  100. }, this ) );
  101. this.picker.val( this.formatDate( this.model.getDate() ) );
  102. },
  103. render: function() {
  104. this.picker.datepicker( "update" );
  105. return this;
  106. },
  107. changeDate: function( model ) {
  108. this.picker.val( this.formatDate( this.model.getDate() ) );
  109. this.render();
  110. },
  111. formatDate: function( date ) {
  112. return DPGlobal.formatDate( date, DPGlobal.parseFormat( this.format ) );
  113. }
  114. } );
  115. window.ChoiceItem = Model.extend( {
  116. } );
  117. window.ChoiceItemView = View.extend( {
  118. tagName: "label",
  119. className: "radio",
  120. templateId: "#choice-item-template",
  121. defaultTemplate: '<input type="radio" name="{{group}}" id="{{id}}" value="{{value}}">{{name}}',
  122. events: { "click" : "select" },
  123. initialize: function() {
  124. this.model.bind( 'change', this.update, this );
  125. },
  126. update: function() {
  127. $( this.el ).html( this.template( this.model ) );
  128. if ( this.model == this.choice.getSelection() ) {
  129. $( this.el ).find( "input" ).attr( "checked", true );
  130. }
  131. },
  132. render: function() {
  133. this.update();
  134. return this;
  135. },
  136. select: function( e ) {
  137. this.choice.setSelection( this.model );
  138. }
  139. } );
  140. window.Choice = Collection.extend( {
  141. model: ChoiceItem,
  142. setSelection: function( selection ) {
  143. if ( !selection ) {
  144. selection = this.models[0];
  145. }
  146. if ( this.selection == selection ) {
  147. return ;
  148. }
  149. if ( this.selection ) {
  150. this.selection.trigger( "change" );
  151. }
  152. this.selection = selection;
  153. this.selection.trigger( "change" );
  154. this.selection.get( "act" )();
  155. },
  156. getSelection: function() {
  157. return this.selection;
  158. },
  159. isSelected: function() {
  160. return ( undefined != this.selection );
  161. }
  162. } );
  163. window.ChoiceView = View.extend( {
  164. templateId: "#choice-template",
  165. id: _.uniqueId( 'choice' ),
  166. initialize: function() {
  167. this.collection.bind( 'add', this.add, this );
  168. this.collection.bind( 'remove', this.remove );
  169. $( this.el ).append( $( this.template( this.model ) ) );
  170. this.collection.each( this.add, this );
  171. },
  172. render: function () {
  173. return this;
  174. },
  175. add: function( model ) {
  176. model.set( "group", this.id );
  177. itemView = new ChoiceItemView( { model: model } );
  178. itemView.choice = this.collection;
  179. $(this.el).append( itemView.render().el );
  180. if ( !this.collection.isSelected() )
  181. {
  182. this.collection.setSelection( model );
  183. }
  184. }
  185. } );
  186. window.Poetry = Model.extend( {
  187. } );
  188. window.PoetryView = View.extend( {
  189. className:"item",
  190. templateId: "#poetry-template",
  191. defaultTemplate: '<img width="200", height="200" src="../../binary/{{image}}"><div>{{contents}}</div>',
  192. initialize: function() {
  193. this.model.bind( 'change', this.update, this );
  194. },
  195. render: function () {
  196. this.update();
  197. return this;
  198. },
  199. update: function() {
  200. $( this.el ).html( this.template( this.model ) );
  201. if ( this.model == this.poetries.getSelection() ) {
  202. $( this.el ).addClass( "active" );
  203. }
  204. }
  205. } );
  206. window.Poetries = Collection.extend( {
  207. model: Poetry,
  208. setSelection: function( selection ) {
  209. if ( !selection ) {
  210. selection = this.models[0];
  211. }
  212. if ( this.selection == selection ) {
  213. return ;
  214. }
  215. if ( this.selection ) {
  216. this.selection.trigger( "change" );
  217. }
  218. this.selection = selection;
  219. this.selection.trigger( "change" );
  220. },
  221. getSelection: function() {
  222. return this.selection;
  223. },
  224. isSelected: function() {
  225. return ( undefined != this.selection );
  226. },
  227. nextRequest: function() {
  228. url = "../../today/candidate";
  229. if ( !_.isEmpty( this.models ) ) {
  230. url += "?start=" + _.last( this.models ).get( "id" );
  231. }
  232. $.ajax( {
  233. method: "GET",
  234. dataType: "json",
  235. url: url,
  236. context: this,
  237. success: this.nextResponse
  238. } );
  239. },
  240. nextResponse: function( json ) {
  241. _.each( json, function( m, l ) {
  242. this.add( new Poetry( m ) );
  243. }, this );
  244. },
  245. } );
  246. window.Carousel = View.extend( {
  247. templateId: "#carousel-template",
  248. defaultTemplate: '<div class="carousel slide" style="height: 220px;"><div class="carousel-inner"></div><a class="carousel-control left" data-slide="prev">&lsaquo;</a><a class="carousel-control right" data-slide="next">&rsaquo;</a></div>',
  249. initialize: function() {
  250. this.collection.bind( 'add', this.add, this );
  251. this.collection.bind( 'remove', this.remove );
  252. $( this.el ).html( $( this.template( this.model ) ) );
  253. $( this.el ).attr( "id", _.uniqueId( 'carousel' ) );
  254. $( this.el ).find( "a" ).attr( "href", "#" + $( this.el ).attr( "id" ) );
  255. this.collection.each( this.add, this );
  256. },
  257. render: function () {
  258. $( this.el ).carousel( { interval: 5000 } );
  259. return this;
  260. },
  261. add: function( model ) {
  262. itemView = new PoetryView( { model: model } );
  263. itemView.poetries = this.collection;
  264. $(this.el).find( ".carousel-inner" ).append( itemView.render().el );
  265. if ( !this.collection.isSelected() )
  266. {
  267. this.collection.setSelection( model );
  268. }
  269. }
  270. } );