ShareBox.SuggestBox.Box.prototype = {
_constructor: function(options) {
this.handler = [];
this.container = $('<div class="suggest-box"></div>').hide().appendTo('body').get(0);
this.warp = $('<ul class="warpper"></ul>').appendTo(this.container).get(0);
},
_transform: function(html, source) {
var fixes = html.match(/\{[^\{]*\}/ig);
if (fixes == null) return;
fixes = fixes.toString()
.replace(/[\{\}]+/g, '')
.split(',');
//写入 data-link 属性
for (var i = 0, f = fixes, s = source; i < f.length; i ++) {
if (s.hasOwnProperty(f[i])) {
if ('object' === typeof s[f[i]]) {
var datas = toJSON(s[f[i]]);
html = html.replace('{' + f[i] + '}', datas);
continue;
}
html = html.replace('{' + f[i] + '}', s[f[i]]);
}
}
return html;
},
//将source转化成html
transform: function(source, code) {
var list = [];
if (source && source.length > 0) {
for (var i = 0, s = source; i < s.length; i ++) {
list.push(this._transform(code || OPTIONS.ShareBox.label, s[i]));
}
}
return list.join('');
},
empty: function() {
$(this.warp).empty();
return this;
},
put: function(list) {
$(this.warp).append(list);
return this;
},
getDatas: function(item) {
var datas = (item ? $(item) : $(this.warp).children().filter('.focus'))
.attr('data-link');
return $.parseJSON(datas);
},
hide: function() {
$(this.container).hide();
this.logout();
return this;
},
at: function(options, cursor_pos) {
var self = this;
var type = options.type, source = options.source, field = options.field, $field = $(field);
this.field = field;
this.empty();
this.put(this.transform(source));
this.logout();
var insert = function() {
setTimeout(function() {
var $eidtor = $field.shareBox();
$eidtor.select(cursor_pos.startPos + 1, cursor_pos.endPos);
var datas = self.getDatas();
$eidtor.insert(datas.value + ' ');
}, 1);
};
this.boxKeyDownHandle = function(e) {
var $l = $(self.warp).children();
switch(e.keyCode) {
//focus prev
case 38: //key ↑
var k = $l.index($l.filter('.focus'));
k = k <= 0 ? $l.length : k;
$l.removeClass('focus')
.eq(k-1)
.mouseover();
return false; //prohibit browers scroll event
//focus next
case 40: //key ↓
var k = $l.index($l.filter('.focus'));
k = k >= $l.length - 1 ? -1 : k;
$l.removeClass('focus')
.eq(k+1)
.mouseover();
return false;
//insert
case 9: //key Tab
case 13: //key Enter
insert();
self.hide();
return false;
//cancle
case 8: //key Backspace
case 27: //key Esc
$field.shareBox().insert('');
self.hide();
return;
}
};
//focus style
this.listMouseoverHandle = function(e) {
$(self.warp).children().removeClass('focus');
$(this).addClass('focus');
};
//select auto insert
this.listClickHandle = function(e) {
insert();
self.hide();
};
this.docClickHandle = function() {
self.hide();
};
$field.bind('keydown', this.boxKeyDownHandle);
$(this.warp).children().bind('mouseover', this.listMouseoverHandle)
.bind('click', this.listClickHandle)
.eq(0).mouseover();
$(document).bind('click', this.docClickHandle);
//position
var pos = $field.shareBox().getCursorOffset();
$(this.container).css({left: pos.left,top:
pos.top}).show();
suggestBox.active = this;
return this;
},
logout: function() {
if ($.isFunction(this.boxKeyDownHandle)) $(this.field).unbind('keydown', this.boxKeyDownHandle);
if ($.isFunction(this.boxKeyDownHandle)) $(this.field).unbind('keydown', this.boxKeyDownHandle);
var $c = $(this.warp).children();
if ($.isFunction(this.listMouseoverHandle)) $c.unbind('mouseover', this.listMouseoverHandle);
if ($.isFunction(this.listMouseoverHandle)) $c.unbind('click', this.listMouseoverHandle);
if ($.isFunction(this.docClickHandle)) $(document).unbind('click', this.docClickHandle);
this.boxKeyDownHandle = this.listMouseoverHandle = this.listClickHandle = this.docClickHandle = undefined;
}
};