template.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. (function() {
  2. Trex.Table.TemplateLoader = Trex.Class.create({
  3. initialize: function() {
  4. this.templateList = _NULL;
  5. },
  6. getTemplate: function(index, callback) {
  7. if (isNaN(index)) {
  8. return;
  9. }
  10. if (this.templateList) {
  11. callback(new TableTemplate(this.templateList[index]));
  12. } else {
  13. var self = this;
  14. this.loadTemplate(function (templateList) {
  15. self.templateList = templateList;
  16. callback(new TableTemplate(self.templateList[index]));
  17. });
  18. }
  19. },
  20. loadTemplate: function(onLoadComplete) {
  21. var url = this.getJSBasePath() + "trex/modules/table/async/template_data.js";
  22. if (EditorJSLoader.getOption('environment') == 'development') {
  23. url += '?dummy=' + (new Date()).getTime();
  24. }
  25. EditorJSLoader.asyncLoadModule({
  26. url: TrexConfig.getUrl(url),
  27. callback: function() {
  28. var templateList = getTableTemplateList();
  29. onLoadComplete(templateList);
  30. }
  31. });
  32. },
  33. getJSBasePath: function() {
  34. var basePath;
  35. try {
  36. basePath = EditorJSLoader.getJSBasePath("editor.js");
  37. } catch (e) {
  38. basePath = EditorJSLoader.getJSBasePath();
  39. }
  40. return basePath;
  41. }
  42. });
  43. var TableTemplate = Trex.Class.create({
  44. initialize: function(templateData) {
  45. this.templateData = templateData;
  46. },
  47. apply: function(table) {
  48. var tableMatrixer = new Trex.Tool.Table.TableCellMatrixer(table);
  49. var tdMatrix = tableMatrixer.getTdMatrix();
  50. var self = this;
  51. for (var i = 0; i < tdMatrix.length; i++) {
  52. for (var j = 0; j < tdMatrix[i].length; j++) {
  53. self.setCellStyle(tdMatrix[i][j], {
  54. isEvenRow: (i % 2) == 1,
  55. isFirstRow: i == 0,
  56. isLastRow: i == tdMatrix.length - 1,
  57. isFirstCol: j == 0,
  58. isLastCol: (j == tdMatrix[i].length - 1)
  59. });
  60. }
  61. }
  62. },
  63. setCellStyle: function(elTd, truthMap) {
  64. var t = this.templateData;
  65. var style = Object.extend({}, t['common']);
  66. Object.extend(style, (truthMap.isEvenRow) ? t['evenRow'] : t['oddRow']);
  67. Object.extend(style, (truthMap.isFirstRow) ? t['firstRow'] : (truthMap.isLastRow) ? t['lastRow'] : {});
  68. Object.extend(style, (truthMap.isLastCol) ? t['lastCol'] : {});
  69. Object.extend(style, (truthMap.isFirstCol) ? t['firstCol'] : {});
  70. txlib.setStyle(elTd, style);
  71. }
  72. });
  73. })();