PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/SJRAtlas.Content/javascripts/ext-2.0.1/examples/forum/forum.js

https://github.com/nbadw/sjr_atlas
JavaScript | 400 lines | 342 code | 44 blank | 14 comment | 6 complexity | f816361a639af30c74b2d4f08091ffde MD5 | raw file
  1. /*
  2. * Ext JS Library 2.0.1
  3. * Copyright(c) 2006-2008, Ext JS, LLC.
  4. * licensing@extjs.com
  5. *
  6. * http://extjs.com/license
  7. */
  8. var Forum = {};
  9. //////////////////////////////////////////////////////////////////////////////////////////////
  10. // The data store for topics
  11. Forum.TopicStore = function(){
  12. Forum.TopicStore.superclass.constructor.call(this, {
  13. remoteSort: true,
  14. proxy: new Ext.data.ScriptTagProxy({
  15. url: 'http://extjs.com/forum/topics-browse-remote.php'
  16. }),
  17. reader: new Ext.data.JsonReader({
  18. root: 'topics',
  19. totalProperty: 'totalCount',
  20. id: 'threadid'
  21. }, [
  22. 'title', 'forumtitle', 'forumid', 'author',
  23. {name: 'replycount', type: 'int'},
  24. {name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},
  25. 'lastposter', 'excerpt'
  26. ])
  27. });
  28. this.setDefaultSort('lastpost', 'desc');
  29. };
  30. Ext.extend(Forum.TopicStore, Ext.data.Store, {
  31. loadForum : function(forumId){
  32. this.baseParams = {
  33. forumId: forumId
  34. };
  35. this.load({
  36. params: {
  37. start:0,
  38. limit:25
  39. }
  40. });
  41. }
  42. });
  43. //////////////////////////////////////////////////////////////////////////////////////////////
  44. // some renderers
  45. Forum.Renderers = {
  46. topic : function(value, p, record){
  47. return String.format(
  48. '<div class="topic"><b>{0}</b><span class="author">{1}</span></div>',
  49. value, record.data.author, record.id, record.data.forumid);
  50. },
  51. lastPost : function(value, p, r){
  52. return String.format('<span class="post-date">{0}</span><br/>by {1}', value.dateFormat('M j, Y, g:i a'), r.data['lastposter']);
  53. }
  54. };
  55. //////////////////////////////////////////////////////////////////////////////////////////////
  56. Forum.SearchView = function(search){
  57. this.preview = new Ext.Panel({
  58. region:'south',
  59. height:250,
  60. title:'View Message',
  61. split:true
  62. });
  63. this.store = new Ext.data.Store({
  64. remoteSort: true,
  65. proxy: new Ext.data.ScriptTagProxy({
  66. url: 'http://extjs.com/forum/topics-remote.php'
  67. }),
  68. reader: new Ext.data.JsonReader({
  69. root: 'topics',
  70. totalProperty: 'totalCount',
  71. id: 'post_id'
  72. }, [
  73. {name: 'postId', mapping: 'post_id'},
  74. {name: 'title', mapping: 'topic_title'},
  75. {name: 'topicId', mapping: 'topic_id'},
  76. {name: 'author', mapping: 'author'},
  77. {name: 'lastPost', mapping: 'post_time', type: 'date', dateFormat: 'timestamp'},
  78. {name: 'excerpt', mapping: 'post_text'}
  79. ])
  80. });
  81. this.store.setDefaultSort('lastpost', 'desc');
  82. this.grid = new Ext.grid.GridPanel({
  83. region:'center',
  84. store: this.store,
  85. cm: new Ext.grid.ColumnModel([{
  86. id: 'topic',
  87. header: "Post Title",
  88. dataIndex: 'title',
  89. width: 420,
  90. renderer: Forum.Renderers.topic
  91. },{
  92. id: 'last',
  93. header: "Date Posted",
  94. dataIndex: 'lastpost',
  95. width: 150,
  96. renderer: Ext.util.Format.dateRenderer('M j, Y, g:i a')
  97. }]),
  98. sm: new Ext.grid.RowSelectionModel({
  99. singleSelect:true
  100. }),
  101. trackMouseOver:false,
  102. loadMask: {msg:'Searching...'},
  103. viewConfig: {
  104. forceFit:true,
  105. enableRowBody:true,
  106. showPreview:true,
  107. getRowClass : function(record, rowIndex, p, ds){
  108. if(this.showPreview){
  109. p.body = '<p>'+record.data.excerpt+'</p>';
  110. return 'x-grid3-row-expanded';
  111. }
  112. return 'x-grid3-row-collapsed';
  113. }
  114. },
  115. bbar: new Ext.PagingToolbar({
  116. pageSize: 25,
  117. store: ds,
  118. displayInfo: true,
  119. displayMsg: 'Displaying results {0} - {1} of {2}',
  120. emptyMsg: "No results to display"
  121. })
  122. });
  123. Forum.SearchView.superclass.constructor.call(this, {
  124. layout:'border',
  125. title:'Search: '+Ext.util.Format.htmlEncode('"'+search+'"'),
  126. items:[ this.grid, this.preview ]
  127. });
  128. this.store.baseParams = {
  129. query: search
  130. };
  131. this.store.load({params:{start:0, limit: 25}});
  132. };
  133. Ext.extend(Forum.SearchView, Ext.Panel);
  134. Ext.onReady(function(){
  135. Ext.QuickTips.init();
  136. var ds = new Forum.TopicStore();
  137. var cm = new Ext.grid.ColumnModel([{
  138. id: 'topic',
  139. header: "Topic",
  140. dataIndex: 'title',
  141. width: 420,
  142. renderer: Forum.Renderers.topic
  143. },{
  144. header: "Author",
  145. dataIndex: 'author',
  146. width: 100,
  147. hidden: true
  148. },{
  149. header: "Replies",
  150. dataIndex: 'replycount',
  151. width: 70,
  152. align: 'right'
  153. },{
  154. id: 'last',
  155. header: "Last Post",
  156. dataIndex: 'lastpost',
  157. width: 150,
  158. renderer: Forum.Renderers.lastPost
  159. }]);
  160. cm.defaultSortable = true;
  161. var viewport = new Ext.Viewport({
  162. layout:'border',
  163. items:[
  164. new Ext.BoxComponent({ // raw
  165. region:'north',
  166. el: 'header',
  167. height:32
  168. }),
  169. new Ext.tree.TreePanel({
  170. id:'forum-tree',
  171. region:'west',
  172. title:'Forums',
  173. split:true,
  174. width: 225,
  175. minSize: 175,
  176. maxSize: 400,
  177. collapsible: true,
  178. margins:'0 0 5 5',
  179. loader: new Forum.TreeLoader(),
  180. rootVisible:false,
  181. lines:false,
  182. autoScroll:true,
  183. root: new Ext.tree.AsyncTreeNode({
  184. text: 'Forums',
  185. expanded:true
  186. })
  187. }),
  188. new Ext.TabPanel({
  189. id:'main-tabs',
  190. activeTab:0,
  191. region:'center',
  192. margins:'0 5 5 0',
  193. resizeTabs:true,
  194. tabWidth:150,
  195. items: {
  196. id:'main-view',
  197. layout:'border',
  198. title:'Loading...',
  199. items:[
  200. new Ext.grid.GridPanel({
  201. region:'center',
  202. id:'topic-grid',
  203. store: ds,
  204. cm: cm,
  205. sm:new Ext.grid.RowSelectionModel({singleSelect:true}),
  206. trackMouseOver:false,
  207. loadMask: {msg:'Loading Topics...'},
  208. viewConfig: {
  209. forceFit:true,
  210. enableRowBody:true,
  211. showPreview:true,
  212. getRowClass : function(record, rowIndex, p, ds){
  213. if(this.showPreview){
  214. p.body = '<p>'+record.data.excerpt+'</p>';
  215. return 'x-grid3-row-expanded';
  216. }
  217. return 'x-grid3-row-collapsed';
  218. }
  219. },
  220. tbar:[
  221. {
  222. text:'New Topic',
  223. iconCls: 'new-topic',
  224. handler:function(){alert('Not implemented.');}
  225. },
  226. '-',
  227. {
  228. pressed: true,
  229. enableToggle:true,
  230. text:'Preview Pane',
  231. tooltip: {title:'Preview Pane',text:'Show or hide the Preview Pane'},
  232. iconCls: 'preview',
  233. toggleHandler: togglePreview
  234. },
  235. ' ',
  236. {
  237. pressed: true,
  238. enableToggle:true,
  239. text:'Summary',
  240. tooltip: {title:'Post Summary',text:'View a short summary of each post in the list'},
  241. iconCls: 'summary',
  242. toggleHandler: toggleDetails
  243. }
  244. ],
  245. bbar: new Ext.PagingToolbar({
  246. pageSize: 25,
  247. store: ds,
  248. displayInfo: true,
  249. displayMsg: 'Displaying topics {0} - {1} of {2}',
  250. emptyMsg: "No topics to display"
  251. })
  252. }), {
  253. id:'preview',
  254. region:'south',
  255. height:250,
  256. title:'View Topic',
  257. split:true
  258. }
  259. ]
  260. }
  261. })
  262. ]
  263. });
  264. var tree = Ext.getCmp('forum-tree');
  265. tree.on('append', function(tree, p, node){
  266. if(node.id == 5){
  267. node.select.defer(100, node);
  268. }
  269. });
  270. var sm = tree.getSelectionModel();
  271. sm.on('beforeselect', function(sm, node){
  272. return node.isLeaf();
  273. });
  274. sm.on('selectionchange', function(sm, node){
  275. ds.loadForum(node.id);
  276. Ext.getCmp('main-view').setTitle(node.text);
  277. });
  278. var searchStore = new Ext.data.Store({
  279. proxy: new Ext.data.ScriptTagProxy({
  280. url: 'http://extjs.com/forum/topics-remote.php'
  281. }),
  282. reader: new Ext.data.JsonReader({
  283. root: 'topics',
  284. totalProperty: 'totalCount',
  285. id: 'post_id'
  286. }, [
  287. {name: 'title', mapping: 'topic_title'},
  288. {name: 'topicId', mapping: 'topic_id'},
  289. {name: 'author', mapping: 'author'},
  290. {name: 'lastPost', mapping: 'post_time', type: 'date', dateFormat: 'timestamp'},
  291. {name: 'excerpt', mapping: 'post_text'}
  292. ])
  293. });
  294. // Custom rendering Template
  295. var resultTpl = new Ext.Template(
  296. '<div class="search-item">',
  297. '<h3><span>{lastPost:date("M j, Y")}<br />by {author}</span>{title}</h3>',
  298. '{excerpt}',
  299. '</div>'
  300. );
  301. var search = new Ext.form.ComboBox({
  302. store: searchStore,
  303. displayField:'title',
  304. typeAhead: false,
  305. loadingText: 'Searching...',
  306. width: 200,
  307. pageSize:10,
  308. listWidth:550,
  309. hideTrigger:true,
  310. tpl: resultTpl,
  311. minChars:3,
  312. emptyText:'Quick Search',
  313. onSelect: function(record){ // override default onSelect to do redirect
  314. window.location =
  315. String.format('http://extjs.com/forum/showthread.php?t={0}&p={1}', record.data.topicId, record.id);
  316. }
  317. });
  318. // apply it to the exsting input element
  319. search.applyTo('search');
  320. function toggleDetails(btn, pressed){
  321. var view = Ext.getCmp('topic-grid').getView();
  322. view.showPreview = pressed;
  323. view.refresh();
  324. }
  325. function togglePreview(btn, pressed){
  326. var preview = Ext.getCmp('preview');
  327. preview[pressed ? 'show' : 'hide']();
  328. preview.ownerCt.doLayout();
  329. }
  330. });
  331. Forum.TreeLoader = function(){
  332. Forum.TreeLoader.superclass.constructor.call(this);
  333. this.proxy = new Ext.data.ScriptTagProxy({
  334. url : this.dataUrl
  335. });
  336. };
  337. Ext.extend(Forum.TreeLoader, Ext.tree.TreeLoader, {
  338. dataUrl: 'http://extjs.com/forum/forums-remote.php',
  339. requestData : function(node, cb){
  340. this.proxy.load({}, {
  341. readRecords : function(o){
  342. return o;
  343. }
  344. }, this.addNodes, this, {node:node, cb:cb});
  345. },
  346. addNodes : function(o, arg){
  347. var node = arg.node;
  348. for(var i = 0, len = o.length; i < len; i++){
  349. var n = this.createNode(o[i]);
  350. if(n){
  351. node.appendChild(n);
  352. }
  353. }
  354. arg.cb(this, node);
  355. }
  356. });