Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

search as you type of cities implementation

almost done, by the way.
  • Loading branch information...
commit bf9509bf45cff2ee91bedff0219cc3b5f8153dbd 1 parent e8bd03d
@gibatronic gibatronic authored
View
36 hypertext-preprocessor/library/ajax.php
@@ -9,6 +9,7 @@
define("CHECK_USER", "8b5b422abef67a034aac2d83f07afbcd");
define("SUPPORT", "256fc6e4dbf98308ceca2b9b924b25af");
define("UNSUPPORT", "89e3d438a10459f93076b8750c1a664f");
+define("SAYT_CITY", "9d86a3d362bbb5c7b2b2b54b90940904");
$system = new stdClass();
@@ -38,6 +39,10 @@
$system->post_id = (integer) $_REQUEST["post_id"];
}
+if ($_REQUEST["query"]) {
+ $system->query = $_REQUEST["query"];
+}
+
if (preg_match("/^[0-9]+\$/", $_REQUEST["start"])) {
$system->start = (integer) $_REQUEST["start"];
} else {
@@ -126,6 +131,37 @@
break;
+ case SAYT_CITY:
+ // Remove the junk and leave only words that metters
+ $system->query = mb_ereg_replace("\W+", " ", $system->query, "i");
+ $system->query = mb_ereg_replace("[^ A-Z]", "_", $system->query, "i");
+ $system->query = mb_ereg_replace(" +", " ", $system->query, "i");
+ $system->query = trim($system->query);
+
+ if ($system->query) {
+ // Guess what is city name and State acronym
+ $city = trim(preg_replace("/\s*\b([A-Z]{2})\b\s*/i", " ", $system->query));
+ $state = preg_replace("/.*\b([A-Z]{2})\b.*/i", "\$1", $system->query);
+
+ $query = "SELECT id, name, url, state_id, state_name, state_sa FROM cities_data WHERE name LIKE '" . $city . "%'";
+
+ if (preg_match("/\b([A-Z]{2})\b/i", $system->query)) {
+ $query .= " AND state_sa LIKE '" . $state . "'";
+ }
+
+ $query .= " ORDER BY name ASC LIMIT 5";
+
+ $system->return->sucess = true;
+ $system->return->query = str_replace("_", ".", $city);
+ $system->return->cities = db($query);
+ } else {
+ $system->return->sucess = false;
+ $system->return->mensage = "Missing query to search for";
+ $system->return->cities = array();
+ }
+
+ break;
+
case UNSUPPORT:
$system->return->action = "Unsupport";
View
2  javascripts/config.js
@@ -25,6 +25,7 @@ var CONFIG = (function() {
'UNSUPPORT' : '89e3d438a10459f93076b8750c1a664f',
'CANDIDATES_LIST' : "75720f54472ffabfca3fcb0a08e19bd9",
'SET_HOMETOWN' : 'dc82dd58115798afdb98d8ea2efefbc3',
+ 'SAYT_CITY' : '9d86a3d362bbb5c7b2b2b54b90940904',
'PERMISSIONS' : ['email,user_about_me,user_birthday,user_education_history,user_hometown,user_interests,user_location,user_relationships,user_religion_politics,user_work_history,publish_actions']
};
@@ -47,6 +48,7 @@ var CONFIG = (function() {
'UNSUPPORT' : '89e3d438a10459f93076b8750c1a664f',
'CANDIDATES_LIST' : "75720f54472ffabfca3fcb0a08e19bd9",
'SET_HOMETOWN' : 'dc82dd58115798afdb98d8ea2efefbc3',
+ 'SAYT_CITY' : '9d86a3d362bbb5c7b2b2b54b90940904',
'PERMISSIONS' : ['email,user_about_me,user_birthday,user_education_history,user_hometown,user_interests,user_location,user_relationships,user_religion_politics,user_work_history,publish_actions']
}
View
125 javascripts/main.js
@@ -1,7 +1,7 @@
/**
-* Developed by Evandro Eisinger & Gibran Malheiros
-* http://agora.vc
-*/
+ * Developed by Evandro Eisinger & Gibran Malheiros
+ * http://agora.vc
+ */
(function($) {
@@ -10,7 +10,7 @@
/**
* page
*/
-
+
var page = function() {
// facebook initialize
@@ -29,7 +29,9 @@
// url initialize
page.url();
-
+
+ // Location city nav inicializa
+ page.location();
}
/**
@@ -421,6 +423,119 @@ console.log(page.auth.token);
window.location.assign("http://" + window.location.hostname + "/oooops/")
}
+ /**
+ * System for the location header field, responsible for the field's behavior and for suggesting cities
+ */
+
+ page.location = function() {
+ page.location.original = page.location.elements.$cityname.val();
+
+ page.location.elements.$cityname.bind("focus", page.location.focus).bind("blur", page.location.blur);
+
+ // Listen to the key up event but don't flood the hell out of it,
+ // wait at least 1 / 8 of a second before triggering the callback
+ page.location.elements.$cityname.bind("keydown", page.location.keyOverload).bind("keyup", page.location.keyOverload);
+ }
+
+ page.location.elements = { }
+
+ page.location.elements.container = ".header__nav";
+ page.location.elements.$citynav = $(".citynav", page.location.container);
+ page.location.elements.$cityname = $(".citynav__cityname", page.location.container);
+ page.location.elements.$dropdown = $(".citynav__searchdropdown", page.location.container);
+
+ page.location.interval = null;
+ page.location.memory = null;
+ page.location.original = null;
+
+ page.location.blur = function(event) {
+ page.location.elements.$cityname.val(page.location.original);
+ page.location.elements.$dropdown.addClass("is-hidden");
+ }
+
+ page.location.error = function(xhr, status, error) {
+
+ }
+
+ page.location.focus = function(event) {
+ $(this).select();
+ }
+
+ page.location.process = function(data, status, xhr) {
+ var html;
+
+ if (data.sucess && data.cities.length) {
+ html = "";
+
+ $(data.cities).each(function(index) {
+ // Highlight the difference
+ this.name = this.name.replace(new RegExp("^(" + data.query + ")(.*)", "i"), "$1<strong>$2</strong>");
+
+ html += "<li data-city-id=\"" + this.id + "\" data-city-url=\"" + this.url + "\" class=\"citynav__searchdropdown__item\">";
+ html += this.name + ", " + this.state_sa;
+ html += "</li>";
+ });
+
+ page.location.elements.$dropdown.html(html).removeClass("is-hidden");
+ }
+ }
+
+ page.location.key = function(event) {
+ var query = $.trim(page.location.elements.$cityname.val());
+
+ if (page.location.memory != query && query.length > 2) {
+ page.location.memory = query;
+
+ $.ajax({
+ data: {
+ action: CONFIG.get("SAYT_CITY"),
+ query: query
+ },
+ dataType: "json",
+ error: page.location.error,
+ success: page.location.process,
+ type: "post",
+ url: CONFIG.get("AJAX_URL")
+ });
+ }
+ }
+
+ page.location.keyOverload = function(event) {
+ // Enter, up and down keys have urgent treatment, the other ones can wait
+
+ window.clearTimeout(page.location.interval);
+
+ if (event.type = "keydown" && $.inArray(event.which, [13, 27, 38, 40]) != -1) {
+ event.preventDefault();
+
+ switch (event.which) {
+ case 13:
+ // Enter
+
+
+ break;
+ case 27:
+ page.location.elements.$cityname.val("");
+ page.location.elements.$dropdown.addClass("is-hidden");
+
+ break;
+ case 38:
+ // Up
+
+
+ break;
+ case 40:
+ // Down
+
+
+ break;
+ }
+ } else {
+ page.location.interval = window.setTimeout(page.location.key, 125, event);
+ }
+ }
+
+ // Takeoff when DOM is ready to bounce
$(page);
}
View
7 stylesheets/modules.css
@@ -768,7 +768,6 @@
border-width: 1px;
box-shadow: 0 3px 4px #66828E inset, 0 1px 2px #3A6679;
width: 410px;
- height: 130px;
z-index: 987654321;
padding:0;
margin:0;
@@ -776,4 +775,10 @@
}
.citynav__searchdropdown__item {
padding: 8px;
+ }
+ .citynav__searchdropdown__item__highlighted {
+ background: 0 0 #345F72;
+ color: #FFFFFF;
+ margin: 4px;
+ padding: 4px;
}
Please sign in to comment.
Something went wrong with that request. Please try again.