Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==========================================================================
// Project: Inc.CheckboxListItemView
// Copyright: ©2010 My Company, Inc.
// ==========================================================================
/*globals Inc */
/** @class
(Document Your View Here)
@extends SC.View
*/
Inc.CheckboxListItemView = SC.ListItemView.extend(
/** @scope Inventory.TagListItemView.prototype */ {
/**
* You have to overwrite this to suit your needs
*/
isChecked: function() {
return NO;
},
/**
* Is called when the checkbox gets checked
*/
check: function() { },
/**
* Is called when the checkbox gets unchecked
*/
uncheck: function() { },
/**
Fills the passed html-array with strings that can be joined to form the
innerHTML of the receiver element. Also populates an array of classNames
to set on the outer element.
This implements some capability to handle the checkbox state.
@param {SC.RenderContext} context
@param {Boolean} firstTime
@returns {void}
*/
render: function(context, firstTime) {
var content = this.get('content'),
del = this.displayDelegate,
level = this.get('outlineLevel'),
indent = this.get('outlineIndent'),
key, value, working, classArray = [];
// add alternating row classes
classArray.push((this.get('contentIndex')%2 === 0) ? 'even' : 'odd');
context.setClass('disabled', !this.get('isEnabled'));
// outline level wrapper
working = context.begin("div").addClass("sc-outline");
if (level>=0 && indent>0) working.addStyle("left", indent*(level+1));
// handle disclosure triangle
value = this.get('disclosureState');
if (value !== SC.LEAF_NODE) {
this.renderDisclosure(working, value);
classArray.push('has-disclosure');
}
// handle checkbox
// Always render checkbox
this.renderCheckbox(working, this.get('isChecked'));
classArray.push('has-checkbox');
// handle label -- always invoke
key = this.getDelegateProperty('contentValueKey', del) ;
value = (key && content) ? (content.get ? content.get(key) : content[key]) : content ;
if (value && SC.typeOf(value) !== SC.T_STRING) value = value.toString();
if (this.get('escapeHTML')) value = SC.RenderContext.escapeHTML(value);
this.renderLabel(working, value);
// handle action
key = this.getDelegateProperty('listItemActionProperty', del) ;
value = (key && content) ? (content.get ? content.get(key) : content[key]) : null ;
if (value) {
this.renderAction(working, value);
classArray.push('has-action');
}
context.addClass(classArray);
context = working.end();
},
/** @private
Returns YES if the list item has a checkbox and the event occurred
inside of it.
*/
_isInsideCheckbox: function(evt) {
return this._isInsideElementWithClassName('sc-checkbox-view', evt);
},
mouseUp: function(evt) {
var ret= NO, del, checkboxKey, content, state, idx, set;
// if mouse was down in checkbox -- then handle mouse up, otherwise
// allow parent view to handle event.
if (this._isMouseDownOnCheckbox) {
// update only if mouse inside on mouse up...
if (this._isInsideCheckbox(evt)) {
del = this.displayDelegate ;
checkboxKey = this.getDelegateProperty('contentCheckboxKey', del);
content = this.get('content') ;
if (content && content.get) {
if (!this.get('isChecked')) {
this.check();
}
else {
this.uncheck();
}
this.displayDidChange(); // repaint view...
}
}
this._removeCheckboxActiveState() ;
ret = YES ;
// if mouse as down on disclosure -- handle mosue up. otherwise pass on
// to parent.
} else if (this._isMouseDownOnDisclosure) {
this._removeDisclosureActiveState();
ret = YES ;
// if mouse was down in right icon -- then handle mouse up, otherwise
// allow parent view to handle event.
} else if (this._isMouseDownOnRightIcon) {
this._removeRightIconActiveState() ;
ret = YES ;
}
// clear cached info
this._isMouseInsideCheckbox = this._isMouseDownOnCheckbox = NO ;
this._isMouseDownOnDisclosure = this._isMouseInsideDisclosure = NO ;
this._isMouseInsideRightIcon = this._isMouseDownOnRightIcon = NO ;
return ret ;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment