CmdUtils.CreateCommand({
names: [ "mapia" , "mpi" ],
icon: "http://mapia.ua/favicon.ico" ,
description: _( "Kiev map powered by mapia.ua" ),
author: {name: "Kottenator" , email: "kottenator@gmail.com" },
license: "MIT"
});
* This source code was highlighted with Source Code Highlighter .
_(...)
- method for i18nCmdUtils.CreateCommand({
names: [ "mapia" , "mpi" ],
icon: "http://mapia.ua/favicon.ico" ,
description: _( "Kiev map powered by mapia.ua" ),
author: {name: "Kottenator" , email: "kottenator@gmail.com" },
license: "MIT" ,
arguments: [
{role: 'object' , nountype: noun_arb_text, label: _( 'address' )}
],
preview: function preview(pblock, args) {
var msg = "You'd just typed \"${text}\"" ;
pblock.innerHTML = _(msg, { text: args. object .text });
},
execute: function execute(args) {
var msg = "You choose address: \"${text}\"" ;
displayMessage(_(msg, { text: args. object .text }));
}
});
* This source code was highlighted with Source Code Highlighter .
arguments: [arg1, arg2, ...]
- description of the arguments of our team (thanks, Cap!).{role: 'object' , nountype: noun_arb_text, label: _( 'address' )}
:role: 'object'
means that this is the first argument expected immediately after the command name; nountype
means something like the type of this variable; label
is what we will see in the command description (for example, “mpi (address) ” ). More information about the arguments of the commands can be read here.preview: function preview(pblock, args) {
var msg = "You'd just typed \"${text}\"" ;
pblock.innerHTML = _(msg, { text: args. object .text });
}
This is a method for displaying a preview in the Ubiquity window. pblock
- container preview, args
- current arguments of the command; args. object .text
args. object .text
is the same argument with role: 'object'
._(...)
method can do not only i18n, but also substitutes parameters in the template.execute: function execute(args) {
var msg = "You choose address: \"${text}\"" ;
displayMessage(_(msg, { text: args. object .text }));
}
And this method is called when we press Enter.displayMessage(...)
displays a small slide-up notification.arguments: [
{role: 'object' , nountype: noun_arb_text, label: _( 'address' )},
{role: 'goal' , nountype: noun_arb_text, label: _( 'zoom' )},
{role: 'source' , nountype: noun_arb_text, label: _( 'map size' )}
],
previewDelay: 800,
getStaticURL: function (address, zoom, size) {
var mapUrl = "http://mapia.ua/static?" ;
address = address || '' ;
zoom = parseInt(zoom, 10) || 15;
zoom = Math.min(18, Math.max(zoom, 6));
size = size || '400x300' ;
var params = {
address: address,
zoom: zoom,
size: size
};
return mapUrl + jQuery.param( params );
},
preview: function preview(pblock, args) {
var img = '<img src="${url}" />' ;
var url = this .getStaticURL(args. object .text, args.goal.text, args.source.text);
pblock.innerHTML = _(img, { url: url });
},
* This source code was highlighted with Source Code Highlighter .
{role: 'goal' , nountype: noun_arb_text, label: _( 'zoom' )},
{role: 'source' , nountype: noun_arb_text, label: _( 'map size' )}
* This source code was highlighted with Source Code Highlighter .
role: 'goal'
will expect us to write something like "mapia address to 16" . This “to 16” will mean that args.goal.text == '16'
and this will be our zoom. Possible values are from 6 to 18.args.source.text == '300x200'
and this will be the size of the picture.getStaticURL(address, zoom, size)
method returns the address string of our map.preview(...)
method is now doing something sensible :) - shows the mappreviewDelay: 800
- in order not to kill the mapia.ua server with a crazy load (on request for each keystroke), we will introduce a delay before a request of 800 millisecondsloadDetails: function (pblock, args, page_num) {
var baseUrl = "http://mapia.ua/search" ;
var params = {city: "%u041A%u0438%u0435%u0432" , "search[query]" : args. object .text};
var self = this ;
jQuery.ajax({
type: "GET" ,
url: baseUrl,
data: params ,
dataType: 'html' ,
error: function () { },
success: function (html) {
$( '#content' , pblock).html(html);
}
});
},
preview: function preview(pblock, args) {
var img = '<img src="${url}" /><div id="content"></div>' ;
var url = this .getStaticURL(args. object .text, args.goal.text, args.source.text);
pblock.innerHTML = _(img, { url: url });
this .loadDetails(pblock, args);
}
* This source code was highlighted with Source Code Highlighter .
loadDetails(...)
method loadDetails(...)
? It simply loads search results into our preview. I think that the code is clear and can not be explained.mapiaURL: "http://mapia.ua/#popup_class=${cat}&popup_id=${sid}" ,
clearSearchResultsCode: function (html) {
html = html.replace(/<div id="search_bar_title">((?:.|\s)*?)<\/div>/i, '<h3 class="search_bar_title">$1</h3>')
.replace(/<p>.*?<\/p>/i, '<p><small>( mapia.ua)</small></p>')
.replace(/(<div class="buildings_info")[^>]*>/gi, '$1>')
.replace(/<a href="\/center\/street\/(\d+)[^>]*>/i, '<a href="' + _(this.mapiaURL, {cat: 'Street', sid: "$1"}) + '">')
.replace(/<a[^<>]+onclick="\$\('street_\d+_buildings'\).*?<\/a>/i, '')
.replace(/<a href="\/center\/address\/(\d+)[^>]*>/gi, '<a href="' + _(this.mapiaURL, {cat: 'Address', sid: "$1"}) + '">')
.replace(/<a href="\/center\/feature\/(\d+)[^>]*>/gi, '<a href="' + _(this.mapiaURL, {cat: 'Feature', sid: "$1"}) + '">')
.replace(/<a href="\/center\/station\/(\d+)[^>]*>/gi, '<a href="' + _(this.mapiaURL, {cat: 'Station', sid: "$1"}) + '">')
.replace(/<a href="\/center\/city\/(\d+)[^>]*>/gi, '<a href="' + _(this.mapiaURL, {cat: 'City', sid: "$1"}) + '">')
.replace(/(<div id="search_bar_title"(?:.|\s)*?)<span(?:.|\s)*?<\/span>/i, '$1')
.replace(/(<div id="search_bar_title"(?:.|\s)*?)<span(?:.|\s)*?<\/span>/i, '$1') /* not a duplicate */
.replace(/href="\/search?[^"]*pages_feature=(\d+)[^"]*"/gi, 'page_num="$1"')
.replace(/<p><a[^<>]*onclick="load_content(?:.|\s)*?<\/p>/i, '')
.replace(/<script>(?:.|\s)*?<\/script>/i, '');
return html;
},
loadDetails: function (pblock, args, page_num) {
var baseUrl = "http://mapia.ua/search" ;
var params = {city: "%u041A%u0438%u0435%u0432" , "search[query]" : args. object .text};
if (page_num)
params.pages_feature = page_num;
var self = this ;
jQuery.ajax({
type: "GET" ,
url: baseUrl,
data: params,
dataType: 'html' ,
error: function () {},
success: function (html) {
// Pre-fixes via RegExp
html = self.clearSearchResultsCode(html);
$( '#content' , pblock).html(html);
// After-fixes via jQuery
$( 'div.pagination a' , pblock).click( function (e){
self.loadDetails(pblock, args, $( this ).attr( 'page_num' ));
e.preventDefault();
return false ;
})
}
});
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/74514/
All Articles