Created
May 16, 2017 07:28
-
-
Save kovchiy/98a1820d9ecc722a03caf2dd3068d51f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* @block Serp Поисковая выдача | |
* @dep grid head divider | |
* @tag serp | |
* @ext grid | |
*/ | |
Beast.decl({ | |
Serp: { | |
inherits:'Grid', | |
mod: { | |
Wrap:true, | |
Loading:false, | |
}, | |
onMod: { | |
width: { | |
edge: function () { | |
// var head = this.elem('head')[0] | |
// var navigation = head.get('Navigation')[0] | |
// head.mod('MarginX', true) | |
// navigation.mod('MarginX', false) | |
}, | |
margin: function () { | |
// var head = this.elem('head')[0] | |
// var navigation = head.get('Navigation')[0] | |
// head.mod('MarginX', false) | |
// navigation.mod('MarginX', true) | |
}, | |
} | |
}, | |
onWin: { | |
'Serp:WillUpdate': function () { | |
this.mod('Loading', true) | |
}, | |
'Serp:DidUpdate': function () { | |
this.mod('Loading', false) | |
}, | |
}, | |
expand: function () { | |
var text = MissEvent.qs('text') | |
if (text !== undefined) { | |
this.apiRequest(text) | |
} | |
}, | |
apiRequest: function (text) { | |
document.title = '«' + text + '» — Яндекс' | |
Ajax({ | |
url: '/api/search', | |
data: { | |
text: text, | |
json_dump: 'searchdata.docs', | |
lr: 213, | |
}, | |
success: function (data) { | |
this.apiProcess(JSON.parse(data)['searchdata.docs']) | |
}.bind(this) | |
}) | |
}, | |
apiProcess: function (docs) { | |
var nodes = [] | |
for (var i = 0, ii = docs.length; i < ii; i++) { | |
var doc = docs[i] | |
var docType = '' | |
var node = undefined | |
if (doc.snippets !== undefined && doc.snippets.main !== undefined && doc.snippets.main.template !== undefined) { | |
docType = doc.snippets.main.template | |
} else if (doc.snippets !== undefined && doc.snippets.full !== undefined && doc.snippets.full.template !== undefined) { | |
docType = doc.snippets.full.template | |
} else { | |
docType = 'generic' | |
} | |
docType = docType.replace(/_-/g, '').toLowerCase() | |
for (var selector in Beast.declaration) { | |
var decl = Beast.declaration[selector] | |
if (decl.adapter !== undefined) { | |
for (var adapterType in decl.adapter) { | |
if (adapterType.toLowerCase() === docType) { | |
node = Beast.node( | |
selector.substr(0, 1).toUpperCase() + selector.substr(1) | |
) | |
decl.adapter[adapterType].call(node, doc) | |
nodes.push(node) | |
break | |
} | |
} | |
} | |
if (node !== undefined) { | |
break | |
} | |
} | |
if (node === undefined) { | |
console.log('[Serp]', 'unknown doc type:', docType, '\n', doc) | |
} | |
} | |
this.elem('list')[0].expand(nodes) | |
} | |
}, | |
Serp__head: { | |
expand: function () { | |
this.implementWith(<Head>{this.get()}</Head>) | |
} | |
}, | |
Serp__list: { | |
inherits: 'Grid', | |
mod: { | |
Margin: true, | |
Rows: true, | |
Left: false, | |
Right: false, | |
}, | |
onCol: function (num, edge) { | |
this.mod('width', edge ? 'isfull' : 'hasmargin') | |
}, | |
expand: function () { | |
if (this.mod('Left')) { | |
this.mod('Col', 12) | |
this.parentBlock().css('width', 'auto') | |
} | |
if (this.mod('Right')) { | |
this.mod('Col', 9) | |
} | |
/* | |
* Расстановка разделителей | |
*/ | |
var children = this.get('/') | |
for (var i = 0, ii = children.length, prev, current; i < ii; i++) { | |
prev = children[i-1] | |
current = children[i] | |
if (current.isKindOf('Snippet')) { | |
if (prev === undefined) { | |
this.append(<Divider Size="M"/>) | |
} | |
else if (prev.isKindOf('Snippet')) { | |
// this.append(<Divider Size="M"/>) | |
} | |
else if (prev.isKindOf('Wiz')) { | |
this.append(<Divider Size="M"/>) | |
} | |
else if (prev.isKindOf('Card') && prev.mod('theme') === 'blank') { | |
this.append(<Divider Size="L"/>) | |
} | |
} | |
else if (current.isKindOf('Minisnippet')) { | |
if (prev === undefined) { | |
this.append(<Divider Size="M"/>) | |
} | |
else if (prev.isKindOf('Minisnippet')) { | |
this.append(<Divider Size="M"/>) | |
} | |
} | |
else if (current.isKindOf('Card') && current.mod('theme') === 'blank') { | |
if (prev === undefined) { | |
this.append(<Divider Size="L"/>) | |
} | |
else if (prev.isKindOf('Snippet')) { | |
this.append(<Divider Size="L"/>) | |
} | |
else if (prev.isKindOf('Card') && prev.mod('theme') === 'blank') { | |
this.append(<Divider Size="L"/>) | |
} | |
else if (prev.isKindOf('Promo')) { | |
this.append(<Divider Size="L"/>) | |
} | |
} | |
else if (current.isKindOf('Wiz')) { | |
if (prev === undefined) { | |
this.append(<Divider Size="M"/>) | |
} | |
else if (prev.isKindOf('Snippet') || prev.isKindOf('Wiz')) { | |
this.append(<Divider Size="M"/>) | |
} | |
else if (prev.isKindOf('Card') && prev.mod('theme') === 'blank') { | |
this.append(<Divider Size="L"/>) | |
} | |
} | |
this.append(current) | |
} | |
}, | |
onWin: { | |
didSubmitSearchText: function (e, data) { | |
if (data.text === '') return | |
Native.httpRequest({ | |
description: 'Serp', | |
url: 'https://searchlab.yandex.ru/web', | |
data: { | |
text: data.text, | |
}, | |
success: 'Serp:RequestSuccess', | |
error: 'Serp:RequestError', | |
}) | |
this.triggerWin('WillUpdate') | |
}, | |
'Serp:RequestSuccess': function (e, details) { | |
this.triggerWin('DidUpdate') | |
this.expand(details.data.get()) | |
}, | |
'Serp:RequestError': function (e, data) { | |
this.triggerWin('DidUpdate') | |
console.log('error', data) | |
} | |
} | |
}, | |
}) | |
/** | |
* @example | |
* <Serp> | |
* <head themeColor="#6888ad"> | |
* <user>ialexsid</user> | |
* <query>вконтакте</query> | |
* </head> | |
* <list> | |
* <Snippet href="#"> | |
* <title>[ВКонтакте] — Добро пожаловать</title> | |
* <url> | |
* <domain>m.vk.com</domain> | |
* <path>/feed</path> | |
* </url> | |
* <text>Поиск людей по их увлечениям, месту учебы и работы, персональным данным и т. д. Возможность создавать и вступать в группы по интересам, прослушивать музыку и смотреть фильмы онлайн.</text> | |
* </Snippet> | |
* </list> | |
* </Serp> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment