HtmlController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using ClearSlideLibrary.Dom;
  6. using ClearSlideLibrary.Animations;
  7. namespace ClearSlideLibrary.HtmlController
  8. {
  9. public class HtmlController
  10. {
  11. private readonly int _numberOfSlides;
  12. private readonly int _allSlidesCount;
  13. private readonly int _slideIndex;
  14. private readonly string _fileName;
  15. private readonly string _baseFileName;
  16. private readonly string _filePath;
  17. private readonly StringBuilder _htmlPart;
  18. private readonly PPTSlide _mSlide;
  19. private const string JS_DIR_NAME = "temp";
  20. public int SlideWidth { get; set; }
  21. public int SlideHeight { get; set; }
  22. public HtmlController(string filepath, string filename,
  23. PPTSlide aSlide, int slideCounter, int slidesNumber)
  24. {
  25. _baseFileName = filename;
  26. try
  27. {
  28. if (filename.Length != 0)
  29. {
  30. if (slidesNumber != 1)
  31. {
  32. this._fileName = filename +"_" +slideCounter.ToString();
  33. }
  34. else
  35. {
  36. this._fileName = filename;
  37. }
  38. }
  39. this._mSlide = aSlide;
  40. _numberOfSlides = 1;
  41. _allSlidesCount = slidesNumber;
  42. _slideIndex = slideCounter;
  43. this._filePath = filepath;
  44. this._htmlPart = new StringBuilder();
  45. }
  46. catch (ArgumentNullException ex)
  47. {
  48. Console.WriteLine("The passed value for filename:" + ex.Message);
  49. return;
  50. }
  51. }
  52. public void GenerateHtml()
  53. {
  54. this.GenerateHeader();
  55. this.GenerateBody();
  56. this.WriteTofile(_htmlPart);
  57. }
  58. private void WriteTofile(StringBuilder content)
  59. {
  60. string htmlFilePath = this._filePath + "\\" + this._fileName;
  61. string outFileName = htmlFilePath + ".html";
  62. FileStream fs = File.Create(outFileName, 1024);
  63. Byte[] htmlcontent = new UTF8Encoding(true).GetBytes(content.ToString());
  64. fs.Write(htmlcontent, 0, htmlcontent.Length);
  65. fs.Close();
  66. string outputDirWithFileNameAdded = Path.Combine(_filePath, _fileName);
  67. CopyJSFiles(_filePath, outputDirWithFileNameAdded);
  68. }
  69. private void CopyJSFiles(string destinationDirForHtmlFile, string newOutputDir)
  70. {
  71. string sourceDir = Path.Combine(destinationDirForHtmlFile, JS_DIR_NAME);
  72. string destDir = newOutputDir;
  73. DirectoryInfo src = new DirectoryInfo(sourceDir);
  74. DirectoryInfo dest = new DirectoryInfo(destDir);
  75. FileInfo[] files = src.GetFiles();
  76. foreach (FileInfo file in files)
  77. {
  78. try
  79. {
  80. if (file.Exists)
  81. {
  82. file.CopyTo(Path.Combine(dest.FullName,
  83. file.Name));
  84. }
  85. }
  86. catch (IOException iex)
  87. {
  88. Console.WriteLine(iex.Message);
  89. }
  90. }
  91. }
  92. private void GenerateHeader()
  93. {
  94. string numbers = DynamicHeaderJSPart();
  95. AppendHeader(numbers);
  96. }
  97. private string DynamicHeaderJSPart()
  98. {
  99. if (_numberOfSlides == 1)
  100. {
  101. return "[0,1,1]";
  102. }
  103. else
  104. {
  105. int count = 0;
  106. StringBuilder numbers = new StringBuilder();
  107. numbers.Append("[");
  108. while (count <= _numberOfSlides - 1)
  109. {
  110. numbers.Append(count.ToString());
  111. numbers.Append(",");
  112. count++;
  113. }
  114. numbers.Append(count);
  115. int length = numbers.ToString().Length;
  116. string outputString = numbers.ToString().Remove(length - 1, 1);
  117. return outputString + "]";
  118. }
  119. }
  120. private void AppendHeader(string numbers)
  121. {
  122. string SC = ".SC {height: " + SlideHeight + "px; width: " + SlideWidth + "px; display: none;}";
  123. string DC = ".DC1 {top: 0px; left: 0px; height: 100%; width: 100%; opacity: 1; }";
  124. string DC0 = ".DC0 {top: 0px; left: 0px; height: 100%; width: 100%; opacity: 0;}";
  125. _htmlPart.Append("<!DOCTYPE html>");
  126. _htmlPart.Append("<html><head>");
  127. _htmlPart.Append("<title>" + this._fileName + "</title>");
  128. _htmlPart.Append("<meta content=\"text/html;charset=UTF-8\" http-equiv=\"content-type\"/>");
  129. _htmlPart.Append("<meta content=\"width=device-width, initial-scale=1,"
  130. + "maximum-scale=1, user-scalable=0\"name=\"viewport\"/>");
  131. _htmlPart.Append("<link href=\"" + this._fileName + "/c.css\" rel=\"stylesheet\" type=\"text/css\"/>");
  132. _htmlPart.Append("<script type=\"text/javascript\" src=\"" + this._fileName + "/jquery.js\"></script>");
  133. _htmlPart.Append("<script type=\"text/javascript\" src=\"" + this._fileName + "/animationUtils.js\"></script>");
  134. _htmlPart.Append("<script type=\"text/javascript\" src=\"" + this._fileName + "/animations.js\"></script>");
  135. _htmlPart.Append("<script type=\"text/javascript\" src=\"" + this._fileName + "/player.js\"></script>");
  136. _htmlPart.Append("<script type=\"text/javascript\">var gv = {w: 0,h: 0,t: 1,a: " +
  137. "'" + this._fileName + "'" + ",v: 0,sh:" + SlideHeight + ",sw:" + SlideWidth + ",s: 0,i:" + numbers +
  138. ",f:1,r:1};</script>");
  139. _htmlPart.Append("<style type=\"text/css\">" + SC + "</style>");
  140. _htmlPart.Append("<style type=\"text/css\">" + DC + DC0 + "</style>");
  141. _htmlPart.Append("</head>");
  142. }
  143. private void GenerateBody()
  144. {
  145. _htmlPart.Append("<body id=\"bodyid\""
  146. + "style=\"background-color:Black;height:100%;width:100%;overflow:hidden;margin:0px;" +
  147. "user-select: none;-khtml-user-select: none;-ms-user-select: none;-o-user-select: text;" +
  148. "-webkit-user-select: none;-moz-user-select: none;-webkit-tap-highlight-color:rgba(0,0,0,0);\"" +
  149. "onkeydown=\"keychanger(event)\">");
  150. AppendPlayer();
  151. _htmlPart.Append("<div id=\"resizer\" style=\"left:0px;top:0px;height:" + SlideHeight + "px;width:" + SlideWidth + "px; overflow:hidden;-moz-transform-origin: 0 0;-o-transform-origin: 0 0; -webkit-transform-origin: 0 0; -ms-transform-origin: 0 0;\">");
  152. DynamicBodyPart();
  153. _htmlPart.Append("<div id=\"loadingImg\" style=\"height:100%;width:100%;display:none;\">");
  154. _htmlPart.Append("<img style=\"height:30px;width:130px;top:255px;left:295px;\" src=\"" + this._fileName + "/pre.gif\" />");
  155. _htmlPart.Append("</div>");
  156. _htmlPart.Append("</div>");
  157. _htmlPart.Append("<script type=\"text/javascript\">");
  158. string staticJS =
  159. (_mSlide.advanceAfterTime >= 0 ? "var advanceAfterSlide='" + _mSlide.advanceAfterTime + "'; var advanceSlideUrl = '" + _baseFileName + "_" + (_slideIndex + 1) + ".html?autoStart=true';" : "") +
  160. "var lastAnimationPlaying=false;if ((document.createElement('canvas').getContext) ? true : false) { if (gv.b != 1) gfl(); $(window).resize(resizer);$(document).ready(resizer);" +
  161. "if(window.location.toString().indexOf(\"autoStart=true\")>0){$(window).bind('load', atLoadAutoStart); }else { $(window).bind('load', atload);}" +
  162. "} else { document.getElementById('resizer').innerHTML = ''; document.getElementById('toolbar').innerHTML = ''; document.getElementById('toolbar').style.width = '0px'; document.getElementById('idpre').innerHTML =" +
  163. "'Oops! Your browser does not support HTML5. You need to upgrade your browser to view this presentation.' }";
  164. //Dynamic part from the generated JavaScript file.
  165. string anima = new ClearSlideLibrary.Animations.JSONGenerator(_mSlide).GetAnimaVariable();
  166. _htmlPart.Append(staticJS);
  167. _htmlPart.Append(anima);
  168. _htmlPart.Append("</script>");
  169. _htmlPart.Append("</body></html>");
  170. }
  171. private void DynamicBodyPart()
  172. {
  173. if (_numberOfSlides != 0)
  174. {
  175. ////foreach (PPTSlide pptSlide in _mSlides)
  176. ////{
  177. string dirpath = Path.Combine(_filePath, _fileName);
  178. HtmlSlide slide = new HtmlSlide(_mSlide.Id, "SC", this._fileName, dirpath, _mSlide.slideIndex)
  179. {
  180. ContainerShape = _mSlide.ContainerShape
  181. };
  182. _htmlPart.Append(slide.DrawElement());
  183. //}
  184. }
  185. }
  186. private void AppendPlayer()
  187. {
  188. _htmlPart.Append("<div id=\"idpre\" style=\"z-index:1000;height:100%;width:100%;color:white;\">");
  189. _htmlPart.Append("<img id=\"play\" style=\"display:none;height:200px;width:200px;cursor:pointer;position:absolute;\"");
  190. _htmlPart.Append("onclick=\"fnPre()\" src=\"" + this._fileName + "/pre.png\" alt=\"click to begin\" />");
  191. _htmlPart.Append("</div>");
  192. _htmlPart.Append("<div class=\"toolbar\" id=\"toolbar\" style=\"bottom:0px;\">");
  193. _htmlPart.Append("<div id=\"ddiv\" style=\"height:50px;\">");
  194. _htmlPart.Append("<div class=\"\" id=\"time\" style=\"display:none;\">");
  195. _htmlPart.Append("<span id=\"times\" style=\"font-weight:bold; color: #525151;\">00:00</span>");
  196. _htmlPart.Append("</div>");
  197. _htmlPart.Append("<div class=\"progress\" id=\"progress\" style=\"display:none;-moz-transform-origin: 0 0;-o-transform-origin: 0 0;-webkit-transform-origin: 0 0; -ms-transform-origin: 0 0;opacity:0;\">");
  198. _htmlPart.Append("<div class=\"progressbar\" id=\"Bar\"> </div>");
  199. _htmlPart.Append("</div>");
  200. String nextUrl = _slideIndex >= _allSlidesCount ? "'" + _fileName + ".html'" : "'" + _baseFileName + "_" + (_slideIndex + 1) + ".html?autoStart=true'";
  201. String prevUrl = _slideIndex <= 1 ? "'" + _fileName + ".html'" : "'" + _baseFileName + "_" + (_slideIndex - 1) + ".html?autoStart=true'";
  202. _htmlPart.Append("<div id=\"buttonDiv\" style=\"width:150px; height:50px; opacity:0;\">");
  203. _htmlPart.Append("<div class=\"next\" style=\"background-image: url('" + this._fileName + "/arrow.png')\" onclick=\"window.location = " + nextUrl + " ; fnPre()\"></div>");
  204. _htmlPart.Append("<div class=\"next2\" style=\"background-image: url('" + this._fileName + "/arrow.png')\" onclick=\"next()\"></div>");
  205. _htmlPart.Append("<div class=\"play\" id=\"playB\" style=\"background-image: url('" + this._fileName + "/arrow.png')\" onclick=\"next()\"></div>");
  206. _htmlPart.Append("<div class=\"prev\" style=\"background-image: url('" + this._fileName + "/arrow.png')\" onclick=\"window.location = " + prevUrl + "; fnPre()\"></div>");
  207. _htmlPart.Append("<div class=\"prev2\" style=\"background-image: url('" + this._fileName + "/arrow.png')\" onclick=\"prev()\"></div>");
  208. _htmlPart.Append("</div>");
  209. _htmlPart.Append("<div id=\"status\" style=\"display:none;width:34px;height:21px;\" title=\"goto\">");
  210. _htmlPart.Append("<div id=\"statustotal\" style=\"top:1px;left:16px;font-size:14px;\">/ 1</div>");
  211. _htmlPart.Append("<input id=\"current\" style=\"width:10px; top:3px;font-size:14px; height:16px;\" onkeypress=\"EnterCurrent(event)\" maxlength=\"1\" type=\"text\"/>");
  212. _htmlPart.Append("</div>");
  213. _htmlPart.Append("</div>");
  214. _htmlPart.Append("</div>");
  215. }
  216. }
  217. }