Permalink
Browse files

Create HTML/JS framework; tie it into database storage

  • Loading branch information...
1 parent ac56e61 commit 4d3e2ae6e7d272aaa23bc9ed988f28e0d0b831d9 @ctfouts ctfouts committed Mar 23, 2012
Showing with 136 additions and 20 deletions.
  1. +21 −5 app.psgi
  2. +11 −0 config.yml
  3. +12 −0 config.yml.default
  4. +20 −0 public/css/xape.css
  5. +29 −15 public/index.html
  6. +13 −0 public/js/xape.js
  7. +30 −0 sql/xape-0.000-mysql.sql
View
@@ -6,29 +6,45 @@ use Xape::Enumerator;
use Method::Signatures;
use Dancer;
use Dancer::Plugin::Ajax;
+use Dancer::Plugin::Database;
get '/' => sub {
send_file 'index.html';
};
-func get_enum_from_text(Str $phrase, Str $language) {
+func get_enum_from_phrase(Str $phrase, Str $language) {
my $e = Xape::Enumerator->new($language);
my $enum = $e->sum($phrase);
my $text = $e->xlit($phrase);
$text ||= $phrase;
return ($enum, $text);
}
+func update_database(Str $phrase, Str $language, Int $enum) {
+ return if database->quick_select(
+ 'phrases', {phrase => $phrase, language => $language});
+ database->quick_insert(
+ 'phrases', {phrase => $phrase, language => $language, enum => $enum});
+}
+
+func get_matches(Int $enum) {
+ my @matches = database->quick_select('phrases', {enum => $enum});
+ return map { {text => $_->{phrase}, language => $_->{language}}} @matches;
+}
+
ajax '/777/:language/:phrase' => sub {
- my ($enum, $text) = get_enum_from_text(params->{phrase}, params->{language});
+ my ($enum, $text)
+ = get_enum_from_phrase(params->{phrase}, params->{language});
+ update_database($text, params->{language}, $enum);
+ my @matches = grep {$_->{text} ne $text} get_matches($enum);
header('Content-Type' => 'application/json; charset=utf-8');
- return to_json({enum => $enum, text => $text});
+ return to_json({enum => $enum, text => $text, matches => \@matches});
};
get '/777/:language/:phrase' => sub {
- my ($enum, $text) = get_enum_from_text(params->{phrase}, params->{language});
+ my ($enum, $phrase) = get_enum_from_phrase(params->{phrase}, params->{language});
header('Content-Type' => 'text/html; charset=utf-8');
- return qq(<dl><dt>$text</dt><dd>$enum</dd></dl>);
+ return qq(<dl><dt>$phrase</dt><dd>$enum</dd></dl>);
};
start;
View
@@ -1 +1,12 @@
charset: UTF-8
+logger: 'log_handler'
+
+plugins:
+ Database:
+ driver: 'SQLite'
+ database: 'xape.sqlite3'
+ connection_check_threshold: 10
+ dbi_params:
+ RaiseError: 1
+ AutoCommit: 1
+ log_queries: 1
View
@@ -0,0 +1,12 @@
+charset: UTF-8
+logger: 'log_handler'
+
+plugins:
+ Database:
+ driver: 'SQLite'
+ database: 'xape.sqlite3'
+ connection_check_threshold: 10
+ dbi_params:
+ RaiseError: 1
+ AutoCommit: 1
+ log_queries: 0
View
@@ -1,3 +1,23 @@
+.toplevel_table {
+ margin:auto;
+ width:40%;
+ background-color:#b0e0b0;
+}
+
+.inputs {
+ margin:left;
+ width:20em;
+}
+
+.outputs {
+ margin:auto;
+ width:10em;
+}
+
+.matches {
+ margin:auto;
+ width:10em;
+}
dl {
float: left;
}
View
@@ -5,21 +5,35 @@
</head>
<body>
- <h1 class="heading"><span id="main">Xape</span></h1>
- <div class="inputs">
- <form id="f">
- <select id="language">
- <option value="he">Hebrew</option>
- </select>
- <input type="text" id="phrase" />
- </form>
- </div>
- <div class="outputs">
- <dl>
- <dt id="phrase_text"></dt>
- <dd id="phrase_enum"></dd>
- </dl>
- </div>
+ <h1 class="heading"><span id="main_heading">Xape</span></h1>
+ <table class="toplevel_table">
+ <tr>
+ <td colspan="2">
+ <div class="inputs">
+ <form id="f">
+ <select id="language">
+ <option value="he">Hebrew</option>
+ </select>
+ <input type="text" id="phrase" />
+ </form>
+ </div>
+ </tr>
+ <tr>
+ <td>
+ <div class="outputs">
+ <dl>
+ <dt id="phrase_text"></dt>
+ <dd id="phrase_enum"></dd>
+ </dl>
+ </div>
+ </td>
+ <td>
+ <div class="matches">
+ <ul id="match_list"></ul>
+ </div>
+ </td>
+ </tr>
+ </table>
</body>
<script type="text/javascript"
View
@@ -18,4 +18,17 @@ function get_enum(phrase, language) {
function update_results(data, textStatus, jqXHR) {
$("#phrase_text").html(data.text);
$("#phrase_enum").html(data.enum);
+ $("#match_list").empty();
+ data.matches.forEach(
+ function(match) {
+ var oid = "link_" + match.text;
+ var link = '<a id="' + oid + '" href="#">' + match.text + '</a>';
+ $("#match_list").append("<li>" + link + "</li>");
+ $("#"+oid).click(
+ function() {
+ get_enum(oid.substr(5, oid.length), match.language);
+ }
+ );
+ }
+ );
}
View
@@ -0,0 +1,30 @@
+--
+-- Created by SQL::Translator::Producer::MySQL
+-- Created on Fri Mar 23 14:07:31 2012
+--
+SET foreign_key_checks=0;
+
+CREATE TABLE `languages` (
+ `id` char(2) NOT NULL,
+ `name` varchar(32) NOT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `name` (`name`)
+);
+
+--
+-- Table: `phrases`
+--
+CREATE TABLE `phrases` (
+ `id` integer(11) NOT NULL auto_increment,
+ `phrase` varchar(128) NOT NULL,
+ `language` char(2) NOT NULL,
+ `enum` integer(11) NOT NULL,
+ INDEX `phrases_k1` (`phrase`),
+ INDEX `phrases_k2` (`enum`),
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `phrase` (`phrase`),
+ CONSTRAINT `phrases_fk1` FOREIGN KEY (`language`) REFERENCES `languages` (`id`)
+);
+
+SET foreign_key_checks=1;
+

0 comments on commit 4d3e2ae

Please sign in to comment.