Syntax-Highlight-Style

Adds extra code highlighting frormating options ot syntaxhighlight.in

  1. // ==UserScript==
  2. // @name Syntax-Highlight-Style
  3. // @namespace https://github.com/Amourspirit/Syntax-Highlight-Style
  4. // @version 1.1.2
  5. // @description Adds extra code highlighting frormating options ot syntaxhighlight.in
  6. // @author Paul Moss
  7. // @match http://syntaxhighlight.in/
  8. // @license MIT
  9. // @homepageURL http://amourspirit.github.io/Syntax-Highlight-Style/
  10. // @update https://github.com/Amourspirit/Syntax-Highlight-Style/raw/master/syntax-highlight-style.user.js
  11. // @contributionURL http://amourspirit.github.io/Syntax-Highlight-Style/#donate
  12. // @grant none
  13. // ==/UserScript==
  14. /* jshint -W097 */
  15. 'use strict';
  16. // #nsregion BIGBYTE
  17. var BIGBYTE = BIGBYTE || {};
  18. if (typeof(BIGBYTE.createNS) == 'undefined') {
  19. BIGBYTE.createNS = function(namespace) {
  20. var nsparts = namespace.split(".");
  21. var parent = BIGBYTE;
  22.  
  23. // we want to be able to include or exclude the root namespace so we strip
  24. // it if it's in the namespace))
  25. if (nsparts[0] === "BIGBYTE") {
  26. nsparts = nsparts.slice(1);
  27. }
  28.  
  29. // loop through the parts and create a nested namespace if necessary
  30. for (var i = 0; i < nsparts.length; i++) {
  31. var partname = nsparts[i];
  32. // check if the current parent already has the namespace declared
  33. // if it isn't, then create it
  34. if (typeof parent[partname] === "undefined") {
  35. parent[partname] = {};
  36. }
  37. // get a reference to the deepest element in the hierarchy so far
  38. parent = parent[partname];
  39. }
  40. // the parent is now constructed with empty namespaces and can be used.
  41. // we return the outermost namespace
  42. return parent;
  43. };
  44. }
  45. if (typeof(BIGBYTE.isjquery) == 'undefined') {
  46. BIGBYTE.isjquery = function(data) {
  47. // If data is already a jQuery object
  48. if (data instanceof jQuery) {
  49. // Do nothing different
  50. data = data;
  51. // Otherwise
  52. } else {
  53. // Convert to jQuery object
  54. data = jQuery(data);
  55. }
  56. // Return jQuery object
  57. return data;
  58. };
  59. }
  60.  
  61. // #endnsregion BIGBYTE
  62. // #nsregion BIGBYTE.USERSCRIPT.DOCUMENT
  63.  
  64. var bbDoc = BIGBYTE.createNS("BIGBYTE.USERSCRIPT.DOCUMENT");
  65. bbDoc.ns = 'BIGBYTE.USERSCRIPT.DOCUMENT';
  66. // #region LoadScripts
  67. if (typeof(bbDoc.loadScript) == 'undefined') {
  68. bbDoc.loadScript = function(scriptItm) {
  69. var lib = this;
  70. if (typeof(scriptItm.count) == 'undefined') {
  71. scriptItm.count = 0;
  72. }
  73. if (typeof(scriptItm.loaded) == 'undefined') {
  74. scriptItm.loaded = false;
  75. }
  76. if (typeof(scriptItm.text) == 'undefined') {
  77. scriptItm.text = ''; // timeout in seconds
  78. }
  79. if (typeof(scriptItm.timeout) == 'undefined') {
  80. scriptItm.timeout = 30; // timeout in seconds
  81. }
  82.  
  83. var bbScriptLoadedEvent = new CustomEvent(
  84. "bbScriptLoaded", {
  85. detail: {
  86. message: "Script Loaded",
  87. time: new Date(),
  88. scriptItm: scriptItm
  89. },
  90. bubbles: true,
  91. cancelable: false
  92. }
  93. );
  94.  
  95. switch (scriptItm.type) {
  96. case 'linkedjs':
  97. var skipTest = false;
  98. if (typeof(scriptItm.testMethod) == 'undefined' || (scriptItm.testMethod.length == 0)) {
  99. skipTest = true;
  100. }
  101. if (skipTest) {
  102. // there is no test for this item so we will and assume
  103. // all is fine/
  104. scriptItm.loaded = true;
  105. lib.addJS_Node(scriptItm.text, scriptItm.src);
  106. // trigger event for loaded
  107.  
  108. //jQuery(document).trigger("bbScriptLoaded", scriptItm);
  109. document.dispatchEvent(bbScriptLoadedEvent);
  110. return;
  111. }
  112. scriptItm.count++;
  113. var maxCount = scriptItm.timeout * 10; // multply by 10 to convert into 10th of seconds
  114.  
  115. if (scriptItm.count > maxCount) {
  116. console.error('unable to load script, Aborting: ', scriptItm.src);
  117. return;
  118. }
  119. var testmethod;
  120. try {
  121. testmethod = eval(scriptItm.testMethod);
  122. } catch (e) {
  123. testmethod = undefined;
  124. }
  125. if (typeof(testmethod) == 'undefined') {
  126. if (!scriptItm.loaded) {
  127. scriptItm.loaded = true;
  128. lib.addJS_Node(scriptItm.text, scriptItm.src);
  129. }
  130. setTimeout(function() {
  131. lib.loadScript(scriptItm);
  132. }, 100);
  133. } else {
  134. // script item is loaded trigger an evert
  135. //jQuery(document).trigger("bbScriptLoaded", scriptItm);
  136. document.dispatchEvent(bbScriptLoadedEvent);
  137. }
  138. break;
  139. case 'css':
  140. if (typeof(scriptItm.tag) == 'undefined') {
  141. scriptItm.tag = 'body'; // timeout in seconds
  142. }
  143. lib.addCss_Node(scriptItm.src, scriptItm.tag);
  144. //jQuery(document).trigger("bbScriptLoaded", scriptItm);
  145. document.dispatchEvent(bbScriptLoadedEvent);
  146. break;
  147. case 'csslink':
  148. lib.addLink_Node(scriptItm.src);
  149. //jQuery(document).trigger("bbScriptLoaded", scriptItm);
  150. document.dispatchEvent(bbScriptLoadedEvent);
  151. break;
  152. default:
  153. // statements_def
  154. break;
  155. }
  156.  
  157.  
  158. }
  159. }
  160.  
  161. // #endregion LoadScripts
  162. // #region BIGBYTE.USERSCRIPT.DOCUMENT Methods
  163. // gneric document related
  164. if (typeof(bbDoc.addJS_Node) == 'undefined') {
  165. bbDoc.addJS_Node = function(text, s_URL, funcToRun, runOnLoad) {
  166. var D = document;
  167. var scriptNode = D.createElement('script');
  168. if (runOnLoad) {
  169. scriptNode.addEventListener("load", runOnLoad, false);
  170. }
  171. scriptNode.type = "text/javascript";
  172. if (text) scriptNode.textContent = text;
  173. if (s_URL) scriptNode.src = s_URL;
  174. if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
  175.  
  176. var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
  177. targ.appendChild(scriptNode);
  178. };
  179. }
  180. if (typeof(bbDoc.addJS_NodeToBody) == 'undefined') {
  181. bbDoc.addJS_NodeToBody = function(text, s_URL, funcToRun, runOnLoad) {
  182. var D = document;
  183. var scriptNode = D.createElement('script');
  184. if (runOnLoad) {
  185. scriptNode.addEventListener("load", runOnLoad, false);
  186. }
  187. scriptNode.type = "text/javascript";
  188. if (text) scriptNode.textContent = text;
  189. if (s_URL) scriptNode.src = s_URL;
  190. if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
  191.  
  192. var targ = D.getElementsByTagName('body')[0] || D.body || D.documentElement;
  193. targ.appendChild(scriptNode);
  194. };
  195. }
  196. if (typeof(bbDoc.addCss_Node) == 'undefined') {
  197. bbDoc.addCss_Node = function(text, element) {
  198. element = typeof element !== 'undefined' ? element : 'head';
  199. var D = document;
  200. var scriptNode = D.createElement('style');
  201. scriptNode.type = "text/css";
  202. if (text) scriptNode.textContent = text;
  203.  
  204. var targ = D.getElementsByTagName(element)[0] || D.body || D.documentElement;
  205. targ.appendChild(scriptNode);
  206. };
  207. }
  208. if (typeof(bbDoc.addLink_Node) == 'undefined') {
  209. bbDoc.addLink_Node = function(href, type, rel) {
  210. type = typeof type !== 'undefined' ? type : "text/css";
  211. rel = typeof rel !== 'undefined' ? rel : "stylesheet";
  212. var D = document;
  213. var scriptNode = D.createElement('link');
  214. scriptNode.type = type;
  215. scriptNode.href = href;
  216. if (rel) scriptNode.rel = rel;
  217.  
  218. var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
  219. targ.appendChild(scriptNode);
  220. };
  221. }
  222.  
  223. if (typeof(bbDoc.addHtml_Node) == 'undefined') {
  224. bbDoc.addHtml_Node = function(html) {
  225. var D = document;
  226. var targ = D.getElementsByTagName('body')[0] || D.body || D.documentElement;
  227. targ.insertAdjacentHTML('beforeend', html);
  228. };
  229. }
  230. // #endregion BIGBYTE.USERSCRIPT.DOCUMENT Methods
  231.  
  232. // #endnsregion BIGBYTE.USERSCRIPT.DOCUMENT"
  233.  
  234. // #nsregion BIGBYTE.USERSCRIPT.UTIL
  235. var bbusu = BIGBYTE.createNS("BIGBYTE.USERSCRIPT.UTIL");
  236. bbusu.ns = 'BIGBYTE.USERSCRIPT.UTIL';
  237. // #region Methods
  238. if(typeof(bbusu.extend) == 'undefined') {
  239. /**
  240. * Extends an object to contain new Properties
  241. * @return {[Object]} the new merged oject
  242. */
  243. bbusu.extend = function () {
  244. for (var i = 1; i < arguments.length; i++)
  245. for (var key in arguments[i])
  246. if (arguments[i].hasOwnProperty(key))
  247. arguments[0][key] = arguments[i][key];
  248. return arguments[0];
  249. };
  250. }
  251.  
  252. // #endregion Methods
  253. // #endnsregion BIGBYTE.USERSCRIPT.UTIL
  254.  
  255. // #nsregion BIGBYTE.USERSCRIPT.CLIPBOARD
  256. // #region Create NS
  257. var bbClip = BIGBYTE.createNS("BIGBYTE.USERSCRIPT.CLIPBOARD");
  258. bbClip.ns = 'BIGBYTE.USERSCRIPT.CLIPBOARD';
  259. // #endregion Create NS
  260. // #region Properties
  261. bbClip.scriptOutputted = false;
  262. // #endregion Properties
  263. // #endnsregion BIGBYTE.USERSCRIPT.CLIPBOARD
  264.  
  265. // #nsregion BIGBYTE.STRING
  266. // #region Create NS
  267. var bbString = BIGBYTE.createNS("BIGBYTE.STRING");
  268. bbString.ns = 'BIGBYTE.STRING';
  269. // #endregion Create NS
  270. // #region Init
  271. if (typeof(bbString.pad) == 'undefined') {
  272. bbString.pad = function pad(str, max, padChar) {
  273. padChar = padChar || '0';
  274. str = str.toString();
  275. return str.length < max ? this.pad(padChar + str, max, padChar) : str;
  276. }
  277. }
  278. // #endregion Init
  279. // #endnsregion BIGBYTE.STRING
  280.  
  281. // #nsregion BIGBYTE.USERSCRIPT.STHL
  282. // #region Create NS
  283. var sthl = BIGBYTE.createNS("BIGBYTE.USERSCRIPT.STHL");
  284. sthl.ns = 'BIGBYTE.USERSCRIPT.STHL';
  285. // #endregion Create ns
  286. // #region Button methods
  287.  
  288. /**
  289. * Toggles Buttons enabled or disabled
  290. * @param {[boolean]} enabled If true buttons will be enable; Otherwise disabled
  291. */
  292. sthl.toggleButtons = function(enabled, forceRedraw) {
  293. enabled = (typeof(enabled) == 'undefined') ? false : enabled;
  294. forceRedraw = (typeof(forceRedraw) == 'undefined') ? false : forceRedraw;
  295. var $ = jQuery;
  296. if (enabled) {
  297. $('#gmconvert').attr("disabled", false);
  298. $('#gmcopy').attr("disabled", false);
  299. $('#gmcopytbl').attr("disabled", false);
  300. $('#gmtable').attr("disabled", false);
  301. } else {
  302. $('#gmconvert').attr("disabled", 'disabled');
  303. $('#gmcopy').attr("disabled", 'disabled');
  304. $('#gmcopytbl').attr("disabled", 'disabled');
  305. $('#gmtable').attr("disabled", 'disabled');
  306. }
  307. if (forceRedraw) {
  308. $('#gmconvert').hide().show(0);
  309. $('#gmcopy').hide().show(0);
  310. $('#gmcopytbl').hide().show(0);
  311. $('#gmtable').hide().show(0);
  312. }
  313. };
  314.  
  315. sthl.getConvertedList = function() {
  316. var $ = jQuery;
  317. var $sourceCode = $('pre.snippet-formatted.sh_sourceCode');
  318. $sourceCode.makeCssInlineCode(false);
  319. var contentHtml = '';
  320. if ($sourceCode.length) {
  321. var $wrapper = $('<div />');
  322. $wrapper.assignStyleFrom($sourceCode).css({
  323. width: 'auto',
  324. height: 'auto',
  325. animation: '',
  326. clip: '',
  327. direction: '',
  328. fill: '',
  329. filter: '',
  330. flex: '',
  331. isolation: '',
  332. orphans: '',
  333. position: '',
  334. r: '',
  335. resize: '',
  336. rx: '',
  337. ry: '',
  338. speak: '',
  339. stroke: '',
  340. top: '',
  341. transform: '',
  342. transition: '',
  343. visibility: '',
  344. widows: '',
  345. x: '',
  346. y: '',
  347. zoom: '',
  348. //background: '',
  349. opacity: '',
  350. order: '',
  351. clear: '',
  352. cx: '',
  353. cy: '',
  354. float: '',
  355. mask: '',
  356. motion: ''
  357. });
  358. // $sourceCode.removeAttrib('class', true);
  359. $sourceCode.wrapInner($wrapper);
  360. contentHtml = $sourceCode.spaceReplace(true)[0].outerHTML;
  361. }
  362. return contentHtml;
  363. };
  364.  
  365. sthl.getConvertedTable = function() {
  366. var $ = jQuery;
  367.  
  368. var $sourceCode = $('pre.snippet-formatted.sh_sourceCode');
  369.  
  370. $sourceCode.find('li').each(function(index, li) {
  371. // convert li spaces to \u00a0
  372. $(li).spaceReplace(false); // do not recurse
  373. });
  374.  
  375. $sourceCode.makeCssInlineCode(true); // generates for html Table
  376. var contentHtml = '';
  377. if ($sourceCode.length) {
  378. contentHtml = this.toTable($sourceCode)[0].outerHTML;
  379. }
  380. return contentHtml;
  381. };
  382.  
  383.  
  384. /*
  385. * Button click to convert ordered list of the code output to inline styled ordered list
  386. */
  387. sthl.convertClick = function(btn) {
  388.  
  389. var $ = jQuery;
  390. var lib = BIGBYTE.USERSCRIPT.STHL;
  391. lib.toggleButtons(false, true);
  392. $.event.trigger({
  393. type: 'codeConversionStart',
  394. message: 'convert_list_start',
  395. time: new Date(),
  396. source: 'convertClick'
  397. });
  398. this.windowScrollPos = $('body').scrollTop();
  399. $('html, body').animate({
  400. scrollTop: 0
  401. }, 'slow');
  402. $(document).disableScroll('html, body');
  403. var $btn = BIGBYTE.isjquery(btn); // convert to jquery obj
  404. var libTmce = BIGBYTE.USERSCRIPT.STHL.TMCE;
  405. if ($btn.data('converted')) {
  406. $btn.data('converted', 0);
  407. } else {
  408. $btn.data('converted', 1);
  409.  
  410. }
  411. var contentHtml = this.getConvertedList();
  412. if (libTmce.fullscreen) {
  413. tinyMCE.get('gminput').execCommand('mceFullScreen');
  414. }
  415. tinyMCE.get('gminput').setContent(contentHtml);
  416. $('.gmbackdrop, .gmbox').animate({
  417. 'opacity': '.50'
  418. }, 300, 'linear');
  419. $('.gmbox').animate({
  420. 'opacity': '1.00'
  421. }, 300, 'linear');
  422. $('.gmbackdrop, .gmbox').css('display', 'block');
  423.  
  424. $.event.trigger({
  425. type: 'codeConversionFinish',
  426. message: 'convert_list_end',
  427. time: new Date(),
  428. source: 'convertClick',
  429. html: contentHtml
  430. });
  431.  
  432. };
  433.  
  434.  
  435. /*
  436. * Button click to convert ordered list of the code output to html table
  437. */
  438. sthl.convertTable = function(btn) {
  439. var $ = jQuery;
  440. $.event.trigger({
  441. type: 'codeConversionStart',
  442. message: 'convert_table_start',
  443. time: new Date(),
  444. source: 'convertTable'
  445. });
  446.  
  447. var libTmce = BIGBYTE.USERSCRIPT.STHL.TMCE;
  448. this.windowScrollPos = $('body').scrollTop();
  449. $('html, body').animate({
  450. scrollTop: 0
  451. }, 'slow');
  452. $(document).disableScroll('html, body');
  453.  
  454. var contentHtml = this.getConvertedTable();
  455. if (libTmce.fullscreen) {
  456. tinyMCE.get('gminput').execCommand('mceFullScreen');
  457. }
  458. tinyMCE.get('gminput').setContent(contentHtml);
  459. $('.gmbackdrop, .gmbox').animate({
  460. 'opacity': '.50'
  461. }, 300, 'linear');
  462. $('.gmbox').animate({
  463. 'opacity': '1.00'
  464. }, 300, 'linear');
  465. $('.gmbackdrop, .gmbox').css('display', 'block');
  466.  
  467. $.event.trigger({
  468. type: 'codeConversionFinish',
  469. message: 'convert_table_end',
  470. time: new Date(),
  471. source: 'convertTable',
  472. html: contentHtml
  473. });
  474. };
  475. // #endregion Button methods
  476. // #region Init
  477. /**
  478. * Init for the main script
  479. */
  480. sthl.init = function(pluginSrc) {
  481. var tinyMceVer = BIGBYTE.USERSCRIPT.STHL.TMCE.version;
  482. //var tinyMceVer = '4.3.2';
  483. pluginSrc = typeof(pluginSrc) == 'undefined' ? '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js' : pluginSrc;
  484.  
  485. // no jquery at this point use pure javascript events
  486. if (document.addEventListener) { // For all major browsers, except IE 8 and earlier
  487. document.addEventListener("bbScriptLoaded", BIGBYTE.USERSCRIPT.STHL.onBbScriptLoaded);
  488. } else if (document.attachEvent) { // For IE 8 and earlier versions
  489. document.attachEvent("onbbScriptLoaded", BIGBYTE.USERSCRIPT.STHL.onBbScriptLoaded);
  490. }
  491.  
  492. if (document.addEventListener) { // For all major browsers, except IE 8 and earlier
  493. document.addEventListener("allScriptsLoaded", BIGBYTE.USERSCRIPT.STHL.onAllScriptsLoaded);
  494. } else if (document.attachEvent) { // For IE 8 and earlier versions
  495. document.attachEvent("onallScriptsLoaded", BIGBYTE.USERSCRIPT.STHL.onAllScriptsLoaded);
  496. }
  497.  
  498. // only add jquery if we need it.
  499. if (typeof(jQuery) == 'undefined') {
  500. this.addScript('jquery', pluginSrc, 'linkedjs', 'jQuery');
  501. }
  502. if ((typeof(jQuery) == 'undefined') || (typeof(jQuery.cookei) == 'undefined')) {
  503. this.addScript('cookie', '//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js', 'linkedjs', 'jQuery.cookie');
  504. }
  505. if (typeof(Clipboard) == 'undefined') {
  506. this.addScript('clipboard', '//cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.5/clipboard.min.js', 'linkedjs', 'Clipboard');
  507. }
  508. this.addScript('icons-css', '//cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css', 'csslink');
  509. // tiny mce
  510. this.addScript('tinyMceJs', '//cdnjs.cloudflare.com/ajax/libs/tinymce/' + tinyMceVer + '/tinymce.min.js', 'linkedjs', 'tinyMCE');
  511. this.addScript('tinyMceCss', '//cdnjs.cloudflare.com/ajax/libs/tinymce/' + tinyMceVer + '/skins/lightgray/skin.min.css', 'csslink');
  512.  
  513. //this.addScript('tinymce_advanced_theme', '//cdnjs.cloudflare.com/ajax/libs/tinymce/' + tinyMceVer + '/themes/advanced/theme.min.js','linkedjs') // no checking required
  514. this.loadScripts();
  515. };
  516. /*
  517. * Load scripts will load one script only at a time.
  518. * When the script is loaded the next if any script will be
  519. * load via the onScripLoadeEvent
  520. * the onScriptLoaded Event calle this function over and over untll all the scripts are loaded
  521. */
  522. sthl.loadScripts = function() {
  523.  
  524. var count = 0;
  525. for (var key in this.scripts) {
  526. count++;
  527. if (count > 1) {
  528. return;
  529. }
  530. BIGBYTE.USERSCRIPT.DOCUMENT.loadScript(this.scripts[key]);
  531. }
  532. };
  533. /*
  534. * Array to hold all the scripts that are to be loaded
  535. */
  536. sthl.scripts = [];
  537. /*
  538. * Adds script item to the BIGBYTE.USERSCRIPT.STHL.scripts array
  539. * these are scripts tha will be loaded when the BIGBYTE.USERSCRIPT.STHL.init() is fired
  540. */
  541. sthl.addScript = function(name, src, type, testMethod, args) {
  542. var newItm = {
  543. name: name,
  544. src: src,
  545. type: type,
  546. testMethod: testMethod
  547. };
  548. if(typeof(args) === undefined) {
  549. this.scripts[name] = newItm;
  550. } else {
  551. var extended = BIGBYTE.USERSCRIPT.UTIL.extend(newItm, args);
  552. this.scripts[name] = extended;
  553. }
  554. };
  555.  
  556. /*
  557. * Function to check and see if there are any scripts left to be loaded
  558. * @returns boolean, true if all the scripts are loaded; Otherwise false
  559. */
  560. sthl.isScriptsLoaded = function() {
  561. var lib = BIGBYTE.USERSCRIPT.STHL;
  562. for (var key in lib.scripts) {
  563. if (!lib.scripts[key].loaded) {
  564. return false
  565. }
  566. }
  567. return true;
  568. };
  569.  
  570.  
  571.  
  572. // #endregion init
  573. // #region Properties
  574. sthl.scriptOutputted = false;
  575. sthl.windowScrollPos = 0;
  576. sthl.includeLineNumbers = true;
  577. /**
  578. * Property that is set to true if the code has had style added to it
  579. * @type {Boolean}
  580. */
  581. sthl.isCodeStyled = false;
  582.  
  583. // light box related
  584. sthl.lightBoxCss = '.gmbackdrop,.gmbox{position:absolute;display:none}.gmbackdrop{top:0;left:0;width:100%;height:100%;background:#000;opacity:0;';
  585. sthl.lightBoxCss += 'filter:alpha(opacity=0);z-index:201}.gmbox{background:#fff;z-index:202;padding:10px;';
  586. sthl.lightBoxCss += '-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;-moz-box-shadow:0 0 5px #444;-webkit-box-shadow:0 0 5px #444;';
  587. sthl.lightBoxCss += 'box-shadow:0 0 5px #444}.gmclose{float:right;margin-right:6px;cursor:pointer}.mce-panel{border:none}div.gmbox .mce-panel{border:';
  588. sthl.lightBoxCss += ' 0 solid rgba(0,0,0,.2)}div.mce-tinymce.mce-container.mce-panel{margin-top:2em}div.mce-tinymce.mce-container.mce-panel.mce-fullscreen';
  589. sthl.lightBoxCss += '{margin-top:0}#gm-edit-btn{font-size:1.6em;color:#ABABAB}#gm-edit-btn:hover{color:#2DBE60}';
  590. sthl.lightBoxCss += '.gmbox-window{top:50%;left:50%;transform: translate(-50%, -50%);position: absolute;';
  591. sthl.lightBoxCss += 'width:650px;height:450px;}';
  592. // #endregion Properties
  593. // #region Init Methods
  594. /*
  595. * internal method to add buttons to the toolbar of the page.
  596. * method alos hooks up some of the buttons. Method is called on document.ready
  597. */
  598. sthl.initToolbar = function($) {
  599. var lib = this;
  600. var $btndiv = $('.buttons:first');
  601. $btndiv.append('<button id="gmconvert" type="button" data-clipboard-text="" data-converted="0" disabled="disabled">Popup List</button>');
  602. $btndiv.append('<button id="gmtable" type="button" disabled="disabled">Popup Table</button>');
  603. $btndiv.append('<button id="gmcopy" type="button" disabled="disabled">Copy List</button>');
  604. $btndiv.append('<button id="gmcopytbl" type="button" disabled="disabled">Copy Table</button>');
  605.  
  606.  
  607. $('button#gmconvert').click(function() {
  608. lib.convertClick(this);
  609. });
  610. $('#gmtable').click(function() {
  611. lib.convertTable(this);
  612. });
  613. };
  614. /*
  615. *
  616. * Inits the line number to hook page line number checkbox
  617. * @param $ optional jQuery object
  618. * @desc called from main init
  619. */
  620.  
  621. sthl.initLineNumbers = function($) {
  622. $ = $ || jQuery;
  623. //label#checkBox.bfc selected
  624. //includeLineNumbers
  625. var lib = this;
  626. $('input#lineNumber').click(function() {
  627. if ($(this).parent('label').hasClass('selected')) {
  628. lib.includeLineNumbers = true;
  629. } else {
  630. lib.includeLineNumbers = false;
  631.  
  632. }
  633. });
  634. };
  635. // #endregion Init Methods
  636. // #region Methods
  637. /*
  638. * resets the lightbox back to hidden state
  639. */
  640. sthl.lightBoxReset = function() {
  641. jQuery('.gmbackdrop, .gmbox').animate({
  642. 'opacity': '0'
  643. }, 300, 'linear', function() {
  644. jQuery('.gmbackdrop, .gmbox').css('display', 'none');
  645. });
  646. jQuery('textarea#gminput').val(''); // clean up textarea
  647. };
  648.  
  649. /**
  650. * Converts a list (ul, ol) elements into a table
  651. * @parm el an element containing the the ul or ol item
  652. * @parm includeNum optional, if true then line numnbers will be incldued; Otherwise false.
  653. * default value is the value the page is currently set to with the line numbers checkbox
  654. */
  655. sthl.toTable = function(el, includeNum, tableClass) {
  656. var $ = jQuery;
  657. var lib = this;
  658. //debugger;
  659. tableClass = tableClass || '';
  660. includeNum = includeNum || this.includeLineNumbers;
  661.  
  662. var $tbl;
  663. if (tableClass) {
  664. $tbl = $('<table />');
  665. } else {
  666. $tbl = $('<table class="' + tableClass + '" />');
  667. }
  668. var count = 0;
  669. el.find('li').each(function(index, li) {
  670. var $li = BIGBYTE.isjquery(li);
  671. var box = $li.hasClass('box');
  672. var boxTop = $li.hasClass('box-top');
  673. var boxMid = $li.hasClass('box-mid');
  674. var boxBot = $li.hasClass('box-bot');
  675. var boxBg = $li.hasClass('box-bg');
  676. count++;
  677. var $row = $('<tr/>');
  678. if (includeNum) {
  679. var $cellNum = $('<td/>').css({
  680. paddingLeft: '5px',
  681. width: '35px',
  682. textAlign: 'right',
  683. whiteSpace: 'nowrap',
  684. paddingRight: '5px'
  685. });
  686. if (box) {
  687. $cellNum.css({
  688. border: $li.css('border'),
  689. borderColor: $li.css('border-color'),
  690. borderRight: '0'
  691. });
  692. if (boxTop) {
  693. $cellNum.css({
  694. borderTop: $li.css('border-top'),
  695. borderLeft: $li.css('border-left'),
  696. borderLeftColor: $li.css('border-left-color')
  697. });
  698. }
  699. if (boxMid) {
  700. $cellNum.css({
  701. borderLeft: $li.css('border-left'),
  702. borderLeftColor: $li.css('border-left-color')
  703. });
  704. }
  705. if (boxBot) {
  706. $cellNum.css({
  707. borderBottom: $li.css('border-bottom'),
  708. borderBottomColor: $li.css('border-bottom-color'),
  709. borderLeft: $li.css('border-left'),
  710. borderLeftColor: $li.css('border-left-color')
  711. });
  712. }
  713. if (boxBg) {
  714. $cellNum.css({
  715. backgroundColor: $li.css('background-color')
  716. });
  717. }
  718. }
  719. $cellNum.html(BIGBYTE.STRING.pad(count, 2) + '.');
  720. $row.append($cellNum);
  721.  
  722. }
  723. var $cellMain = $('<td/>');
  724. $cellMain = lib._copyCss($cellMain, li);
  725. $cellMain.css({
  726. listStyle: '',
  727. //wordWrap: 'normal',
  728. lineHeight: '',
  729. fontSize: '',
  730. fontFamily: '',
  731. 'word-wrap': 'break-word'
  732. });
  733. if (includeNum) {
  734. $cellMain.css({
  735. width: '100%'
  736. });
  737. } else {
  738. $cellMain.css({
  739. width: '100%',
  740. paddingLeft: '10px'
  741. });
  742. }
  743.  
  744. if (box) {
  745. $cellMain.css({
  746. border: $li.css('border'),
  747. borderColor: $li.css('border-color'),
  748. borderLeft: (includeNum ? '0' : $li.css('border-left'))
  749. });
  750. if (boxTop) {
  751. if (includeNum) {
  752. $cellMain.css({
  753. borderTop: $li.css('border-top'),
  754. borderLeft: '0',
  755. borderRight: $li.css('border-right'),
  756. borderRightColor: $li.css('border-Right-color')
  757. });
  758. } else {
  759. $cellMain.css({
  760. borderTop: $li.css('border-top'),
  761. borderLeft: $li.css('border-left'),
  762. borderLeftColor: $li.css('border-left-color'),
  763. borderRight: $li.css('border-right'),
  764. borderRightColor: $li.css('border-Right-color')
  765. });
  766. }
  767.  
  768. }
  769. if (boxMid) {
  770. if (includeNum) {
  771. $cellMain.css({
  772. borderLeft: '0',
  773. borderRight: $li.css('border-right'),
  774. borderRightColor: $li.css('border-Right-color')
  775. });
  776. } else {
  777. $cellMain.css({
  778. borderLeft: $li.css('border-left'),
  779. borderLeftColor: $li.css('border-left-color'),
  780. borderRight: $li.css('border-right'),
  781. borderRightColor: $li.css('border-Right-color')
  782. });
  783. }
  784.  
  785. }
  786. if (boxBot) {
  787. if (includeNum) {
  788. $cellMain.css({
  789. borderBottom: $li.css('border-bottom'),
  790. borderBottomColor: $li.css('border-bottom-color'),
  791. borderLeft: '0',
  792. borderRight: $li.css('border-right'),
  793. borderRightColor: $li.css('border-Right-color')
  794. });
  795. } else {
  796. $cellMain.css({
  797. borderBottom: $li.css('border-bottom'),
  798. borderBottomColor: $li.css('border-bottom-color'),
  799. borderLeft: $li.css('border-left'),
  800. borderLeftColor: $li.css('border-left-color'),
  801. borderRight: $li.css('border-right'),
  802. borderRightColor: $li.css('border-Right-color')
  803. });
  804. }
  805. }
  806. if (boxBg) {
  807. $cellMain.css({
  808. backgroundColor: $li.css('background-color')
  809. });
  810. }
  811. }
  812. $cellMain.html($(this).html());
  813. $row.append($cellMain);
  814. $tbl.append($row);
  815. });
  816. // set the default css styles
  817. $tbl = this._copyCss($tbl, el);
  818. // corrected css styles
  819. $tbl.css({
  820. whiteSpace: 'nowrap',
  821. listStyle: '',
  822. wordWrap: 'normal',
  823. 'border-collapse': 'collapse',
  824. 'table-layout': 'fixed',
  825. width: '100%'
  826. });
  827. return $tbl;
  828. }
  829. sthl.createToggleButton = function() {
  830. var $ = jQuery;
  831. if ($('btncustomtoggle').length === 0) {
  832. $('<div id="customtoggle"><button id="btncustomtoggle" type="buton" style="display: none">Toggle</button></div>').insertBefore('#htmlToCopy');
  833. $('#btncustomtoggle').click(function() {
  834. tinyMCE.execCommand('mceToggleEditor', true, 'htmlToCopy');
  835. });
  836. }
  837.  
  838. };
  839. /*
  840. * internal copies specifice class files from toEl to el.
  841. */
  842.  
  843. sthl._copyCss = function(toEl, el) {
  844. var $toEl = BIGBYTE.isjquery(toEl);
  845. var $el = BIGBYTE.isjquery(el);
  846.  
  847. $toEl.css({
  848. paddingLeft: $el.css('padding-left'),
  849. listStyle: $el.css('list-style'),
  850. backgroundColor: $el.css('background-color'),
  851. lineHeight: $el.css('line-height'),
  852. borderLeft: $el.css('border-left'),
  853. color: $el.css('color'),
  854. fontWeight: $el.css('font-weight'),
  855. fontStyle: $el.css('font-style'),
  856. fontSize: $el.css('font-size'),
  857. fontFamily: $el.css('font-family'),
  858. textAlign: $el.css('text-align'),
  859. whiteSpace: $el.css('white-space'),
  860. wordWrap: $el.css('word-wrap')
  861. })
  862. return $toEl;
  863. };
  864.  
  865. sthl.lightBoxAddCss = function() {
  866. var bdoc = BIGBYTE.USERSCRIPT.DOCUMENT;
  867. bdoc.addCss_Node(this.lightBoxCss, 'body');
  868. };
  869.  
  870. sthl.getLightBoxHtml = function(id, title) {
  871. id = typeof id !== 'undefined' ? id : 'gminput';
  872. title = typeof title !== 'undefined' ? title : '';
  873. var h = '<div class="gmbackdrop"></div>';
  874. h += '<div id="tinybox" class="gmbox gmbox-window"><div class="gmclose"><i class="fi-x" style="color:black"></i></div>';
  875. h += title;
  876. h += '<textarea id="' + id + '" rows="18" cols="68"></textarea>';
  877. h += '</div></div>';
  878. return h;
  879. }
  880.  
  881. sthl.writeLightBox = function(id, title) {
  882. var html = this.getLightBoxHtml(id, title);
  883. var bdoc = BIGBYTE.USERSCRIPT.DOCUMENT;
  884. bdoc.addHtml_Node(html);
  885. };
  886.  
  887. /**
  888. * Init that fires when Clibporad Lib is loaded
  889. */
  890. sthl.clipboardInit = function() {
  891.  
  892. var clipboard = new Clipboard('#gmcopy', {
  893. text: function(trigger) {
  894. var $ = jQuery;
  895. $.event.trigger({
  896. type: 'codeConversionStart',
  897. message: 'convert_list_start',
  898. time: new Date(),
  899. source: 'gmcopy'
  900. });
  901.  
  902. var contentHtml;
  903. try {
  904. contentHtml = BIGBYTE.USERSCRIPT.STHL.getConvertedList();
  905. } catch (e) {
  906. // statements
  907. console.log(e);
  908. }
  909. $.event.trigger({
  910. type: 'codeConversionFinish',
  911. message: 'convert_list_end',
  912. time: new Date(),
  913. source: 'gmcopy',
  914. html: contentHtml
  915. });
  916. return contentHtml;
  917. }
  918. });
  919. var clipboardTbl = new Clipboard('#gmcopytbl', {
  920. text: function(trigger) {
  921. var $ = jQuery;
  922. $.event.trigger({
  923. type: 'codeConversionStart',
  924. message: 'convert_table_start',
  925. time: new Date(),
  926. source: 'gmcopytbl'
  927. });
  928. var contentHtml;
  929. try {
  930. contentHtml = BIGBYTE.USERSCRIPT.STHL.getConvertedTable();
  931. } catch (e) {
  932. // statements
  933. console.log(e);
  934. }
  935.  
  936. $.event.trigger({
  937. type: 'codeConversionFinish',
  938. message: 'convert_table_end',
  939. time: new Date(),
  940. source: 'gmcopytbl',
  941. html: contentHtml
  942. });
  943. return contentHtml;
  944.  
  945. }
  946. });
  947.  
  948. clipboard.on('success', function(e) {
  949. console.info('Action:', e.action);
  950. //console.info('Text:', e.text);
  951. console.info('Trigger:', e.trigger);
  952. alert('Data as been copied to the clipboard');
  953. });
  954.  
  955. clipboard.on('error', function(e) {
  956. console.error('Action:', e.action);
  957. console.error('Trigger:', e.trigger);
  958. alert('Unable to copy data to the clipboard');
  959. });
  960.  
  961. clipboardTbl.on('success', function(e) {
  962. console.info('Action:', e.action);
  963. console.info('Trigger:', e.trigger);
  964. alert('Data as been copied to the clipboard');
  965. });
  966.  
  967. clipboardTbl.on('error', function(e) {
  968. console.error('Action:', e.action);
  969. console.error('Trigger:', e.trigger);
  970. alert('Unable to copy data to the clipboard');
  971. });
  972. };
  973.  
  974. /**
  975. * Calls page like method to relaod the code from the origiona source
  976. * @return {[boolean]} returns true if the code was reset; Otherwise false
  977. */
  978. sthl.reHighlight = function() {
  979. if ($("#dropZone").val() == "") {
  980. $(".doneButton").attr("disabled", true);
  981. return false;
  982. }
  983. $("#theWholeEnchilada").html('<pre class="shi_pre"></pre>');
  984. $("#theWholeEnchilada .shi_pre").text($("#dropZone").val());
  985. languageSelected = $("#language_selected option:selected").val();
  986. styleSelected = $("#style_selected option:selected").val();
  987. lineNumber = $("#lineNumber").is(":checked");
  988. collapsing = ($("#collapsing").is(":checked") || $("#startCollapsing").is(":checked"));
  989. startCollapsing = $("#startCollapsing").is(":checked");
  990. boxStyling = $("#boxStyling").val();
  991. boxColorStyling = $("#boxColorStyling").val();
  992. boxFillStyling = $("#boxFillStyling").val();
  993. $("pre.shi_pre").snippet(languageSelected, {
  994. style: styleSelected,
  995. showNum: lineNumber,
  996. collapse: collapsing,
  997. startCollapsed: startCollapsing,
  998. box: boxStyling,
  999. boxColor: boxColorStyling,
  1000. boxFill: boxFillStyling
  1001. })
  1002. return true;
  1003. };
  1004.  
  1005. /**
  1006. * Set the page control value select elements if there are cookie value
  1007. * otherwise save the cookie values
  1008. */
  1009. sthl.setFromCookies = function() {
  1010. if (typeof($.cookie('language_selected_val')) == 'undefined') {
  1011. $.cookie('language_selected_val', $("#language_selected option").filter(":selected").val(), {
  1012. expires: 14
  1013. });
  1014. } else {
  1015. $("#language_selected").val($.cookie('language_selected_val'));
  1016. }
  1017.  
  1018. if (typeof($.cookie('style_selected_val')) == 'undefined') {
  1019. $.cookie('style_selected_val', $("#style_selected option").filter(":selected").val(), {
  1020. expires: 14
  1021. });
  1022. } else {
  1023. $("#style_selected").val($.cookie('style_selected_val'));
  1024. }
  1025. };
  1026.  
  1027. // #endregion Methods
  1028. // #region ensure Methods
  1029. sthl.ensurePlugins = function($) {
  1030. if (typeof($.fn.enableScroll) == 'undefined') {
  1031. $.extend($.fn, {
  1032. enableScroll: function(selector, height) {
  1033. height = height || 'auto';
  1034. if ((typeof(selector) == 'undefined') || (selector == null)) {
  1035. $(this).css({
  1036. 'overflow': 'auto',
  1037. 'height': height
  1038. });
  1039.  
  1040. } else {
  1041. $(this).find(selector).css({
  1042. 'overflow': 'auto',
  1043. 'height': height
  1044. });
  1045. }
  1046. }
  1047. });
  1048. }
  1049. if (typeof($.fn.disableScroll) == 'undefined') {
  1050. $.extend($.fn, {
  1051. disableScroll: function(selector, height) {
  1052. height = height || '100%';
  1053. if ((typeof(selector) == 'undefined') || (selector == null)) {
  1054. $(this).css({
  1055. 'overflow': 'hidden',
  1056. 'height': height
  1057. });
  1058.  
  1059. } else {
  1060. $(this).find(selector).css({
  1061. 'overflow': 'hidden',
  1062. 'height': height
  1063. });
  1064. }
  1065. }
  1066. });
  1067. }
  1068. if (typeof($.fn.spaceReplace) == 'undefined') {
  1069. $.extend($.fn, {
  1070. spaceReplace: function(recurse) {
  1071. recurse = typeof(recurse) == 'undefined' ? false : recurse;
  1072.  
  1073. if (recurse) {
  1074. return this.find('*').each(function(index, value) {
  1075. try {
  1076. if (typeof(value) == 'undefined') {
  1077. return;
  1078. }
  1079. $(value)._spaceRepaceSingle();
  1080. } catch (e) {
  1081. console.log('spaceReplace error:', e);
  1082. }
  1083.  
  1084. });
  1085. } else {
  1086. return this._spaceRepaceSingle();
  1087. }
  1088.  
  1089. },
  1090. _spaceRepaceSingle: function() {
  1091. this.contents().filter(function() {
  1092. return this.nodeType == 3; // Text node
  1093. }).each(function() {
  1094. this.data = this.data.replace(/ /g, '\u00a0');
  1095. });
  1096. return this;
  1097. }
  1098. });
  1099. }
  1100. if (typeof($.fn.removeAttrib) == 'undefined') {
  1101. $.extend($.fn, {
  1102. removeAttrib: function(attributeName, recurse) {
  1103. recurse = typeof(recurse) == 'undefined' ? false : recurse;
  1104.  
  1105. if (recurse) {
  1106. return $(this).find('*').each(function(index, value) {
  1107. try {
  1108. if (typeof(value) == 'undefined') {
  1109. return;
  1110. }
  1111. $(value).removeAttr(attributeName);
  1112. } catch (e) {
  1113. // statements
  1114. console.log('error:', e);
  1115. }
  1116.  
  1117. });
  1118. } else {
  1119. return $(this).removeAttr(attributeName);
  1120. }
  1121. }
  1122. });
  1123. }
  1124. };
  1125.  
  1126. /*
  1127. * internal method that creates/loads makeCssInlineCode jQuery plugin
  1128. * this plugin converts specific the css style of the element and child elemets into inline css style
  1129. */
  1130. sthl.ensureMakeCssInlineCodePlugin = function($) {
  1131. if (typeof($.fn.makeCssInlineCode) == 'undefined') {
  1132. $.extend($.fn, {
  1133. makeCssInlineCode: function(forTable) {
  1134. // forTable if true then will generate css a little diffferently for html table
  1135. forTable = typeof(forTable) == 'undefined' ? false : forTable;
  1136.  
  1137. this.each(function(idx, el) {
  1138. var $this = $(el);
  1139. var tagName = $this.tagName(true);
  1140. if ($this.attr('class')) {
  1141. // if element has a class then set the styles
  1142. $this.css({
  1143. color: $this.css('color'),
  1144. fontWeight: $this.css('font-weight'),
  1145. fontStyle: $this.css('font-style'),
  1146. //fontSize: $this.css('font-size'),
  1147. //fontFamily: $this.css('font-family'),
  1148. textAlign: $this.css('text-align'),
  1149. //whiteSpace: $this.css('white-space'),
  1150. //wordWrap: $this.css('word-wrap')
  1151. });
  1152. }
  1153. // get tagname in lowercase format;
  1154. switch (tagName) {
  1155. case 'li':
  1156. $this.css({
  1157. paddingLeft: $this.css('padding-left'),
  1158. listStyle: $this.css('list-style'),
  1159. backgroundColor: $this.css('background-color'),
  1160. lineHeight: $this.css('line-height'),
  1161. //borderLeft: $this.css('border-left'),
  1162. borderTop: $this.css('border-top'),
  1163. borderRight: $this.css('border-right'),
  1164. borderBottom: $this.css('border-bottom'),
  1165. borderLeftColor: $this.css('border-left-color'),
  1166. borderTopColor: $this.css('border-top-color'),
  1167. borderRightColor: $this.css('border-right-color'),
  1168. borderBottomColor: $this.css('border-bottom-color'),
  1169. borderLeft: $this.css('border-left'),
  1170. color: $this.css('color'),
  1171. fontWeight: $this.css('font-weight'),
  1172. fontStyle: $this.css('font-style'),
  1173. //fontSize: $this.css('font-size'),
  1174. //fontFamily: $this.css('font-family'),
  1175. textAlign: $this.css('text-align'),
  1176. whiteSpace: $this.css('white-space'),
  1177. wordWrap: $this.css('word-wrap'),
  1178. margin: $this.css('margin'),
  1179. 'list-style-position': $this.css('list-style-position')
  1180.  
  1181. });
  1182. break;
  1183. case 'ol': // line numbers
  1184. var padding = $this.css('-webkit-padding-start');
  1185. $this.css({
  1186. 'list-style-type': 'decimal',
  1187. display: 'block',
  1188. '-webkit-margin-before': '1em',
  1189. '-moz-margin-before': '1em',
  1190. '-khtml-margin-before': '1em',
  1191. '-o-margin-before': '1em',
  1192.  
  1193. '-webkit-margin-after': '1em',
  1194. '-moz-margin-after': '1em',
  1195. '-khtml-margin-after': '1em',
  1196. '-o-margin-after': '1em',
  1197.  
  1198. '-webkit-margin-start': '0px',
  1199. '-moz-margin-start': '0px',
  1200. '-khtml-margin-start': '0px',
  1201. '-o-margin-start': '0px',
  1202.  
  1203. '-webkit-margin-end': '0px',
  1204. '-moz-margin-end': '0px',
  1205. '-khtml-margin-end': '0px',
  1206. '-o-margin-end': '0px',
  1207.  
  1208. '-moz-padding-start': padding,
  1209. '-khtml-padding-start': padding,
  1210. '-o-padding-start': padding,
  1211. 'padding-start': padding,
  1212. 'padding-left': padding
  1213. });
  1214.  
  1215. break;
  1216. case 'ul': // no line numbers
  1217. $this.css({
  1218. 'list-style-type': 'decimal',
  1219. display: 'block',
  1220. '-webkit-margin-before': '1em',
  1221. '-moz-margin-before': '1em',
  1222. '-khtml-margin-before': '1em',
  1223. '-o-margin-before': '1em',
  1224.  
  1225. '-webkit-margin-after': '1em',
  1226. '-moz-margin-after': '1em',
  1227. '-khtml-margin-after': '1em',
  1228. '-o-margin-after': '1em',
  1229.  
  1230. '-webkit-margin-start': '0px',
  1231. '-moz-margin-start': '0px',
  1232. '-khtml-margin-start': '0px',
  1233. '-o-margin-start': '0px',
  1234. '-webkit-margin-end': '0px',
  1235. '-moz-margin-end': '0px',
  1236. '-khtml-margin-end': '0px',
  1237. '-o-margin-end': '0px',
  1238.  
  1239. '-moz-padding-start': '0',
  1240. '-khtml-padding-start': '0',
  1241. '-o-padding-start': '0',
  1242. 'padding-start': '0',
  1243. 'padding-left': '0'
  1244. });
  1245. break;
  1246. case 'span':
  1247. if ((!forTable) && ($this.hasClass('box-sp'))) {
  1248. $this.css({
  1249. display: $this.css('display'),
  1250. width: $this.css('width')
  1251. });
  1252. } else {
  1253. $this.css({
  1254. display: '',
  1255. width: ''
  1256. });
  1257. }
  1258. break;
  1259. default:
  1260. // statements_def
  1261. break;
  1262. }
  1263.  
  1264. //var style = el.style;
  1265. //$this.removeAttr('class'); // remove class from element
  1266. $this.children().makeCssInlineCode(forTable);
  1267. });
  1268. }
  1269. });
  1270. }
  1271. };
  1272. /*
  1273. * internal method that creates/loads makeCssInline jQuery plugin
  1274. * this plugin converts all the css style of the element and child elemets into inline css style
  1275. */
  1276. sthl.ensureMakeCssInlinePlugin = function($) {
  1277. if (typeof($.fn.makeCssInline) == 'undefined') {
  1278. $.extend($.fn, {
  1279. makeCssInline: function(recurse) {
  1280. recurse = typeof(recurse) == 'undefined' ? false : recurse;
  1281. if (recurse) {
  1282. return this.find('*').each(function(index, value) {
  1283. try {
  1284. if (typeof(value) == 'undefined') {
  1285. return;
  1286. }
  1287. $(value)._makeCssInlineSingle();
  1288. } catch (e) {
  1289. console.log('makeCssInline error:', e);
  1290. }
  1291.  
  1292. });
  1293. } else {
  1294. return this.each(function(index, value) {
  1295. $(value)._makeCssInlineSingle();
  1296. })
  1297. }
  1298. },
  1299. _makeCssInlineSingle: function() {
  1300.  
  1301. var el = this[0];
  1302.  
  1303. var style = el.style;
  1304. var properties = [];
  1305. for (var property in style) {
  1306. if (this.css(property)) {
  1307. properties.push(property + ':' + this.css(property));
  1308. }
  1309. }
  1310.  
  1311. el.style.cssText = properties.join(';');
  1312. return this;
  1313. }
  1314. });
  1315. }
  1316.  
  1317. if (typeof($.fn.assignStyleFrom) == 'undefined') {
  1318. /**
  1319. * Assigns style of one jQuery Object to current object
  1320. * @param {[jQuery]} el) A jQuery object or dom element
  1321. * @return {[jQuery]} jQuery object with the new style assigned
  1322. */
  1323. $.extend($.fn, {
  1324. assignStyleFrom: function(el) {
  1325. if (typeof(el) == 'undefined') {
  1326. return this;
  1327. }
  1328. var $el;
  1329. if (el instanceof jQuery) {
  1330. $el = el;
  1331. } else {
  1332. $el = jQuery(el);
  1333. }
  1334. if ($el.length === 0) {
  1335. return this;
  1336. }
  1337. var style = $el[0].style;
  1338. var properties = [];
  1339. for (var property in style) {
  1340. if ($el.css(property)) {
  1341. properties.push(property + ':' + $el.css(property));
  1342. }
  1343. }
  1344. this[0].style.cssText = properties.join(';');
  1345. return this;
  1346. }
  1347. });
  1348. }
  1349. };
  1350.  
  1351. // #endregion enusre Methods
  1352. // #region Events
  1353. /*
  1354. * Event Handler that is fired when a script is loaded
  1355. * This event fires each time a script is loaded.
  1356. */
  1357. sthl.onBbScriptLoaded = function(e) {
  1358. var lib = BIGBYTE.USERSCRIPT.STHL;
  1359. // delete the added script
  1360.  
  1361. delete lib.scripts[e.detail.scriptItm.name];
  1362. var done = lib.isScriptsLoaded();
  1363. if (done) {
  1364. var allScriptsLoaded = new CustomEvent(
  1365. "allScriptsLoaded", {
  1366. detail: {
  1367. message: "All Scripts Loaded",
  1368. time: new Date(),
  1369. },
  1370. bubbles: true,
  1371. cancelable: false
  1372. }
  1373. );
  1374. document.dispatchEvent(allScriptsLoaded);
  1375. //jQuery(document).trigger("allScriptsLoaded");
  1376. } else {
  1377. // add the next script
  1378. lib.loadScripts();
  1379. }
  1380. }
  1381.  
  1382. /*
  1383. * Event Handler that fires when all scripts are loaded
  1384. * this is main loading point for the script.
  1385. */
  1386. sthl.onAllScriptsLoaded = function(e) {
  1387. console.log('all scripts have been loaded.');
  1388. jQuery(function($, undefined) {
  1389. var lib = BIGBYTE.USERSCRIPT.STHL;
  1390.  
  1391. if (typeof($.fn.tagName) == 'undefined') {
  1392. // in older version of jquery tagname was retturned in uppercase but now is in lowercase
  1393. $.fn.tagName = function(toLower) {
  1394. var tn = this.prop("tagName");
  1395. if (toLower) {
  1396. tn = tn.toLowerCase();
  1397. }
  1398. return tn;
  1399. };
  1400. }
  1401.  
  1402. // hide the intro section of the webpage
  1403. $('#intro').hide();
  1404.  
  1405. $(document).on("tinymceInit", lib.onTinymceInit);
  1406. $(document).on("tinymceCancel", lib.onTinymceCancel);
  1407. $(document).on("codeConversionStart", lib.onCodeConversionStart);
  1408. $(document).on("codeConversionFinish", lib.onCodeConversionFinish);
  1409. // $(document).on("clipboardLoaded", lib.clipboardInit);
  1410. lib.clipboardInit();
  1411. $('button.startOver').on('click', lib.onStartOverClick);
  1412. $('button.doneButton').unbind('click').on('click', lib.onDoneButtonClick);
  1413.  
  1414. // load the values from cookies and set page controls
  1415. lib.setFromCookies();
  1416.  
  1417. // events for page change
  1418. $('#dropZone').on('change', {
  1419. source: 'dropZone',
  1420. type: 'textarea'
  1421. }, lib.onPageMainChanged);
  1422. $('select#language_selected').on("change", {
  1423. source: 'language_selected',
  1424. type: 'dropdown'
  1425. }, lib.onPageMainChanged);
  1426. $('select#style_selected').on('change', {
  1427. source: 'style_selected',
  1428. type: 'dropdown'
  1429. }, lib.onPageMainChanged);
  1430. $('input#lineNumber').on('change', {
  1431. source: 'lineNumber',
  1432. type: 'checkbox'
  1433. }, lib.onPageMainChanged);
  1434. $('input#boxColorStyling').on('change', {
  1435. source: 'boxColorStyling',
  1436. type: 'input'
  1437. }, lib.onPageMainChanged);
  1438. $('input#boxFillStyling').on('change', {
  1439. source: 'boxFillStyling',
  1440. type: 'input'
  1441. }, lib.onPageMainChanged);
  1442. $('button.startOver').on('click', {
  1443. source: 'button.startOver',
  1444. type: 'button'
  1445. }, lib.onPageReset);
  1446.  
  1447. $(document).on('tinymceFullScreen', lib.onTinyMceFulllscreen);
  1448.  
  1449. $('body').append('<input id="gmhidden" type="hidden" value="" />');
  1450.  
  1451. lib.ensureMakeCssInlinePlugin($);
  1452. lib.ensureMakeCssInlineCodePlugin($);
  1453. lib.ensurePlugins($);
  1454. lib.initToolbar($);
  1455. lib.lightBoxAddCss();
  1456. lib.writeLightBox();
  1457. lib.createToggleButton(); // create the toggle button to toggle tinymce
  1458. lib.TMCE.init();
  1459.  
  1460. sthl.initLineNumbers($);
  1461.  
  1462. $('.gmclose').click(function() {
  1463. $.event.trigger({
  1464. type: "tinymceCancel",
  1465. message: 'cancel',
  1466. time: new Date(),
  1467. tinyMceId: 'gminput'
  1468. });
  1469. });
  1470.  
  1471.  
  1472. });
  1473. };
  1474. /**
  1475. * Event that fires when the page code elements have changed
  1476. */
  1477. sthl.onPageMainChanged = function(e) {
  1478. var lib = BIGBYTE.USERSCRIPT.STHL;
  1479. var $ = jQuery;
  1480. if ($("#dropZone").val() == "") {
  1481. lib.toggleButtons(false);
  1482. } else {
  1483. lib.toggleButtons(true);
  1484. }
  1485.  
  1486. switch (e.data.source) {
  1487. case 'language_selected': // dropeown language has been changed
  1488. $.cookie('language_selected_val', $("#language_selected option").filter(":selected").val(), {
  1489. expires: 14
  1490. });
  1491. break;
  1492. case 'style_selected':
  1493. $.cookie('style_selected_val', $("#style_selected option").filter(":selected").val(), {
  1494. expires: 14
  1495. });
  1496. break;
  1497. default:
  1498. // statements_def
  1499. break;
  1500. }
  1501. };
  1502.  
  1503. /**
  1504. * Event that fire when the page reset takes place
  1505. * @param {[objece]} e event args
  1506. */
  1507. sthl.onPageReset = function(e) {
  1508. var lib = BIGBYTE.USERSCRIPT.STHL;
  1509. lib.toggleButtons(false);
  1510. lib.isCodeStyled = false;
  1511. // set elements from cookies
  1512. lib.setFromCookies();
  1513. };
  1514.  
  1515. sthl.onDoneButtonClick = function(e) {
  1516.  
  1517. if ($("#dropZone").val() == "") {
  1518. return
  1519. }
  1520. var lib = BIGBYTE.USERSCRIPT.STHL;
  1521. // highlight the code again if it has been styled
  1522. if (lib.isCodeStyled == true) {
  1523.  
  1524. lib.reHighlight();
  1525. lib.isCodeStyled == false;
  1526. }
  1527.  
  1528. if ($("#step4:visible")) {
  1529. $("#step4").slideToggle()
  1530. }
  1531. $("#step5").slideToggle();
  1532. $(this).parents(".wrapper").slideToggle();
  1533. $("#theWholeEnchilada").css("height", "0");
  1534. $("#htmlToCopy").val($("#theWholeEnchilada").html());
  1535. document.getElementById($("#style_selected").val()).style.display = "block"
  1536.  
  1537. // add tiny mce toggle option on top of the
  1538. // formated source code window
  1539. var nsTmce = BIGBYTE.USERSCRIPT.STHL.TMCE;
  1540. jQuery('#btncustomtoggle').show();
  1541. nsTmce.initHtmlTocopy();
  1542. tinyMCE.execCommand('mceToggleEditor', true, 'htmlToCopy');
  1543.  
  1544. };
  1545. sthl.onStartOverClick = function(e) {
  1546. var $ = jQuery;
  1547. var lib = BIGBYTE.USERSCRIPT.STHL;
  1548. tinyMCE.execCommand('mceRemoveEditor', true, 'htmlToCopy');
  1549. $('btncustomtoggle').hide();
  1550. // fixes issue with theWholeEnchilada have a height of auto after reset
  1551. // this fix is only needed due to this script making a chnage somewhere
  1552. // along the way
  1553. $('#theWholeEnchilada').css({
  1554. height: 'auto'
  1555. });
  1556. lib.isCodeStyled = false;
  1557.  
  1558. };
  1559. sthl.onCodeConversionStart = function(e) {
  1560. var lib = BIGBYTE.USERSCRIPT.STHL;
  1561. if (lib.reHighlight()) {
  1562. //console.log('code is rewritten');
  1563. } else {
  1564. //console.log('unable to rewrite code');
  1565. }
  1566. };
  1567. sthl.onCodeConversionFinish = function(e) {
  1568. var lib = BIGBYTE.USERSCRIPT.STHL;
  1569. lib.toggleButtons(true);
  1570. // set the property that indicates
  1571. // if the colorized code has had syyles added to it
  1572. lib.isCodeStyled = true;
  1573. };
  1574.  
  1575.  
  1576. /**
  1577. * Event that fire when TinyMce is initiated
  1578. */
  1579. sthl.onTinymceInit = function(e) {
  1580. //console.log('Tiny Mce Init was triggered');
  1581. };
  1582. /**
  1583. * Event that fire when TinyMce save is clicked
  1584. */
  1585. sthl.onTinymceSave = function(e) {
  1586. var $ = jQuery;
  1587. if (e.tinyMceId == 'gminput') {
  1588. //console.log('Tiny Mce save was triggered');
  1589. BIGBYTE.USERSCRIPT.STHL.lightBoxReset();
  1590. var libTmce = BIGBYTE.USERSCRIPT.STHL.TMCE;
  1591. if ($('body').hasClass('mce-fullscreen')) {
  1592. $('body').removeClass('mce-fullscreen');
  1593. }
  1594. $(document).enableScroll('html, body');
  1595. // $('body').animate({ scrollTop: this.windowScrollPos}, 'slow');
  1596. $('body').scrollTop(this.windowScrollPos);
  1597. }
  1598.  
  1599. };
  1600. /**
  1601. * Event that fire when TinyMce close is clicked
  1602. */
  1603. sthl.onTinymceCancel = function(e) {
  1604. var $ = jQuery;
  1605. if (e.tinyMceId == 'gminput') {
  1606. BIGBYTE.USERSCRIPT.STHL.lightBoxReset();
  1607. tinymce.get('gminput').setContent(''); // clean up tinymce
  1608. var libTmce = BIGBYTE.USERSCRIPT.STHL.TMCE;
  1609. if ($('body').hasClass('mce-fullscreen')) {
  1610. $('body').removeClass('mce-fullscreen');
  1611. }
  1612. $(document).enableScroll('html, body');
  1613. $('body').scrollTop(this.windowScrollPos);
  1614. }
  1615.  
  1616. };
  1617. sthl.onTinyMceFulllscreen = function(e) {
  1618. var $ = jQuery;
  1619. if (e.tinyMceId == 'gminput') {
  1620. if (e.state) {
  1621. if ($('#tinybox').hasClass('gmbox-window')) {
  1622. $('#tinybox').removeClass('gmbox-window');
  1623. }
  1624. } else {
  1625. if (!$('#tinybox').hasClass('gmbox-window')) {
  1626. $('#tinybox').addClass('gmbox-window');
  1627. }
  1628. }
  1629. }
  1630. };
  1631. // #endregion Events
  1632. // #endnsregion
  1633. // #nsregion BIGBYTE.USERSCRIPT.STHL.TMCE
  1634. // #region Create NS
  1635. var tmce = BIGBYTE.createNS("BIGBYTE.USERSCRIPT.STHL.TMCE");
  1636. tmce.ns = 'BIGBYTE.USERSCRIPT.STHL.TMCE';
  1637. // #endregion Create ns
  1638. // #region Properties
  1639. tmce.scriptOutputted = false;
  1640. tmce.fullscreen = false;
  1641. tmce.loopcount = 0;
  1642. tmce.version = '4.3.4';
  1643. // #endregion Properties
  1644. // #region init
  1645. if (typeof(tmce.init) == 'undefined') {
  1646. tmce.init = function() {
  1647. var ver = this.version;
  1648. var id = 'gminput';
  1649. tinyMCE.init({
  1650. selector: 'textarea#' + id,
  1651. visual: false, // turn off visual aids by default
  1652. //entity_encoding: 'named',
  1653. //entities: '160,nbsp',
  1654. init_instance_callback: function() {
  1655. jQuery('.mce-i-mysave').addClass('fi-save').css({
  1656. color: 'black'
  1657. });
  1658. // add x icon to button
  1659. jQuery('.mce-i-myexit').addClass('fi-x').css({
  1660. color: 'black'
  1661. });
  1662. jQuery.event.trigger({
  1663. type: "tinymceInit",
  1664. message: 'init',
  1665. time: new Date(),
  1666. tinyMceId: id
  1667. });
  1668. },
  1669. height: 260,
  1670. // extended_valid_elements : "span[!class]",
  1671. inline: false,
  1672. browser_spellcheck: true,
  1673. plugins: "",
  1674. menubar: "edit insert format view tools table",
  1675. toolbar1: 'mysave myexit insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link',
  1676. toolbar2: 'fullscreen print preview media | forecolor backcolor | insertdatetime table searchreplace code',
  1677. external_plugins: {
  1678. 'fullscreen': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/fullscreen/plugin.min.js',
  1679. 'textcolor': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/textcolor/plugin.min.js',
  1680. 'nonbreaking': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/nonbreaking/plugin.min.js',
  1681. 'insertdatetime': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/insertdatetime/plugin.min.js',
  1682. 'code': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/code/plugin.min.js',
  1683. 'hr': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/hr/plugin.min.js',
  1684. 'searchreplace': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/searchreplace/plugin.min.js',
  1685. 'table': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/table/plugin.min.js'
  1686. },
  1687. //valid_elements: 'ol ul',
  1688. //extended_valid_elements: 'ol[|class|style] ul[class|style]',
  1689. keep_styles: false,
  1690. setup: function(ed) {
  1691. // Add a custom button
  1692. ed.on('FullscreenStateChanged', function(e) {
  1693. this.fullscreen = e.state;
  1694. jQuery.event.trigger({
  1695. type: "tinymceFullScreen",
  1696. message: 'fullscreen toogle',
  1697. time: new Date(),
  1698. state: e.state,
  1699. tinyMceId: id
  1700. });
  1701.  
  1702. });
  1703. ed.addButton('myexit', {
  1704. title: 'Close',
  1705. onclick: function() {
  1706. jQuery.event.trigger({
  1707. type: "tinymceCancel",
  1708. message: 'cancel',
  1709. time: new Date(),
  1710. tinyMceId: id
  1711. });
  1712. }
  1713. });
  1714.  
  1715. }
  1716. });
  1717.  
  1718. };
  1719. }
  1720. if (typeof(tmce.initHtmlTocopy) == 'undefined') {
  1721. tmce.initHtmlTocopy = function() {
  1722. var ver = this.version;
  1723. var id = 'htmlToCopy'
  1724. tinyMCE.init({
  1725. selector: 'textarea#' + id,
  1726. visual: false, // turn off visual aids by default
  1727. init_instance_callback: function() {
  1728. jQuery.event.trigger({
  1729. type: "tinymceInit",
  1730. message: 'initHtmlTocopy',
  1731. time: new Date(),
  1732. tinyMceId: id
  1733. });
  1734. },
  1735. height: 260,
  1736. // extended_valid_elements : "span[!class]",
  1737. inline: false,
  1738. browser_spellcheck: true,
  1739. plugins: "",
  1740. menubar: "edit insert format view tools table",
  1741. toolbar1: 'myexit insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link',
  1742. toolbar2: 'fullscreen print preview media | forecolor backcolor | insertdatetime table searchreplace code',
  1743. external_plugins: {
  1744. 'fullscreen': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/fullscreen/plugin.min.js',
  1745. 'textcolor': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/textcolor/plugin.min.js',
  1746. 'nonbreaking': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/nonbreaking/plugin.min.js',
  1747. 'insertdatetime': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/insertdatetime/plugin.min.js',
  1748. 'code': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/code/plugin.min.js',
  1749. 'hr': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/hr/plugin.min.js',
  1750. 'searchreplace': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/searchreplace/plugin.min.js',
  1751. 'table': 'https://cdnjs.cloudflare.com/ajax/libs/tinymce/' + ver + '/plugins/table/plugin.min.js'
  1752. },
  1753. //theme : "advanced",
  1754. theme_advanced_toolbar_location: "top",
  1755. theme_advanced_toolbar_align: "left",
  1756. theme_advanced_statusbar_location: "bottom",
  1757. theme_advanced_resizing: true,
  1758. content_css: 'shi/css/shi_default.min.css',
  1759. setup: function(ed) {
  1760. // Add a custom button
  1761. ed.on('FullscreenStateChanged', function(e) {
  1762.  
  1763. jQuery.event.trigger({
  1764. type: "tinymceFullScreen",
  1765. message: 'fullscreen toogle',
  1766. time: new Date(),
  1767. state: e.state,
  1768. tinyMceId: id
  1769. });
  1770.  
  1771. });
  1772. }
  1773. //theme_advanced_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1"
  1774.  
  1775. });
  1776. };
  1777. }
  1778.  
  1779. // #endregion init
  1780. // #endnsregion BIGBYTE.USERSCRIPT.STHL.TMCE
  1781.  
  1782. // init the lib objects.
  1783. sthl.init();