Skip to content

Commit

Permalink
Make javascript external plus some details
Browse files Browse the repository at this point in the history
  • Loading branch information
aloker committed Oct 17, 2009
1 parent 74d819e commit 8e5c22f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -21,6 +21,6 @@ Currently the following markup languages are supported:
For the licences of the 3rd party libraries see the license.txt files in the respective folder under _3rdParty_ For the licences of the 3rd party libraries see the license.txt files in the respective folder under _3rdParty_


* A demo can be found at <http://markup.andreloker.de> * A demo can be found at <http://markup.andreloker.de>
* [Project page on GitHub.com](http://github.com/aloker/markup-preview) * [Project page with source on GitHub.com](http://github.com/aloker/markup-preview)


~ Andre Loker ~ Andre Loker
15 changes: 7 additions & 8 deletions Source/MarkupPreview/MarkupPreview/Controllers/HomeController.cs
@@ -1,16 +1,15 @@
#region Copyright © 2009 Andre Loker (mail@andreloker.de). All rights reserved. #region Copyright © 2009 Andre Loker (mail@andreloker.de). All rights reserved.

// $Id$ // $Id$
#endregion #endregion


using System;
using System.IO;
using System.Reflection;
using Castle.MonoRail.Framework;
using MarkupPreview.Processing;

namespace MarkupPreview.Controllers namespace MarkupPreview.Controllers
{ {
using System;
using System.IO;
using System.Reflection;
using Castle.MonoRail.Framework;
using Processing;

[Layout("default")] [Layout("default")]
public class HomeController : SmartDispatcherController public class HomeController : SmartDispatcherController
{ {
Expand All @@ -23,7 +22,7 @@ public void Index()


private void LoadMarkupTypes() private void LoadMarkupTypes()
{ {
PropertyBag["markupTypes"] = Enum.GetValues(typeof (MarkupType)); PropertyBag["markupTypes"] = Enum.GetValues(typeof(MarkupType));
} }


public void Process(MarkupType type, string source) public void Process(MarkupType type, string source)
Expand Down
1 change: 1 addition & 0 deletions Source/MarkupPreview/MarkupPreview/MarkupPreview.csproj
Expand Up @@ -131,6 +131,7 @@
<ItemGroup> <ItemGroup>
<Content Include="assets\css\main.css" /> <Content Include="assets\css\main.css" />
<Content Include="assets\js\prototype.js" /> <Content Include="assets\js\prototype.js" />
<Content Include="assets\js\site.js" />
<Content Include="Default.aspx" /> <Content Include="Default.aspx" />
<Content Include="Global.asax" /> <Content Include="Global.asax" />
<Content Include="Web.config" /> <Content Include="Web.config" />
Expand Down
59 changes: 9 additions & 50 deletions Source/MarkupPreview/MarkupPreview/Views/home/index.brail
@@ -1,54 +1,13 @@
<div id="left" class="container"> <div id="left" class="container">
<h2>Markup ${FormHelper.Select("type", markupTypes, {@onchange: 'site.process();'})}</h2> <h2>Markup ${FormHelper.Select("type", markupTypes)}</h2>
<div class="inner"> <div class="inner">${FormHelper.TextArea("source", {@rows:20, @cols: 60})}</div>
${FormHelper.TextArea("source", {@rows:20, @cols: 80, @onchange: 'site.process();', @onkeypress: 'site.resizeTextarea(this);', @onkeyup: 'site.resizeTextarea(this);'})}
</div> </div>
</div>

<div id="right" class="container"> <div id="right" class="container">
<h2>HTML</h2> <h2>HTML</h2>
<div id="destination" class="inner"></div> <div id="destination" class="inner"></div>
</div> </div>

<script type="text/javascript">
<script type="text/javascript"> /*<![CDATA[*/ /*<![CDATA[*/
var Site = new Class.create({ var site = new Site("${UrlHelper.For({@controller:'home', @action:'process'})}");

/*]]>*/
previousValue: '', </script>
previousType: -1,

initialize: function() {
this.previousValue = '';
new PeriodicalExecuter(this.process, 2);
$('source').focus();
},

process: function() {
var newValue = $('source').value;
var newType =$("type").value;
if (newValue != this.previousValue || newType != this.previousType) {
new Ajax.Request("${UrlHelper.For({@controller:'home', @action:'process'})}", {
parameters: { source: newValue, type: newType }
});
this.previousValue = newValue;
this.previousType = newType;
}
},

// based on http://blog.bigsmoke.us/2007/02/18/automatic-html-textarea-resizing
resizeTextarea: function(textArea) {
if (!textArea.originalRowCount) {
textArea.originalRowCount = textArea.rows;
}
var rows = textArea.value.split('\n');
var neededRows = 1;
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
neededRows += (row.length > textArea.cols) ? Math.floor(row.length / textArea.cols) : 1;
}
textArea.rows = Math.max(neededRows, textArea.originalRowCount);
}
});

var site = new Site();

/*]]>*/</script>
Expand Up @@ -3,8 +3,12 @@
<html xmlns="http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <head>
<title>Markup Preview</title> <title>Markup Preview</title>
<meta name="author" content="Andre Loker" />
<meta name="description" content="Transforms markup of different kind to HTML" />
<meta name="keywords" content="markup, markdown, html, transform, git, github, andre, loker" />
<link rel="Stylesheet" type="text/css" href="${siteRoot}/assets/css/main.css" /> <link rel="Stylesheet" type="text/css" href="${siteRoot}/assets/css/main.css" />
<script type="text/javascript" src="${siteRoot}/assets/js/prototype.js"></script> <script type="text/javascript" src="${siteRoot}/assets/js/prototype.js"></script>
<script type="text/javascript" src="${siteRoot}/assets/js/site.js?v1"></script>
</head> </head>
<body> <body>
<div id="head"> <div id="head">
Expand Down
51 changes: 51 additions & 0 deletions Source/MarkupPreview/MarkupPreview/assets/js/site.js
@@ -0,0 +1,51 @@
var Site = new Class.create({

previousValue: '',
previousType: -1,
updateUrl: '',

initialize: function(updateUrl) {
new PeriodicalExecuter(this.process.bind(this), 2);
this.updateUrl = updateUrl;
this.previousValue = '';
this.previousType = -1;
$('type')
.observe("change", this.process.bind(this))
$('source')
.observe("change", this.process.bind(this))
.observe("keyup", this.resizeTextarea)
.observe("keypress", this.resizeTextarea)
.focus();
},

process: function() {
var newValue = $("source").value;
var newType = $("type").value;
if (newValue != this.previousValue || newType != this.previousType) {
if (!newValue) {
Element.update("destination", "");
} else {
new Ajax.Request(this.updateUrl, {
parameters: { source: newValue, type: newType }
});
}
this.previousValue = newValue;
this.previousType = newType;
}
},

// based on http://blog.bigsmoke.us/2007/02/18/automatic-html-textarea-resizing
resizeTextarea: function() {
var textArea = $('source');
if (!textArea.originalRowCount) {
textArea.originalRowCount = textArea.rows;
}
var rows = textArea.value.split('\n');
var neededRows = 1;
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
neededRows += (row.length > textArea.cols) ? Math.floor(row.length / textArea.cols) : 1;
}
textArea.rows = Math.max(neededRows, textArea.originalRowCount);
}
});

0 comments on commit 8e5c22f

Please sign in to comment.