Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add sharing ability for the current state of App as link in output se…

…ction
  • Loading branch information...
commit be2dff9a3ff6425950613eecb808b03abb45abcc 1 parent 5c7c752
@johnkpaul authored
View
2  application.css
@@ -244,7 +244,7 @@ input, textarea, #results, #groups {
}
#groups {
- margin-bottom: 0;
+ margin-bottom: 20px;
}
#groups h3 {
View
37 application.js
@@ -36,6 +36,10 @@
return new RegExp(value, option);
};
+ Expression.prototype.asUrlPart = function() {
+ return encodeURIComponent(this.regexp.val() + "||||" + this.option.val());
+ };
+
return Expression;
})(Spine.Controller);
@@ -65,6 +69,10 @@
return this.values = val.split('\n');
};
+ TestStrings.prototype.asUrlPart = function() {
+ return encodeURIComponent(JSON.stringify(this.values));
+ };
+
return TestStrings;
})(Spine.Controller);
@@ -102,12 +110,20 @@
this.matchGroups(value, matches, count);
count += 1;
}
+ this.addShareLink(this.expression.asUrlPart(), this.test_strings.asUrlPart());
return this.showOutput();
} catch (error) {
return this.showError();
}
};
+ Results.prototype.addShareLink = function(expression_url, test_strings_url) {
+ var url;
+ url = window.location.protocol + window.location.pathname + "#";
+ url += expression_url + "||||" + test_strings_url;
+ return $("#share_link").attr("href", url);
+ };
+
Results.prototype.matchResults = function(value, matches) {
var index, length, match, string, _i, _len;
if (!matches) return;
@@ -115,7 +131,6 @@
for (_i = 0, _len = matches.length; _i < _len; _i++) {
match = matches[_i];
if (value === '') break;
- console.log("This is the match: " + match);
index = value.indexOf(match);
length = match.length;
if (index > -1) {
@@ -123,11 +138,8 @@
if (index > -1) {
string += "<span>" + (value.slice(index, index + length)) + "</span>";
}
- console.log("value before: " + value + " length: " + length + " index: " + index + " string: " + string);
value = value.slice(length + index);
}
- console.log("value after: " + value + " string: " + string);
- console.log('');
}
string += value;
return this.drawResult(string);
@@ -186,7 +198,9 @@
App = (function() {
function App() {
- this.loadExample = __bind(this.loadExample, this); this.expression = new Expression({
+ this.load = __bind(this.load, this);
+ this.loadExample = __bind(this.loadExample, this);
+ this.loadFromHash = __bind(this.loadFromHash, this); this.expression = new Expression({
el: '#expression'
});
this.test_strings = new TestStrings({
@@ -194,14 +208,27 @@
});
this.results = new Results(this.expression, this.test_strings);
$('#example').bind('click', this.loadExample);
+ if (window.location.hash !== '') this.loadFromHash();
}
+ App.prototype.loadFromHash = function() {
+ var expression_encoded, option, regex, test_strings_from_url, _ref, _ref2;
+ _ref = window.location.hash.substr(1).split("||||").map(decodeURIComponent), expression_encoded = _ref[0], test_strings_from_url = _ref[1];
+ _ref2 = expression_encoded.split("||||"), regex = _ref2[0], option = _ref2[1];
+ test_strings_from_url = JSON.parse(test_strings_from_url);
+ return this.load(regex, option, test_strings_from_url);
+ };
+
App.prototype.loadExample = function(event) {
var option, regex, test_strings;
event.preventDefault();
regex = "^(https?)://((?:[A-Z0-9]*\\\.?)*)((?:\\\/?[A-Z0-9])*)";
option = 'i';
test_strings = ['https://github.com/jonmagic/scriptular', 'http://scriptular.com', 'http://www.google.com', 'http://www.guardian.co.uk'];
+ return this.load(regex, option, test_strings);
+ };
+
+ App.prototype.load = function(regex, option, test_strings) {
$('input[name=expression]').val(regex);
$('input[name=option]').val(option);
$('textarea').val(test_strings.join('\n'));
View
4 index.html
@@ -46,6 +46,8 @@
<ul id="results"></ul>
<h2>Match Groups:</h2>
<ul id="groups"></ul>
+ <h2>Share Link:</h2>
+ <a id="share_link" href="">Share this regular expression</a>
</div>
</div>
@@ -201,4 +203,4 @@
})();
</script>
</body>
-</html>
+</html>
View
27 scripts/application.coffee
@@ -17,6 +17,9 @@ class Expression extends Spine.Controller
buildRegex: (value, option) ->
new RegExp(value, option)
+ asUrlPart:() ->
+ encodeURIComponent(@regexp.val() + "||||" + @option.val())
+
class TestStrings extends Spine.Controller
elements:
'textarea': 'input'
@@ -31,6 +34,9 @@ class TestStrings extends Spine.Controller
getValues: (val) ->
@values = val.split('\n')
+ asUrlPart:() ->
+ encodeURIComponent(JSON.stringify(@values))
+
class Results
constructor: (@expression, @test_strings) ->
@expression.bind 'update', @compile
@@ -56,10 +62,16 @@ class Results
@matchResults(value, matches)
@matchGroups(value, matches, count)
count += 1
+ @addShareLink(@expression.asUrlPart(), @test_strings.asUrlPart())
@showOutput()
catch error
@showError()
+ addShareLink: (expression_url, test_strings_url) ->
+ url = window.location.protocol+window.location.pathname+"#"
+ url += expression_url + "||||" + test_strings_url
+ $("#share_link").attr("href",url)
+
matchResults: (value, matches) ->
return unless matches
string = ''
@@ -126,6 +138,15 @@ class App
@test_strings = new TestStrings(el: '#test_strings')
@results = new Results(@expression, @test_strings)
$('#example').bind 'click', @loadExample
+ if window.location.hash != ''
+ @loadFromHash()
+
+ loadFromHash: () =>
+ [expression_encoded, test_strings_from_url] = window.location.hash.substr(1).split("||||").map(decodeURIComponent)
+ [regex, option] = expression_encoded.split("||||")
+ test_strings_from_url = JSON.parse(test_strings_from_url)
+
+ @load(regex,option,test_strings_from_url)
loadExample: (event) =>
event.preventDefault()
@@ -139,6 +160,9 @@ class App
'http://www.guardian.co.uk'
]
+ @load(regex,option,test_strings)
+
+ load: (regex,option,test_strings) =>
$('input[name=expression]').val(regex)
$('input[name=option]').val(option)
$('textarea').val(test_strings.join('\n'))
@@ -146,5 +170,6 @@ class App
@test_strings.onKeyPress()
@results.compile
+
window.App = App
-window.$ = $
+window.$ = $
Please sign in to comment.
Something went wrong with that request. Please try again.