Skip to content

Commit

Permalink
Fixed issue that prevents dataviews from closing because of the widge…
Browse files Browse the repository at this point in the history
…t's unsubscribe handlers not being handled correctly

Cleaned up widget for readability.
  • Loading branch information
Rvthof committed Sep 23, 2015
1 parent 1e410d5 commit 12863d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 73 deletions.
File renamed without changes.
100 changes: 27 additions & 73 deletions src/LabelSelect/widget/LabelSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
define([
'dojo/_base/declare', 'mxui/widget/_WidgetBase', 'dijit/_TemplatedMixin',
'mxui/dom', 'dojo/dom', 'dojo/query', 'dojo/dom-prop', 'dojo/dom-geometry', 'dojo/dom-class', 'dojo/dom-style', 'dojo/dom-construct', 'dojo/_base/array', 'dojo/_base/lang', 'dojo/text',
'LabelSelect/lib/jquery-1.11.2.min', 'dojo/text!LabelSelect/widget/template/LabelSelect.html'
'LabelSelect/lib/jquery-1.11.2', 'dojo/text!LabelSelect/widget/template/LabelSelect.html'
], function (declare, _WidgetBase, _TemplatedMixin, dom, dojoDom, domQuery, domProp, domGeom, domClass, domStyle, domConstruct, dojoArray, lang, text, _jQuery, widgetTemplate) {
'use strict';
var $ = _jQuery.noConflict(true);

// Declare widget's prototype.
return declare('LabelSelect.widget.LabelSelect', [_WidgetBase, _TemplatedMixin], {
// _TemplatedMixin will create our dom node using this HTML template.
templateString: widgetTemplate,

// Internal variables. Non-primitives created in the prototype are shared between all widget instances.
_handles: null,
_contextObj: null,
_listBox: null,
Expand All @@ -24,14 +21,11 @@ define([
_refAttribute: null,
_tagCache: null,

// dojo.declare.constructor is called to construct the widget instance. Implement to initialize non-primitive properties.
constructor: function () {
this._handles = [];
},

// dijit._WidgetBase.postCreate is called after constructing the widget. Implement to do extra setup work.
postCreate: function () {
console.log(this.id + '.postCreate');
//set the variables:
this._tagEntity = this.tagAssoc.split('/')[1];
this._refAttribute = this.tagAssoc.split('/')[0];
Expand All @@ -46,9 +40,7 @@ define([
this._setupEvents();
},

// mxui.widget._WidgetBase.update is called when context is changed or initialized. Implement to re-render and / or fetch data.
update: function (obj, callback) {
console.log(this.id + '.update');
if (obj) {
domStyle.set(this.domNode, "visibility", "visible");

Expand All @@ -61,32 +53,7 @@ define([
callback();
},

// mxui.widget._WidgetBase.enable is called when the widget should enable editing. Implement to enable editing if widget is input widget.
enable: function () {

},

// mxui.widget._WidgetBase.enable is called when the widget should disable editing. Implement to disable editing if widget is input widget.
disable: function () {

},

// mxui.widget._WidgetBase.resize is called when the page's layout is recalculated. Implement to do sizing calculations. Prefer using CSS instead.
resize: function (box) {

},

// mxui.widget._WidgetBase.uninitialize is called when the widget is destroyed. Implement to do special tear-down work.
uninitialize: function () {
// Clean up listeners, helper objects, etc. There is no need to remove listeners added with this.connect / this.subscribe / this.own.
},

_setupEvents: function () {

},

_fetchCurrentLabels: function () {
console.log(this.id + '._fetchCurrentLabels');
//fetch all referenced labels
var xpath = '//' + this._tagEntity + this.tagConstraint.replace('[%CurrentObject%]', this._contextObj.getGuid());
mx.data.get({
Expand All @@ -97,10 +64,10 @@ define([


_processTags: function (objs) {
console.log(this.id + '._processTags');
var refObjs = this._contextObj.get(this._refAttribute),
tagArray = [],
currentTags = [];

dojoArray.forEach(objs, function (tagObj, index) {
//clean up the text
var value = dom.escapeString(tagObj.get(this._tagAttribute));
Expand All @@ -121,7 +88,6 @@ define([
},

_renderCurrentTags: function (currentTags) {
console.log(this.id + '._renderCurrentTags');
//we're not using the plugin function "remove all" because we don't want to remove references
var items = this._listBox.getElementsByTagName("li");
while (items.length > 0) {
Expand All @@ -143,7 +109,6 @@ define([
},

_startTagger: function (options) {
console.log(this.id + '._startTagger');
if (options) {
$('#' + this.id + '_ListBox').tagit(options);
} else {
Expand All @@ -155,7 +120,6 @@ define([


_createTagobject: function (value) {
console.log(this.id + '._createTagobject');
//create a new tag
mx.data.create({
entity: this._tagEntity,
Expand Down Expand Up @@ -187,7 +151,6 @@ define([
},

_execMf: function (guid, mf, cb) {
console.log(this.id + '._execMf');
if (guid && mf) {
mx.data.action({
applyto: 'selection',
Expand All @@ -203,18 +166,15 @@ define([
}
}, this);
}

},


_resetSubscriptions: function () {
console.log(this.id + '._resetSubscriptions');
// Release handle on previous object, if any.
var handle = null,
attrHandle = null,
validationHandle= null;

if (this._handles) {
if (this._handles && this._handles.length && this._handles.length > 0) {
dojoArray.forEach(this._handles, function (handle) {
this.unsubscribe(handle);
});
Expand Down Expand Up @@ -263,20 +223,19 @@ define([
},

_isReference: function (guid) {
console.log(this.id + '._isReference');
var isRef = false,
refs = this._contextObj.getReferences(this._refAttribute);
dojoArray.forEach(refs, function (ref, i) {
if (ref === guid) {
isRef = true;
}

dojoArray.forEach(refs, function (ref, i) {
if (ref === guid) {
isRef = true;
}
});

return isRef;
},

_saveObject: function () {
console.log(this.id + '._saveObject');
mx.data.save({
mxobj: this._contextObj,
callback: function () {
Expand All @@ -287,63 +246,60 @@ define([


_setOptions: function (tagArray) {
console.log(this.id + '._setOptions');
//TODO: allow users to set options
var self = this,
options = {
var options = {
availableTags: tagArray,
autocomplete: {
delay: 0,
minLength: 0
},
enableCreate: self.enableCreate,
showAutocompleteOnFocus: self.showAutoCompleteOnFocus,
enableCreate: this.enableCreate,
showAutocompleteOnFocus: this.showAutoCompleteOnFocus,
removeConfirmation: false,
caseSensitive: true,
allowDuplicates: false,
allowSpaces: false,
readOnly: self.readOnly,
readOnly: this.readOnly,
tagLimit: null,
singleField: false,
singleFieldDelimiter: ',',
singleFieldNode: null,
tabIndex: null,
placeholderText: null,

afterTagAdded: function (event, ui) {
self._clearValidations();
afterTagAdded: lang.hitch(this, function (event, ui) {
this._clearValidations();
//fetch tag from cache
var tagObj = self._tagCache[ui.tagLabel];
var tagObj = this._tagCache[ui.tagLabel];
if (tagObj) {
//check if already a reference
if (!self._isReference(tagObj.getGuid())) {
self._contextObj.addReference(self._refAttribute, tagObj.getGuid());
self._saveObject();
if (!this._isReference(tagObj.getGuid())) {
this._contextObj.addReference(this._refAttribute, tagObj.getGuid());
this._saveObject();
}
} else if (self.enableCreate) {
self._createTagobject(ui.tagLabel);
} else if (this.enableCreate) {
this._createTagobject(ui.tagLabel);
} else {
logger.warn('No Tag found for value: ' + ui.tagLabel);
}
},
}),

afterTagRemoved: function (event, ui) {
self._clearValidations();
afterTagRemoved: lang.hitch(this, function (event, ui) {
this._clearValidations();
//fetch tag from cache
var tagObj = self._tagCache[ui.tagLabel];
var tagObj = this._tagCache[ui.tagLabel];
if (tagObj) {
self._contextObj.removeReferences(self._refAttribute, [tagObj.getGuid()]);
self._saveObject();
this._contextObj.removeReferences(this._refAttribute, [tagObj.getGuid()]);
this._saveObject();
} else {
logger.warn('No Tag found for value: ' + ui.tagLabel);
}
}
})
};
this._startTagger(options);
},

_handleValidation: function (validations) {

this._clearValidations();

var val = validations[0],
Expand All @@ -370,9 +326,7 @@ define([
});

this.domNode.appendChild(this._alertdiv);

}

});
});

Expand Down

0 comments on commit 12863d2

Please sign in to comment.