forked from garakh/kladrapi-jsclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
215 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var gulp = require('gulp'), | ||
csso = require('gulp-csso'), | ||
imagemin = require('gulp-imagemin'), | ||
uglify = require('gulp-uglify'), | ||
concat = require('gulp-concat'), | ||
rename = require('gulp-rename'); | ||
|
||
gulp.task('default', function() { | ||
gulp.src('./kladr/css/style.css') | ||
.pipe(csso()) | ||
.pipe(rename('jquery.kladr.min.css')) | ||
.pipe(gulp.dest('./')); | ||
|
||
gulp.src(['./kladr/js/core.js', './kladr/js/plugin.js']) | ||
.pipe(concat('jquery.kladr.js')) | ||
.pipe(uglify()) | ||
.pipe(rename({suffix: '.min'})) | ||
.pipe(gulp.dest('./')); | ||
}); |
File renamed without changes
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
jQuery Kladr | ||
================================================================================ | ||
|
||
Исходный код плагина | ||
|
||
* **core.js** - Реализует $.kladr. Позволяет обращаться к сервису [kladr-api.ru] [1]. | ||
* **plugin.js** - Реализует плагин $( '' ).kladr. | ||
|
||
[1]: http://kladr-api.ru/ "КЛАДР API" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
(function ($) { | ||
$.kladr = {}; | ||
|
||
// Service URL | ||
$.kladr.url = 'http://kladr-api.ru/api.php'; | ||
|
||
// Enum KLADR object types | ||
$.kladr.type = { | ||
region: 'region', | ||
district: 'district', | ||
city: 'city', | ||
street: 'street', | ||
building: 'building' | ||
}; | ||
|
||
// Validate query | ||
$.kladr.validate = function (query) { | ||
switch (query.type) { | ||
case $.kladr.type.region: | ||
case $.kladr.type.district: | ||
case $.kladr.type.city: | ||
if (query.parentType && !query.parentId) { | ||
error('parentId undefined'); | ||
return false; | ||
} | ||
break; | ||
case $.kladr.type.street: | ||
if (query.parentType != $.kladr.type.city) { | ||
error('parentType must equal "city"'); | ||
return false; | ||
} | ||
if (!query.parentId) { | ||
error('parentId undefined'); | ||
return false; | ||
} | ||
break; | ||
case $.kladr.type.building: | ||
if (query.parentType != $.kladr.type.street) { | ||
error('parentType must equal "street"'); | ||
return false; | ||
} | ||
if (!query.parentId) { | ||
error('parentId undefined'); | ||
return false; | ||
} | ||
break; | ||
default: | ||
if (!query.oneString) { | ||
error('type incorrect'); | ||
return false; | ||
} | ||
break; | ||
} | ||
|
||
if (query.oneString && query.parentType && !query.parentId) { | ||
error('parentId undefined'); | ||
return false; | ||
} | ||
|
||
if (query.limit < 1) { | ||
error('limit must greater than 0'); | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
// Send query to service | ||
$.kladr.api = function (query, callback) { | ||
if (!callback) { | ||
error('Callback undefined'); | ||
return; | ||
} | ||
|
||
if (!$.kladr.validate(query)) { | ||
callback([]); | ||
return; | ||
} | ||
|
||
var def = $.Deferred(); | ||
|
||
def.done(callback); | ||
def.fail(function (er) { | ||
error(er); | ||
callback([]); | ||
}); | ||
|
||
$.getJSON($.kladr.url + "?callback=?", | ||
toApiFormat(query), | ||
function (data) { | ||
def.resolve(data.result || []); | ||
} | ||
); | ||
|
||
setTimeout(function () { | ||
def.reject('Request error'); | ||
}, 3000); | ||
}; | ||
|
||
// Check exist object | ||
$.kladr.check = function (query, callback) { | ||
if (!callback) { | ||
error('Callback undefined'); | ||
return; | ||
} | ||
|
||
query.withParents = false; | ||
query.limit = 1; | ||
|
||
$.kladr.api(query, function (objs) { | ||
if (objs && objs.length) { | ||
callback(objs[0]); | ||
} else { | ||
callback(false); | ||
} | ||
}); | ||
}; | ||
|
||
function toApiFormat (query) { | ||
var params = {}, | ||
fields = { | ||
token: 'token', | ||
key: 'key', | ||
type: 'contentType', | ||
name: 'query', | ||
withParents: 'withParent', | ||
oneString: 'oneString', | ||
limit: 'limit' | ||
}; | ||
|
||
if (query.parentType && query.parentId) { | ||
params[query.parentType + 'Id'] = query.parentId; | ||
} | ||
|
||
for (var i in query) { | ||
if (query.hasOwnProperty(i) && fields.hasOwnProperty(i) && query[i]) { | ||
params[fields[i]] = query[i]; | ||
} | ||
} | ||
|
||
return params; | ||
} | ||
|
||
function error (error) { | ||
window.console && window.console.error && window.console.error(error); | ||
} | ||
})(jQuery); |
Oops, something went wrong.