nativescript-tag
Version:
NativeScript UI plugin for tagging
342 lines (341 loc) • 15.9 kB
JavaScript
"use strict";
var common = require("./tag.common");
var app = require("application");
var color_1 = require('color');
function onValuePropertyChanged(data) {
var tagGroup = data.object;
if (!tagGroup.android) {
return;
}
var arrJava = Array.create(java.lang.String, data.newValue.length);
data.newValue.forEach(function (item, index) {
arrJava[index] = item;
});
var tagList = java.util.Arrays.asList(arrJava);
tagGroup.tagGroup.setTags(tagList);
if (tagGroup.ntag_autoComplete) {
tagGroup.tagGroup.removeViewAt(tagGroup.tagGroup.getChildCount() - 1);
}
}
common.TagGroup.valueProperty.metadata.onSetNativeValue = onValuePropertyChanged;
require("utils/module-merge").merge(common, module.exports);
var TagGroup = (function (_super) {
__extends(TagGroup, _super);
function TagGroup() {
_super.call(this);
this.ntag_editMode = false;
this.ntag_autoComplete = false;
}
Object.defineProperty(TagGroup.prototype, "android", {
get: function () {
return this._android;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TagGroup.prototype, "tagGroup", {
get: function () {
return this._tagGroup;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TagGroup.prototype, "autoCompleteTags", {
get: function () {
return this._autoCompleteTags;
},
set: function (val) {
this._autoCompleteTags = val;
if (val) {
this.autoCompleteTagsUpdate(val);
}
},
enumerable: true,
configurable: true
});
TagGroup.prototype._createUI = function () {
this._tagGroup = new me.gujun.android.taggroup.TagGroup(this._context);
this.styleTags();
var thisObj = this;
if (this.ntag_editMode) {
var f = this._tagGroup.getClass().getDeclaredField("isAppendMode");
f.setAccessible(true);
f.setBoolean(this._tagGroup, true);
this._android = this._tagGroup;
var tagGroup_1 = this._tagGroup;
var tagGroupClickListener = new android.view.View.OnClickListener({
onClick: function (view) {
tagGroup_1.submitTag();
}
});
this._tagGroup.appendInputTag();
this._tagGroup.setOnClickListener(tagGroupClickListener);
}
else if (this.ntag_autoComplete) {
var f = this._tagGroup.getClass().getDeclaredField("isAppendMode");
f.setAccessible(true);
f.setBoolean(this._tagGroup, true);
var AutoCompleteTextView = android.widget.AutoCompleteTextView;
var LinearLayout = android.widget.LinearLayout;
var LayoutParams = android.widget.LinearLayout.LayoutParams;
var context = app.android.context;
var root = new LinearLayout(context);
root.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
root.setOrientation(LinearLayout.VERTICAL);
this._autoCompleteTextView = new AutoCompleteTextView(context);
this._autoCompleteTextView.setThreshold(1);
var that_1 = new WeakRef(this);
var tagGroup_2 = this._tagGroup;
var autoComplete_1 = this._autoCompleteTextView;
this._autoCompleteTextView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener({
onItemClick: function (parent, view, position, id) {
var owner = that_1.get();
if (owner) {
var currentTags = tagGroup_2.getTags();
var newTags = new java.util.ArrayList(java.util.Arrays.asList(currentTags));
newTags.add(parent.getItemAtPosition(position));
tagGroup_2.setTags(newTags);
tagGroup_2.removeViewAt(tagGroup_2.getChildCount() - 1);
owner._onPropertyChangedFromNative(TagGroup.valueProperty, thisObj.convertJavaToJSArr(newTags.toArray()));
autoComplete_1.setText(null);
}
}
}));
this._autoCompleteTextView.setOnClickListener(new android.view.View.OnClickListener({
onClick: function (view) {
if (view.getText().toString()) {
var owner = that_1.get();
if (owner) {
var currentTags = tagGroup_2.getTags();
var newTags = new java.util.ArrayList(java.util.Arrays.asList(currentTags));
newTags.add(view.getText().toString());
tagGroup_2.setTags(newTags);
tagGroup_2.removeViewAt(tagGroup_2.getChildCount() - 1);
owner._onPropertyChangedFromNative(TagGroup.valueProperty, thisObj.convertJavaToJSArr(newTags.toArray()));
view.setText(null);
}
}
}
}));
this.styleAutoComplete();
root.addView(this._autoCompleteTextView);
root.addView(this._tagGroup);
this._android = root;
}
else {
this._android = this._tagGroup;
}
var that = new WeakRef(this);
var tagChangeListener = new me.gujun.android.taggroup.TagGroup.OnTagChangeListener({
onAppend: function (tagGroup, newTag) {
var instance = that.get();
if (instance) {
var newTags = new java.util.ArrayList(java.util.Arrays.asList(tagGroup.getTags()));
instance._onPropertyChangedFromNative(TagGroup.valueProperty, thisObj.convertJavaToJSArr(newTags.toArray()));
}
},
onDelete: function (tagGroup, deletedTag) {
var instance = that.get();
if (instance) {
var newTags = new java.util.ArrayList(java.util.Arrays.asList(tagGroup.getTags()));
instance._onPropertyChangedFromNative(TagGroup.valueProperty, thisObj.convertJavaToJSArr(newTags.toArray()));
}
}
});
this._tagGroup.setOnTagChangeListener(tagChangeListener);
var tagClickListener = new me.gujun.android.taggroup.TagGroup.OnTagClickListener({
onTagClick: function (tag) {
var instance = that.get();
if (instance && instance.ntag_tagClick) {
instance.notify({ eventName: TagGroup.TAG_CLICK_EVENT, object: instance, data: tag });
if (typeof (instance.ntag_tagClick) === 'function') {
instance.ntag_tagClick(tag);
}
}
}
});
this._tagGroup.setOnTagClickListener(tagClickListener);
};
TagGroup.prototype.autoCompleteTagsUpdate = function (val) {
var ArrayAdapter = android.widget.ArrayAdapter;
var arr = Array.create(java.lang.String, val.length);
val.forEach(function (item, index) {
arr[index] = item;
});
var adapter = new ArrayAdapter(this._context, android.R.layout.simple_list_item_1, arr);
this._autoCompleteTextView.setAdapter(adapter);
};
TagGroup.prototype.convertJavaToJSArr = function (newTags) {
var newTagsJs = [];
for (var i = 0; i < newTags.length; i++) {
newTagsJs.push(newTags[i]);
}
return newTagsJs;
};
TagGroup.prototype.styleAutoComplete = function () {
if (!this.ntag_inputTextColor) {
this._autoCompleteTextView.setTextColor(new color_1.Color('#DE000000').android);
}
else {
this._autoCompleteTextView.setTextColor(new color_1.Color(this.ntag_inputTextColor).android);
}
if (!this.ntga_acPopupBg) {
this._autoCompleteTextView.setDropDownBackgroundDrawable(new android.graphics.drawable.ColorDrawable(new color_1.Color('#F5F8FA').android));
}
else {
this._autoCompleteTextView.setDropDownBackgroundDrawable(new android.graphics.drawable.ColorDrawable(new color_1.Color(this.ntga_acPopupBg).android));
}
if (this.ntag_inputHint) {
this._autoCompleteTextView.setHint(this.ntag_inputHint);
}
if (!this.ntag_inputHintColor) {
this._autoCompleteTextView.setHintTextColor(new color_1.Color('#80000000').android);
}
else {
this._autoCompleteTextView.setHintTextColor(new color_1.Color(this.ntag_inputHintColor).android);
}
if (!this.ntag_textSize) {
var textSize = this._tagGroup.sp2px(13.0);
this._autoCompleteTextView.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, textSize);
}
else {
var textSize = this._tagGroup.sp2px(this.ntag_textSize);
this._autoCompleteTextView.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, textSize);
}
};
TagGroup.prototype.styleTags = function () {
var AndroidColor = android.graphics.Color;
if (this.ntag_borderColor) {
var f = this._tagGroup.getClass().getDeclaredField("borderColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_borderColor));
}
if (this.ntag_textColor) {
var f = this._tagGroup.getClass().getDeclaredField("textColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_textColor));
}
if (this.ntag_bgColor) {
var f = this._tagGroup.getClass().getDeclaredField("backgroundColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_bgColor));
}
if (this.ntag_dashBorderColor) {
var f = this._tagGroup.getClass().getDeclaredField("dashBorderColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_dashBorderColor));
}
if (this.ntag_inputHintColor) {
var f = this._tagGroup.getClass().getDeclaredField("inputHintColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_inputHintColor));
}
if (this.ntag_inputTextColor) {
var f = this._tagGroup.getClass().getDeclaredField("inputTextColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_inputTextColor));
}
if (this.ntag_checkedBorderColor) {
var f = this._tagGroup.getClass().getDeclaredField("checkedBorderColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_checkedBorderColor));
}
if (this.ntag_checkedTextColor) {
var f = this._tagGroup.getClass().getDeclaredField("checkedTextColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_checkedTextColor));
}
if (this.ntag_checkedMarkerColor) {
var f = this._tagGroup.getClass().getDeclaredField("checkedMarkerColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_checkedMarkerColor));
}
if (this.ntag_checkedBgColor) {
var f = this._tagGroup.getClass().getDeclaredField("checkedBackgroundColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_checkedBgColor));
}
if (this.ntag_pressedBgColor) {
var f = this._tagGroup.getClass().getDeclaredField("pressedBackgroundColor");
f.setAccessible(true);
f.setInt(this._tagGroup, AndroidColor.parseColor(this.ntag_pressedBgColor));
}
if ((this.ntag_small || this.ntag_large) && !(this.ntag_small && this.ntag_large)) {
var textSize = void 0;
var hSpacing = void 0;
var vSpacing = void 0;
var hPadding = void 0;
var vPadding = void 0;
if (this.ntag_small) {
textSize = 10;
hSpacing = 6;
vSpacing = 3;
hPadding = 8;
vPadding = 2;
}
else if (this.ntag_large) {
textSize = 15;
hSpacing = 9;
vSpacing = 5;
hPadding = 14;
vPadding = 4;
var f_1 = this._tagGroup.getClass().getDeclaredField("borderStrokeWidth");
f_1.setAccessible(true);
f_1.setFloat(this._tagGroup, this._tagGroup.dp2px(0.7));
}
var f = this._tagGroup.getClass().getDeclaredField("textSize");
f.setAccessible(true);
f.setFloat(this._tagGroup, this._tagGroup.sp2px(textSize));
f = this._tagGroup.getClass().getDeclaredField("horizontalSpacing");
f.setAccessible(true);
f.setInt(this._tagGroup, this._tagGroup.dp2px(hSpacing));
f = this._tagGroup.getClass().getDeclaredField("verticalSpacing");
f.setAccessible(true);
f.setInt(this._tagGroup, this._tagGroup.dp2px(vSpacing));
f = this._tagGroup.getClass().getDeclaredField("horizontalPadding");
f.setAccessible(true);
f.setInt(this._tagGroup, this._tagGroup.dp2px(hPadding));
f = this._tagGroup.getClass().getDeclaredField("verticalPadding");
f.setAccessible(true);
f.setInt(this._tagGroup, this._tagGroup.dp2px(vPadding));
}
if (this.ntag_textSize) {
var f = this._tagGroup.getClass().getDeclaredField("textSize");
f.setAccessible(true);
f.setFloat(this._tagGroup, this._tagGroup.sp2px(this.ntag_textSize));
}
if (this.ntag_borderStrokeWidth) {
var f = this._tagGroup.getClass().getDeclaredField("borderStrokeWidth");
f.setAccessible(true);
f.setFloat(this._tagGroup, this._tagGroup.dp2px(this.ntag_borderStrokeWidth));
}
if (this.ntag_hSpacing) {
var f = this._tagGroup.getClass().getDeclaredField("horizontalSpacing");
f.setAccessible(true);
f.setInt(this._tagGroup, this._tagGroup.dp2px(this.ntag_hSpacing));
}
if (this.ntag_vSpacing) {
var f = this._tagGroup.getClass().getDeclaredField("verticalSpacing");
f.setAccessible(true);
f.setInt(this._tagGroup, this._tagGroup.dp2px(this.ntag_vSpacing));
}
if (this.ntag_hPadding) {
var f = this._tagGroup.getClass().getDeclaredField("horizontalPadding");
f.setAccessible(true);
f.setInt(this._tagGroup, this._tagGroup.dp2px(this.ntag_hPadding));
}
if (this.ntag_vPadding) {
var f = this._tagGroup.getClass().getDeclaredField("verticalPadding");
f.setAccessible(true);
f.setInt(this._tagGroup, this._tagGroup.dp2px(this.ntag_vPadding));
}
if (this.ntag_inputHint) {
var f = this._tagGroup.getClass().getDeclaredField("inputHint");
f.setAccessible(true);
f.set(this._tagGroup, this.ntag_inputHint);
}
};
TagGroup.TAG_CLICK_EVENT = 'ntag_tagClick';
return TagGroup;
}(common.TagGroup));
exports.TagGroup = TagGroup;