Last active
May 9, 2016 16:46
-
-
Save kovchiy/834975cf72d11c5d3b20a0f76ec4edc6 to your computer and use it in GitHub Desktop.
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
/* | |
* Фрагмент описания компонента Suggest гибридного приложения для iOS | |
*/ | |
Beast.decl({ | |
/* | |
* Основной блок | |
*/ | |
Suggest: { | |
// Модификаторы и параметры по умолчанию | |
mod: { | |
State: 'release' | |
}, | |
param: { | |
text: '' | |
}, | |
// Этап шаблонизации (теперь это модно называть render) | |
expand: function () { | |
if (!this.has()) { | |
this.append( | |
<list/>, | |
<fastanswer/> | |
) | |
} | |
}, | |
// Этап инициализации поведения | |
domInit: function () { | |
MissEvent.verticalSwipe(this.domNode()) | |
this.showHistory() | |
}, | |
// Подписка на события: | |
// изменения модификаторов | |
onMod: { | |
State: { | |
release: function (arrowBlur) { | |
if (arrowBlur) { | |
Native.call('setArrowFocus', {focus:false}) | |
} | |
} | |
} | |
}, | |
// DOM-события | |
on: { | |
didswipe: function (e, detail) { | |
if (detail.isFast) { | |
this.mod('state', 'release', true) | |
} | |
} | |
}, | |
// DOM-события окна | |
onWin: { | |
arrowDidFocus: function (e, data) { | |
this.update(data.text) | |
this.mod('state', 'active') | |
}, | |
arrowDidBlur: function () { | |
this.mod('state', 'release') | |
}, | |
arrowDidSubmitText: function () { | |
this.mod('state', 'release') | |
}, | |
arrowDidChangeText: function (e, data) { | |
this.update(data.text) | |
}, | |
'Suggest:RequestSuccess': function (e, details) { | |
var items = details.data[1].map(function (item) { | |
return typeof item === 'string' | |
? <item><text>{item}</text></item> | |
: false | |
}) | |
this.elem('list')[0].expand(items) | |
}, | |
'Suggest:FastAnswerRequestSuccess': function (e, details) { | |
this.elem('fastanswer')[0].expand( | |
Dig(details, 'data[0](title, url)', function () { | |
var snippet = ( | |
<Snippet href="{this.url}"> | |
<title>{this.title}</title> | |
<url/> | |
</Snippet> | |
) | |
Dig(this, 'display_items[](type, value)', function () { | |
if (this.type === 'paragraph') { | |
snippet.append( | |
<text>{this.value}</text> | |
) | |
} | |
else if (this.type === 'image') { | |
if (!snippet.has('thumb')) { | |
snippet.append( | |
<thumb>{this.value}</thumb> | |
) | |
} | |
} | |
}) | |
return snippet | |
}) | |
) | |
}, | |
}, | |
// Поход за данными | |
update: function (text) { | |
if (text === '' && this.param('text') !== '') { | |
this.param('text', '') | |
this.showHistory() | |
} else if (text !== this.param('text')) { | |
this.param('text', text) | |
this.elem('fastanswer')[0].empty() | |
Native.httpRequest({ | |
description: 'SuggestList', | |
url: '...', | |
data: { | |
part: text, | |
}, | |
success: 'Suggest:RequestSuccess', | |
error: 'Suggest:RequestError', | |
cancelable: true, | |
}) | |
Native.httpRequest({ | |
description: 'SuggestFastAnswer', | |
url: '...', | |
data: { | |
text: text, | |
}, | |
success: 'Suggest:FastAnswerRequestSuccess', | |
error: 'Suggest:FastAnswerRequestError', | |
cancelable: true, | |
}) | |
} | |
}, | |
// Захардкоженная история запросов (для целей прототипа пока не важно) | |
showHistory: function () { | |
this.elem('list')[0].expand( | |
<group> | |
<groupTitle>история поисков</groupTitle> | |
<item> | |
<text>аптеки льва толстого</text> | |
</item> | |
<item> | |
<text>выживший купить билет</text> | |
</item> | |
<item> | |
<text>оскар 2016</text> | |
</item> | |
<item> | |
<text>seinabo sey</text> | |
</item> | |
<item> | |
<text>iphone 5s купить маркет</text> | |
</item> | |
</group> | |
) | |
} | |
}, | |
/* | |
* Элементы | |
*/ | |
Suggest__list: { | |
expand: function () { | |
var list = <List>{this.get()}</List> | |
var parentBlock = this.parentBlock() | |
list.appendTo(this) | |
.elem('item') | |
.forEach(function (item) { | |
item.on('Release', function () { | |
parentBlock.mod('state', 'release') | |
Native.call('submitArrowText', {text: this.param('text')}) | |
}) | |
}) | |
} | |
}, | |
Suggest__fastanswer: { | |
inherits:'grid', | |
mod: { | |
MarginX:true | |
} | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment