Exemplo n.º 1
0
class WaveModel(object):
	"""
	Wave model class. Top level model which sends all events.
	
	@class {public} pygowave.model.WaveModel
	"""
	
	# --- Event documentation ---
	"""
	Fired if a Wavelet has been added.
	@event onWaveletAdded
	@param {String} waveletId ID of the Wavelet that has been added
	@param {Boolean} isRoot True if this is the (new) root Wavelet
	"""
	
	"""
	Fired before a wavelet is removed.
	@event onWaveletAboutToBeRemoved
	@param {String} waveletId ID of the Wavelet that will be removed
	"""
	# ---------------------------
	
	def __init__(self, waveId, viewerId):
		"""
		Called on instantiation.
		@constructor {public} initialize
		@param {String} waveId ID of the Wave
		@param {String} viewerId ID of the viewer
		"""
		self._rootWavelet = None
		self._waveId = waveId
		self._viewerId = viewerId
		self._wavelets = Hash()

	def id(self):
		"""
		Returns the ID of this Wave.
		
		@function {public String} id
		"""
		return self._waveId

	def viewerId(self):
		"""
		Returns the ID of the viewer.
		
		@function {public String} viewerId
		"""
		return self._viewerId

	def loadFromSnapshot(self, obj, participants):
		"""
		Load the wave's contents from a JSON-serialized snapshot and a map of
		participant objects.
		
		@function {public} loadFromSnapshot
		@param {Object} obj The JSON-serialized snapshot to load
		@param {Hash} participants A map of participant objects
		"""
		
		rootWavelet = obj["wavelet"]
		
		wvl_options = {
			"creator": participants[rootWavelet["creator"]],
			"is_root": True,
			"created": rootWavelet["creationTime"],
			"last_modified": rootWavelet["lastModifiedTime"],
			"title": rootWavelet["title"],
			"version": rootWavelet["version"]
		}
		
		rootWaveletObj = self.createWavelet(rootWavelet["waveletId"], wvl_options)
		
		for part_id in rootWavelet["participants"]:
			rootWaveletObj.addParticipant(participants[part_id])
		
		rootWaveletObj.loadBlipsFromSnapshot(obj["blips"], rootWavelet["rootBlipId"], participants);

	def createWavelet(self, id, options):
		"""
		Create a Wavelet and add it to this Wave. For options see the
		{@link pygowave.model.Wavelet.initialize Wavelet constructor}.<br/>
		Note: Fires {@link pygowave.model.WaveModel.onWaveletAdded onWaveletAdded}
		
		@function {public Wavelet} createWavelet
		@param {String} id Wavelet ID
		@param {Object} options Information about the Wavelet.
		"""
		w = Wavelet(self, id, options)
		self._wavelets.set(id, w)
		self.fireEvent('waveletAdded', [id, w.isRoot()])
		return w
	
	def wavelet(self, waveletId):
		"""
		Return a Wavelet of this Wave by its ID.
		
		@function {public Wavelet} wavelet
		@param {String} waveletId ID of the Wavelet
		"""
		return self._wavelets.get(waveletId)
	
	def allWavelets(self):
		"""
		Return a list of all Wavelets on this Wave.
		
		@function {public Wavelet[]} allWavelets
		"""
		return self._wavelets.getValues()

	def rootWavelet(self):
		"""
		Returns the root Wavelet of this Wave.
		
		@function {public Wavelet} rootWavelet
		"""
		return self._rootWavelet
	
	def _setRootWavelet(self, wavelet):
		"""
		Internal method to set the root Wavelet. Not intended to be called
		outside of this implementation.
		
		@function {private} _setRootWavelet
		@param {Wavelet} wavelet Wavelet to be set as root Wavelet
		"""
		self._rootWavelet = wavelet
	
	def removeWavelet(self, waveletId):
		"""
		Removes and deletes a wavelet by its id. Fires
		{@link pygowave.model.WaveModel.onWaveletAboutToBeRemoved} beforehand.
		
		@function {public} removeWavelet
		@param {String} waveletId ID of the Wavelet to remove
		"""
		if not self._wavelets.has(waveletId):
			return
		
		self.fireEvent('waveletAboutToBeRemoved', waveletId)
		wavelet = self._wavelets.get(waveletId)
		self._wavelets.erase(waveletId)
		if wavelet == self._rootWavelet:
			self._rootWavelet = None